flutter - 在不可用的情况下更新Flutter ListTile

标签 flutter listview dart boolean dart-pub

目标是在点击时将字幕的ListTile中的值更改为。点击会切换 _act bool(boolean) 值,但ListTile的前导和副标题值不会从其初始状态更改。我的ListView代码如下。请帮忙。

        return new ListView(
          children: querySnapShot.data.documents.map((DocumentSnapshot document) {
            bool _act = false;
            return new ListTile(
              leading: _act != false ? const Icon(Icons.remove_red_eye, color: Color(0xff00caff), size: 40) : null,
              title: new Text((DBProvider.db.idDeHasher(document['exampleid'], key) ?? 'fallback'), style: TextStyle(color: secondaryColor)),
              subtitle: _act != false ? new Text((document['exampleString']  ?? 'fallbackString'), style: TextStyle(color: secondaryColor)) : null,
              onTap: () { 
                _act = !_act;
                print(_act.toString());
                }
            );
          }).toList(),
        );

最佳答案

您需要将小部件设置为Stateful Widget并在setState上点击时调用ListTile
根据docs:

Calling setState notifies the framework that the internal state of this object has changed in a way that might impact the user interface in this subtree, which causes the framework to schedule a build for this State object.


因此,如果窗口小部件的状态更改为,则您有来调用setState以触发 View 重建,并立即查看新状态所隐含的更改。
我以您的代码为例添加了一个演示:
     // define a list of acts
        List _acts = [];
         return new ListView(
          children: querySnapShot.data.documents.asMap().entries.map((entry) {
          // declare all entries of the list to false
          acts.add(false);
          // access the index 
          int index = entry.key;
          // access the document
          DocumentSnapshot document = entry.value;
          // DocumentSnapshot document = entry
            return new ListTile(
              leading: _acts[index] != false ? const Icon(Icons.remove_red_eye, color: Color(0xff00caff), size: 40) : null,
              title: new Text((DBProvider.db.idDeHasher(entry.val['exampleid'], key) ?? 'fallback'), style: TextStyle(color: secondaryColor)),
              subtitle: _acts[index] != false ? new Text((document['exampleString']  ?? 'fallbackString'), style: TextStyle(color: secondaryColor)) : null,
              onTap: () { 
               // call setstate
              setState(() { // new line
              // change the bool variable based on the index
               _acts[index] = !_acts[index];
                print(_acts[index].toString());
              });
                }
            );
          }).toList(),
        );

关于flutter - 在不可用的情况下更新Flutter ListTile,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63307507/

相关文章:

android - 如何初始化选择每个选项的多项选择警报对话框?

firebase - Flutter:如何在字段中显示用户列表?

button - 为新的行 flutter 创建按钮

flutter - 内部存储数据波动

wpf - 使用 System.Linq.IGrouping<string,Class T> 对 WPF ListView 进行排序

c# - 为选定的 ListView 项目禁用蓝色边框

flutter - 在 Flutter 中从本地存储播放视频列表(内存不足错误)

class - 如何从Flutter中的另一个类访问一个类的变量

android - Flutter“无法确定 bundle 的Java版本”适用于Android Studio version3.6

flutter - Flutter/Dart Scope问题-是否需要返回信息以防止重复文件上传?