我的问题是:
Flutter 有没有办法只允许扩展动态生成的 ListView 的一个 ExpansionTile?
例如我的 ListView 有三个 ExpansionTiles,我点击第一个,它展开了。现在,如果我点击第二个,第二个应该展开,而第一个应该自行关闭。
我想过使用这个修改过的 ExpansionTile ( https://stackoverflow.com/a/48935106/3775957 ),但想不出一个解决方案。
在我看来,它应该像将此任务放入“onExpansionChanged”方法一样工作,但我不知道如何操作。
我这样构建我的 ListView:
ListView.builder(
itemCount: myArray.length,
itemBuilder: (context, index) {
return ExpansionTile(
backgroundColor: Colors.grey[200],
key: PageStorageKey('${myArray[index].name}'),
title: Text(myArray[index].name),
children: _buildChildrenWidgets(
groupName: myArray[index].name,
childrenNames: myArray[index].anotherArray),
);
});
最佳答案
你可以使用ExpansionPanelList
像这样
ListView.builder(
itemCount: snapshot.data.length,
itemBuilder: (BuildContext context, int i) {
return Card(
child: ExpansionPanelList(
expansionCallback: (int index, bool status) {
setState(() {
_activeMeterIndex =
_activeMeterIndex == i ? null : i;
});
},
children: [
ExpansionPanel(
isExpanded: _activeMeterIndex == i,
headerBuilder:
(BuildContext context, bool isExpanded) =>
Container(
padding: const EdgeInsets.only(left: 15.0),
alignment: Alignment.centerLeft,
child: Text(
snapshot.data[i].category,
),
),
body: Column(
children: snapshot.data[i].subcategory.map((e) {
return Text(e);
}).toList(),
),
),
],
),
);
},
);
https://stackoverflow.com/questions/54534864/
相关文章:
unix - 还有其他像 VIM Tutor 这样的教程吗?
python-3.x - 使用Qtbot在菜单栏和工具栏中模拟鼠标点击
graphql - Shopify graphql 客户创建
sql - 状态 : Failure -Test failed: IO Error: An exis
python - GAE : Why does GAE perform worse than nor
c# - .Net 核心 2.2 未将 302 的状态代码更新为 401。OnRedirectToL
apache-kafka - kafka有内置的流量控制机制吗