Alternating background colors on ListView.builder
You can use the index
provided to the item builder to set the color, and if you want to use a ListTile
you can easily give it a background color by wrapping it in a Container
:
ListView.builder(
itemBuilder: (BuildContext context, int index) {
return Container(
color: (index % 2 == 0) ? Colors.red : Colors.green,
child: ListTile(
title: ...
),
);
},
)
Got it.
Instead of using ListTile
I can use a Card
to have the color property accessible. Then with @pskink's suggestion, I can use the index to determine whether it's odd or even.