在 flutter 中展开时更改 ExpansionTile 的标题

问题描述 投票:0回答:1

我想在flutter中更改ExpansionTile标题,因此当tile展开时,标题为“less”,当tile未展开时,标题为“more”...

我已经尝试使用

ExpansionTileController.isExpanded? "less": "more"
,但出现错误:
Failed assertion: line 49 pos 12: '_state != null': is not true.

flutter dart expansion
1个回答
0
投票

在构建小部件上方创建一个 bool var

bool _customTileExpanded = true;

将条件添加到标题文本并使用

onExpansionChanged
属性来更改状态。

ExpansionTile(
  title: Text(_customTileExpanded ? 'ExpansionTile more' : 'less'),
  subtitle: const Text('Custom expansion arrow icon'),
  children: const <Widget>[
    ListTile(title: Text('This is tile number 2')),
  ],
  onExpansionChanged: (bool expanded) {
    setState(() {
      _customTileExpanded = expanded;
    });
  },
),
© www.soinside.com 2019 - 2024. All rights reserved.