如何在颤动中用 for 循环替换 CARD

Posted

技术标签:

【中文标题】如何在颤动中用 for 循环替换 CARD【英文标题】:how do i replace CARD with a for loop in flutter 【发布时间】:2021-02-16 07:57:17 【问题描述】:

我想用 for 循环替换卡片。这是我显示输出的屏幕主体

body: FutureBuilder<List>
       (
         future: db.getAllRecords("EMPLOYEE"),
        initialData: List(),
        builder: (context,snapshot)
          return snapshot.hasData
              ? ListView.builder(
                itemCount: snapshot.data.length,
                itemBuilder: (_, int position)
                 final item =snapshot.data[position];
                 return Card(
                   child:ListTile(
                     title: Text(
                        snapshot.data[position].row[1]
                     ),
                   ),
                 );
                ,
          )
             : Center(
                child:CircularProgressIndicator() ,
              );

【问题讨论】:

【参考方案1】:

其实你写的方式是 Flutter 中的标准实现方式。您可以调用默认的 ListView 构造函数而不是 ListView.builder 构造函数,然后调用 for 循环,但这不是最佳做法。

List<Widget> _buildListItems(BuildContext context, List<...> list) 
    final output = List<Widget>();
    for (var item in list) 
        output.add(
            Card(
                child: ListTile(
                    title: Text(item.row[1]),
                ),
            ),
        );
    
    return output;


//... back to your build(context) body

    body: FutureBuilder<List>(
        future: db.getAllRecords("EMPLOYEE"),
        builder: (context, snapshot) 
            return snapshot.hasData
                ? ListView(
                    children: [
                        ..._buildListItems(context, snapshot.data),
                    ],
                )
                : Center(
                    child: CircularProgressIndicator(),
                );
        
    )

【讨论】:

好吧,我会用我无法使用卡的员工(最多 5 人)发送短信,还有其他方法吗? 您的评论似乎是一个完全不同的问题,与 UI 无关。您可以使用pub.dev/packages/flutter_sms 发送短信。我建议发布一个新问题,详细说明您想要实现的目标。

以上是关于如何在颤动中用 for 循环替换 CARD的主要内容,如果未能解决你的问题,请参考以下文章

在颤动中用参数推动替换的最佳方法是啥?

如何在VS中用for循环输出2到100中所以素数?

有没有办法在 XSLT 转换中用应用模板替换 for-each?

flutter : 如何在颤动中制作卡片

我们如何在颤动中制作漂亮的瓷砖?

如何使用 vue 创建动态网格?