以编程方式展开/折叠ListView中的所有ExpansionTiles

时间:2019-02-28 07:52:03

标签: listview dart flutter

我有一个ListView,其中包含许多以树状结构嵌套的ExpansionTiles。我想在AppBar中添加一个按钮,使用户可以一次展开/折叠所有ExpansionTiles。

仅在模型中的bool expanded的每个项目上设置变量List并将其用于initiallyExpanded是行不通的-这些项目将保持不变,直到我滚动列表为止,此时它们将更改为正确的展开状态。

我还尝试重新创建(使用List.from()..add(null)..removeLast()的整个列表),以诱骗ListView认为列表已更改。 ,但也不起作用。我什至尝试过:

// Backup our list, empty it and notify listeners
List tmp = List.from(model.inventoryTree);
model.inventoryTree = [];
model.callNotifyListeners();

// Recreate the original list and notify listeners
model.inventoryTree = List.from(tmp);
model.callNotifyListeners();

但是那也不起作用。所以我终于转向“蛮力”,在清空列表和恢复列表之间增加了一个延迟:

// Backup our list, empty it and notify listeners
List tmp = List.from(model.inventoryTree);
model.inventoryTree = [];
model.callNotifyListeners();

// Wait a while, recreate the list and notify listeners
Future.delayed(Duration(milliseconds: 500), (){
    model.inventoryTree = List.from(tmp);
    model.callNotifyListeners();
});

最后它可以工作,但是延迟当然会使它看起来对用户来说很粗略。有什么更好的方法可以做这种事情吗?我尝试使用PageStorageKey中的ExpansionTile,但是无法正常工作。

0 个答案:

没有答案
相关问题