I need to have some lists collapse by their topics in my project and am wondering if I'll need to implement this from zero or rather use a component from flutter. Does this component exist?
Thanks in advance :)
A list in Flutter is created using the ListView widget. There are two types of lists: User defines all items inside the list (Roughly equivalent to a ScrollView in Android) User defines one item which is repeated across the list (Equivalent to a RecyclerView)
Expandable ListView is a type of list view that is used to show multiple types of data based on category and subcategory. Expandable list view is used to expand or collapse the view in list items In a flutter app. We can easily make our own Expandable ListView using the ExpansionTile widget.
There are broadly two types of widgets in the flutter: Stateless Widget. Stateful Widget.
The Flutter Gallery has two examples that may be relevant for your accordion-like lists.
Expansion Panel Demo & Two-level List Demo
The Expansion Panel demo is probably what you want. If so, take a look at how the demo leverages ExpansionPanel and uses a headerBuilder
and body
. You can extends this to make the header and bodies as complex as you need. The Gallery demo adds a DemoItem helper class. You can use this pattern or come up with your own design.
Here is a snippet that shows the demo using ExpansionPanelList
by passing a callback and the list of DemoItem
s:
child: new ExpansionPanelList(
expansionCallback: (int index, bool isExpanded) {
setState(() {
_demoItems[index].isExpanded = !isExpanded;
});
},
children: _demoItems.map((DemoItem<dynamic> item) {
return new ExpansionPanel(
isExpanded: item.isExpanded,
headerBuilder: item.headerBuilder,
body: item.build()
);
}).toList()
),
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With