Is there a way to find an index number, of a particular item using ListView
instead of ListView.builder
? I want to then, set backgrounds to a particular item on the list. Say every third item to be Red in background. My lists are 10-15 items in length.
This is my List
// List - Words
class WordList extends StatelessWidget {
final List<WordModal> _wordModal;
WordList(this._wordModal);
@override
Widget build(BuildContext context) {
return new ListView(
padding: new EdgeInsets.symmetric(vertical: 8.0),
children: _buildList(),
);
}
List<WordCard> _buildList() {
return _wordModal.map((word) => new WordCard(word)).toList();
}
}
This is my Card Builder
// Word Card Item
class WordCard extends StatelessWidget {
final WordModal _genericModal;
WordCard(this._genericModal);
@override
Widget build(BuildContext context) {
return new Card(
margin: new EdgeInsets.symmetric(horizontal: 10.0, vertical: 6.0),
child: Container(
//color: ** want to put background color here **
child: Column(
children: <Widget>[
new ExpansionTile(
title: Container(
child: Column(
children: <Widget>[
new ListTile(
leading: new CircleAvatar(
child: Icon(Icons.image, color: Colors.grey), backgroundColor: Colors.white,),
title: Text(_genericModal.animalGenus),
subtitle: Text(
_genericModal.animalSpecies,
),
),
],
),
),
children: <Widget>[
Row(
children: <Widget>[
SizedBox(
width: 50,
),
Text(
_genericModal.animalHabitatLocation,
style: (TextStyle(fontStyle: FontStyle.italic)),
),
],
),
new Row(
children: <Widget>[
Spacer(),
IconButton(
icon: new Icon(
Icons.volume_up,
size: 28,
color: Colors.grey,
),
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => DetailPage()));
}),
],
),
],
),
],
),
));
}
}
here is some minimal implementation
int _selectedIndex;
_onSelected(int index) {
setState(() {
_selectedIndex = index;
});
}
and then in your listview the widget you want to change color
ListView.builder(
itemCount: querySnapshot.documents.length,
padding: EdgeInsets.all(8.0),
itemBuilder: (context, i) {
...
IconButton(
iconSize: 28,
icon: Icon(
Icons.favorite_border,
color: _selectedIndex != null && _selectedIndex == i
? Colors.redAccent
: Colors.grey,
),
onPressed: () {
_onSelected(i);
Yes, you can try to change it inside tour itemBuilder function, I'll leave you an example here:
class PageTwoState extends State<PageTwo> {
@override
Widget build(BuildContext context) {
return ListView.builder(
itemExtent: 250.0,
itemBuilder: (context, index) => Container(
padding: EdgeInsets.all(10.0),
child: Material(
elevation: 4.0,
borderRadius: BorderRadius.circular(5.0),
color: index % 2 == 0 ? Colors.cyan : Colors.deepOrange,
child: Center(
child: Text(index.toString()),
),
),
),
);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.