简体   繁体   English

如何在 flutter 中的 ListView.builder 中循环遍历 ListTile

[英]How to Loop through a ListTile from ListView.builder in flutter

I am trying to display a list tile in flutter based on list of data from cloud firestore.我正在尝试根据来自云 Firestore 的数据列表在 flutter 中显示列表图块。 I want the leading icon to change for each tile when the tile is tapped.我希望在点击磁贴时为每个磁贴更改前导图标。 My problem is whenever I tap any tile, the whole list changes.我的问题是每当我点击任何图块时,整个列表都会改变。 I want only the tapped tile to change.我只想改变被敲击的瓷砖。 Here is my code:这是我的代码:

 StreamBuilder(
                        stream: Firestore.instance
                            .collection('Recharge_Card')
                            .snapshots(),
                        //print an integer every 2secs, 10 times
                        builder: (context, snapshot) {
                          if (!snapshot.hasData) {
                            return Text("Loading..");
                          }
                          return SizedBox(
                            height: _height / 1.9,
                            child: ListView.builder(
                              // itemExtent: 80.0,
                              itemCount: snapshot.data.documents.length,
                              itemBuilder: (context, index) {
                                DocumentSnapshot myCards =
                                    snapshot.data.documents[index];

                                return Card(
                                  elevation: 20.0,
                                  child: ListTile(
                                    onTap: () {
                                      setState(() {
                                        x = Text('Tapped');
                                      });
                                    },
                                    leading: x,
                                    title: Text(myCards['CardPin']),
                                    trailing: Text(myCards['Value']),
                                  ),
                                );
                              },
                            ),
                          );`
                        },
                      ),

Thats happens because the x is the same for all cards, you need separate this guy, my suggestion is put the x inside DocumentSnapshot , so u can change only tapped card, something like this:发生这种情况是因为所有卡片的x都是相同的,您需要将这个人分开,我的建议是将x放在DocumentSnapshot中,所以您只能更改已点击的卡片,如下所示:

StreamBuilder(
    stream: Firestore.instance
        .collection('Recharge_Card')
        .snapshots(),
    //print an integer every 2secs, 10 times
    builder: (context, snapshot) {
        if (!snapshot.hasData) {
        return Text("Loading..");
        }
        return SizedBox(
        height: _height / 1.9,
        child: ListView.builder(
            // itemExtent: 80.0,
            itemCount: snapshot.data.documents.length,
            itemBuilder: (context, index) {
            DocumentSnapshot myCards =
                snapshot.data.documents[index];

            return Card(
                elevation: 20.0,
                child: ListTile(
                onTap: () {
                    setState(() {
                        myCards['x'] = Text('Tapped');
                    });
                },
                leading: myCards['x'],
                title: Text(myCards['CardPin']),
                trailing: Text(myCards['Value']),
                ),
            );
            },
        ),
        );`
    },
),

Add key to your card and provide the unique value, in your case it is your index为您的卡添加密钥并提供唯一值,在您的情况下,它是您的索引

         Card(
            key: ValueKey(index),
              //....
            )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM