【问题标题】:How to make a listview table be zipped every 4如何使列表视图表每 4 次压缩一次
【发布时间】:2020-04-16 13:33:42
【问题描述】:

如何让列表视图表格每 4 次压缩一次。 项目而不是 1 白色 1 黑色 .. 然而,我设法用奇数或偶数来做代码:

return Expanded(
                    child: ListView.builder(
                        itemCount: snapshot.data.length,
                        itemBuilder: (context, index) {

                          return Column(
                            children: <Widget>[

                              Container(
                                color: **index.isOdd** 
                                    ? Colors.black12
                                    : Colors.white,
                                child: Row(
                                  children: <Widget>[
                                    Expanded(
                                      child: Text(
                                        "${snapshot.data[index].descricaoRoupa} ",
                                        textAlign: TextAlign.center,
                                        style: TextStyle(fontSize: 12),
                                      ),
                                    ),

我希望我可以改为使用 index.odd 来使用 index * 4 类似的东西 白色的 白色的 白色的 白色的 黑色的 黑色的 黑色的 黑色的 白色的 白色的 白色的 白色的 黑色的 黑色的 黑色的 黑色的 白色的 ...

【问题讨论】:

    标签: arrays flutter dart mobile data-structures


    【解决方案1】:

    您必须使用 MOD 功能。指数 % 8 = 0-7

    if (index % 8 < 4 ) // 0-3
    {
      //first 4
    }
    else // 4-7
    {
      // second 4
    }
    

    在你的代码中这样做:

    Container( 
      color: index % 8 < 4 ? Colors.black12 : Colors.white,
      // ...
    }
    

    【讨论】:

      猜你喜欢
      • 2018-10-13
      • 1970-01-01
      • 2015-12-22
      • 1970-01-01
      • 2021-11-25
      • 2018-01-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多