【问题标题】:How to loop icons in flutter?如何在颤动中循环图标?
【发布时间】:2020-09-05 02:01:03
【问题描述】:

我有数据列表,我想声明每个列表都有一个图标...每当用户单击图标时..图标将更改为另一个图标..这是代码

ListView.builder(
              itemCount: data.length,
              itemBuilder: (context, index) {
                return Column(
                  children: <Widget>[
                    ListTile(
                      leading: InkWell(
                        onTap: () {
                          setState((){
                             match=true;
                           });
                        },
                        child: 
                         match
                             ? Icons.check
                             : Icons.error
                      ),
                      title: Text(data[index],),
                      subtitle: Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          Row(
                            children: [
                              Text("subs here.."),
                            ],
                          ),
                        ],
                      ),
                    ),
                  ],
                );
              }),

从该代码..用户单击第一个图标后...列表视图构建器中的所有图标都已更改..有没有办法仅更改我单击的图标(不是所有图标)的图标?

【问题讨论】:

    标签: loops flutter widget icons


    【解决方案1】:

    您可以维护您为匹配变量所做的选定列表小部件的索引

                         setState((){
                                 match=true;
                                 _index = index;
                                 });
    

    然后添加一个类似的条件

               match && _index == index
                         ? Icons.check
                         : Icons.error
                  ),
    

    【讨论】:

    • 我喜欢这个答案。它非常优化。这应该被标记为正确答案。
    • 嗨..等等..我会试试的,很快就会告诉你结果
    • 它工作得很好,就像我想要的一样。非常感谢你的帮助:D
    【解决方案2】:

    将列表项分隔在其自己的有状态小部件中,以便每个列表项都可以保存自己的状态。

    class ListItem extends StatefulWidget {
      @override
      _ListItemState createState() => _ListItemState();
    }
    
    class _ListItemState extends State<ListItem> {
      bool match = false;
    
      @override
      Widget build(BuildContext context) {
        return ListTile(
          leading: InkWell(
              onTap: () {
                setState(() {
                  match = true;
                });
              },
              child: Icon(match ? Icons.check : Icons.error)),
          title: Text(
            "asd",
          ),
          subtitle: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Row(
                children: [
                  Text("subs here.."),
                ],
              ),
            ],
          ),
        );
      }
    }
    

    将小部件添加到 ListView

    itemBuilder: (context, index) {
                  return ListItem();
                }
    

    【讨论】:

    • 你好..我会试试,让你知道结果
    猜你喜欢
    • 2020-12-17
    • 2021-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-20
    • 1970-01-01
    相关资源
    最近更新 更多