gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 04:40:43 27 4
gpt4 key购买 nike

目标是在点击时将字幕的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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com