使用Switch语句在Flutter ListView中添加多种颜色

如何使用 switch语句或其他方式语句向列表视图添加多种颜色

我想在下面的代码中将switch语句用于10种不同的颜色。如果您知道此答案,请帮助

ListView.builder(
  itemBuilder: (BuildContext context,int index) {
    return Container(
      color: (index % 10 == 0) ? Colors.red : Colors.green,child: ListTile(
        title: ...
      ),);
  },)

如果您需要更多代码,请对问题发表评论

izying110 回答:使用Switch语句在Flutter ListView中添加多种颜色

  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: ...
      ),);
  },
,

这就是我所说的随机颜色:

Color((math.Random().nextDouble() * 0xFFFFFF).toInt() << 0)
              .withOpacity(1.0);

在您的情况下:

Color(index << 0).withOpacity(1.0);
,
 child: Container(
        width: 100,height: 100,color: index % 4 == 0
               ? Colors.green
               : index % 4 == 1
               ? Colors.amber
               : index % 4 == 2
               ? Colors.pink
               : Colors.lime,)

如果梯形图也在同一行中寻找开关箱,那么如果您真的想单行执行此操作,则这样

本文链接:https://www.f2er.com/3147941.html

大家都在问