如何使用开关语句,或者如何使用语句向列表 View 添加多种颜色
我想在下面的代码中为10种不同的颜色使用switch语句。如果您知道此答案,请帮助
ListView.builder(
itemBuilder: (BuildContext context, int index) {
return Container(
color: (index % 10 == 0) ? Colors.red : Colors.green,
child: ListTile(
title: ...
),
);
},
)
如果您需要更多代码,请对问题发表评论
最佳答案
itemBuilder: (BuildContext context, int index) {
Color color;
switch (index % 10) {
case 0:
color = Colors.red;
break;
case 1:
color = Colors.blue;
break;
... // and so on, up to 9
default:
color = Colors.black;
}
return Container(
color: color,
child: ListTile(
title: ...
),
);
},
关于flutter - 使用Switch语句在Flutter ListView中添加多种颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58740961/