gpt4 book ai didi

flutter - 如何在回调中获取实例

转载 作者:IT王子 更新时间:2023-10-29 06:34:20 26 4
gpt4 key购买 nike

我正在尝试切换抽屉中 ListTile 列表中的选定项?

    ListTile(
title: Text("Name"),
leading: Icon(Icons.dashboard),
onTap: () {
currentSelected.selected = false
this.selected = true;
currentSelected = this; // << How to get the instance of ListTile
},
),

最佳答案

this 指向包含问题代码的小部件。您可以在分配 ListTile 的地方创建一个变量,然后您可以在 onTap 中引用它。

ListTile listTile;
listTile = ListTile(
title: Text("Name"),
leading: Icon(Icons.dashboard),
onTap: () {
currentSelected.selected = false
this.selected = true;
currentSelected = listTile
},
),
return listTile;

最好使用一个值来存储所选项目,例如 itemId,而不是小部件引用。

关于flutter - 如何在回调中获取实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54867805/

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