gpt4 book ai didi

flutter - 如何从它自己的 onTap 中禁用 ListTile?

转载 作者:IT王子 更新时间:2023-10-29 06:43:21 24 4
gpt4 key购买 nike

我正在做 flutter baby names codelab并正在执行与 firestore 的交易以补救竞争条件。如果我向一个名字发送垃圾邮件,它将导致错过选票和 IllegalStateException。

我想在事务完成时从 onTap 中禁用 ListTile,然后在事务更新后重新启用它。

我尝试从交易中设置状态,但没有成功。代码如下。


child: ListTile(
title: Text(record.name),
trailing: Text(record.votes.toString()),
onTap: () => Firestore.instance.runTransaction((transaction) async {
final freshSnapshot = await transaction.get(record.reference);
final fresh = Record.fromSnapshot(freshSnapshot);
await transaction.update(record.reference, {'votes': fresh.votes + 1});
///DOES NOT WORK
setState(() {
enabled: false
});
///
}),

我尝试了这里的其中一个建议,但它也不起作用。似乎 _isEnableTile bool 值的状态被重置,即使我从未将其设置回 true。一种可行的方法是将 _isEnableTile 设置为字段(即在类级别),不幸的是,这会导致通过“启用”参数禁用所有列表 block 。




Widget _buildListItem(BuildContext context, DocumentSnapshot data) {
bool _isEnableTile = true;
final record = Record.fromSnapshot(data);
return Padding(
key: ValueKey(record.name),
padding: const EdgeInsets.symmetric(horizontal: 16.0, vertical: 8.0),
child: Container(
decoration: BoxDecoration(
border: Border.all(color: Colors.grey),
borderRadius: BorderRadius.circular(5.0),
),
child: ListTile(
title: Text(record.name),
trailing: Text(record.votes.toString()),
onTap: () async {
print(_isEnableTile);
if (_isEnableTile) {
_isEnableTile = false;
print('doing it');
await Firestore.instance.runTransaction((transaction) async {
final freshSnapshot = await transaction.get(record.reference);
final fresh = Record.fromSnapshot(freshSnapshot);
await transaction
.update(record.reference, {'votes': fresh.votes + 1});
});
} else {
print('hmmm not doing it');
}
},
),
),
);
}

上述代码的预期行为是能够点击一次,然后再也不能点击,因为 _isEnableTile 永远不会切换回 true。不是这种情况。 _isEnableTile 不断重置为 true(很可能在 onTap 完成后立即重置)。

编辑:不幸的是,您不能使用 enabled 来切换启用状态,因为出于某种原因它在 ListTile 中是最终的。

最佳答案

像拿一面旗子一样

bool isEnableTile = true;

并设置为

onTap: (isEnableTile == true )? (Code+StateToChange "isEnableTile" to *false*) : null

关于flutter - 如何从它自己的 onTap 中禁用 ListTile?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54127441/

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