Flutter - Collapsing ExpansionTile after choosing an item

后端 未结 6 1400
傲寒
傲寒 2020-11-27 20:04

I\'m trying to get ExpansionTile to collapse after I choose an item, but it does not close the list that was opened.

I tried to use the onExpansio

6条回答
  •  粉色の甜心
    2020-11-27 21:04

    I found a simple solution. Just add a global key to the ExpansionTile widget and it will work as intended.

    ExpansionTile(
      key: GlobalKey(),
      title: Text(title),
      children: listTiles,
      ...
    )
    

    Not sure why this works, but my guess is that adding a key somehow forces the ExpansionTile widget to rebuild every time title is changed in setState, therefore return to its initial (collapsed) state.

提交回复
热议问题