Last active
March 25, 2021 01:02
-
-
Save andreidiaconu/581c99f36c22b08a9018232d19104134 to your computer and use it in GitHub Desktop.
Code demonstrating how you can keep your ExpansionTile widgets expanded when you set a new state and replace the data. The key is to generate the keys that ExpansionTile uses from attributes of your data that are unique. Code to focus on is line 105. Original question was asked here https://stackoverflow.com/questions/50087222/prevent-expansiont…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
class ExpansionTileSample extends StatefulWidget { | |
@override | |
ExpansionTileSampleState createState() { | |
return new ExpansionTileSampleState(); | |
} | |
} | |
class ExpansionTileSampleState extends State<ExpansionTileSample> { | |
List<Entry> changingData; | |
@override | |
void initState() { | |
super.initState(); | |
this.changingData = generateData(); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return new MaterialApp( | |
home: new Scaffold( | |
appBar: new AppBar( | |
title: const Text('ExpansionTile'), | |
), | |
body: new ListView.builder( | |
itemBuilder: (BuildContext context, int index) => | |
new EntryItem(changingData[index]), | |
itemCount: changingData.length, | |
), | |
floatingActionButton: new FloatingActionButton( | |
onPressed: (){ | |
setState(() { | |
this.changingData = generateData(); //exact same data | |
}); | |
}, | |
child: new Icon(Icons.refresh), | |
), | |
), | |
); | |
} | |
} | |
// One entry in the multilevel list displayed by this app. | |
class Entry { | |
Entry(this.title, [this.children = const <Entry>[]]); | |
final String title; | |
final List<Entry> children; | |
} | |
// The entire multilevel list displayed by this app. | |
generateData() => <Entry>[ | |
new Entry( | |
'Chapter A', | |
<Entry>[ | |
new Entry( | |
'Section A0', | |
<Entry>[ | |
new Entry('Item A0.1'), | |
new Entry('Item A0.2'), | |
new Entry('Item A0.3'), | |
], | |
), | |
new Entry('Section A1'), | |
new Entry('Section A2'), | |
], | |
), | |
new Entry( | |
'Chapter B', | |
<Entry>[ | |
new Entry('Section B0'), | |
new Entry('Section B1'), | |
], | |
), | |
new Entry( | |
'Chapter C', | |
<Entry>[ | |
new Entry('Section C0'), | |
new Entry('Section C1'), | |
new Entry( | |
'Section C2', | |
<Entry>[ | |
new Entry('Item C2.0'), | |
new Entry('Item C2.1'), | |
new Entry('Item C2.2'), | |
new Entry('Item C2.3'), | |
], | |
), | |
], | |
), | |
]; | |
// Displays one Entry. If the entry has children then it's displayed | |
// with an ExpansionTile. | |
class EntryItem extends StatelessWidget { | |
const EntryItem(this.entry); | |
final Entry entry; | |
Widget _buildTiles(Entry root) { | |
if (root.children.isEmpty) return new ListTile(title: new Text(root.title)); | |
return new ExpansionTile( | |
key: new PageStorageKey<String>(root.title), // root.title == newerRoot.title is true | |
title: new Text(root.title), | |
children: root.children.map(_buildTiles).toList(), | |
); | |
} | |
@override | |
Widget build(BuildContext context) { | |
return _buildTiles(entry); | |
} | |
} | |
void main() { | |
runApp(new ExpansionTileSample()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment