Education
Software Development Executive - II
Last updated on Oct 25, 2023
Last updated on Oct 5, 2023
Flutter, a remarkable mobile operating system, boosts developers to create compelling and visually rich applications. An essential component enhancing the visual richness of Flutter apps is the ExpansionPanel. Inbuilt but often overlooked, ExpansionPanel can provide an interactive feature to your Flutter apps. We'll explore the use and importance of ExpansionPanel through this article, the basic structure of its implementation, and how it's better above the ExpansionTile. Let's dive right in!
In the sea of Flutter widgets, the ExpansionPanel is a distinctive one. It helps construct a list of expansion panels, which are essentially single-purpose containers with an expand/collapse feature. Design-wise, the expansion panel consists of a header (panel header), an expansion indicator, and a body. Users can tap the header or the indicator to expand/collapse the body section.
The Expansion Panel's speciality lies in how it provides an excellent option to present multiple chunks of data, which you can hide in their collapsed state. To illustrate, let's consider a restaurant review app. By implementing the ExpansionPanel, each restaurant's detailed review can neatly be tucked away, keeping the primary data visually clean for users.
Now, enough said, let's get into the 'how-to' part.
Before getting started with the ExpansionPanel widget, let's define the initial setup needed for any Flutter project. We'll start by defining the void main function and define the MyApp class.
1 void main() { 2 runApp(MyApp()); 3 } 4 5 class MyApp extends StatelessWidget { 6 // This widget is the root of your application. 7 8 Widget build(BuildContext context) { 9 return MaterialApp( 10 title: 'Flutter Expansion Panel Demo', 11 home: Scaffold( 12 appBar: AppBar( 13 title: Text('Flutter Expansion Panel Demo'), 14 ), 15 body: SingleChildScrollView(child: ExpansionPanelDemo()), 16 ), 17 ); 18 } 19 } 20
In this MyApp class, the body creates instances of a new widget ExpansionPanelDemo that contains our ExpansionPanel. Now, we'll move on to define the ExpansionPanelDemo widget using the ExpansionPanelList class.
1 class ExpansionPanelDemo extends StatefulWidget { 2 ExpansionPanelDemo({Key key}) : super(key: key); 3 4 5 _ExpansionPanelDemoState createState() => _ExpansionPanelDemoState(); 6 } 7 8 item(int index) { 9 return ExpansionPanel( 10 headerBuilder: (BuildContext context, bool isExpanded) => 11 ListTile( 12 leading: Icon(Icons.ac_unit), 13 title: Text('Item ${index+1}'), 14 ), 15 body: ListTile( 16 title: Text('Body of Item ${index+1}'), 17 ), 18 ); 19 } 20 21 class _ExpansionPanelDemoState extends State<ExpansionPanelDemo> { 22 List<Item> _data = generateItems(5); 23 24 25 Widget build(BuildContext context) { 26 return ListView( 27 children: _data.map<ExpansionPanel>((Item item) { 28 return item(index); 29 }).toList(), 30 ); 31 } 32 } 33
Here, the headerBuilder is expected to return a widget which acts as the header when the panel is not expanded. In the code snippet, I have used a ListTile as the header. Then, the body returns the data you wish to display when the panel is expanded. We have provided a simple ListTile description for each item for this example
Since you've now grasped the basic idea of implementing an ExpansionPanel in your Flutter application, it's about time to learn why you should prefer ExpansionPanel. The key advantage of using the ExpansionPanel is its superior data handling capabilities. Thanks to this widget, developers can manage large amounts of data and present it to users neatly and compactly. It offers an excellent user experience by allowing users to focus on the main data while retaining the choice of accessing details when required. Furthermore, the isExpanded parameter that every ExpansionPanel holds tags along with other customization options that enrich its usability. And trust me, exotic customization isn't something you often come across in prebuilt widgets!
Another vital widget you can't miss in the context of expansion in Flutter apps is the ExpansionTile. Simply put, it is a single-purpose widget that provides a straightforward way of displaying single-level datasets. But, how does it differ from ExpansionPanel? To comprehend this, we need to consider the nuances that these widgets possess. Should we explore those in the next segment? Of course, we should!
While the purpose of both ExpansionTile and ExpansionPanel seems similar at a glance - that is, providing data in an expandable and collapsable manner, their use cases show significant differences. The ExpansionTile, by default, is handier when dealing with single-level data lists. It's pretty useful when there's no requirement to nest various widgets or handle complex data structures inside them.
On the other hand, the richness of ExpansionPanel comes alive when dealing with multi-level, complex, and custom datasets due to its higher flexibility and customization options. The ExpansionPanel doesn't limit its use case to lists. Whether it's a form that should unfold more data fields or a nested list structure, the ExpansionPanel can take it all.
1 ExpansionPanelList( 2 expansionCallback: (int index, bool isExpanded) { 3 setState(() { 4 _data[index].isExpanded = !isExpanded; 5 }); 6 }, 7 children: _data.map<ExpansionPanel>((Item item) { 8 return ExpansionPanel( 9 headerBuilder: (BuildContext context, bool isExpanded) { 10 return ListTile( 11 leading: Icon(Icons.ac_unit), 12 title: Text('Item ${index+1}'), 13 ); 14 }, 15 body: ListTile( 16 title: Text('Body of Item ${index+1}'), 17 ), 18 isExpanded: item.isExpanded, 19 ); 20 }).toList(), 21 ); 22
Note that when the user presses the ExpansionPanel, the expansionCallback function is invoked with the corresponding panel index.
Through this blog, we aimed to give you an understanding of what the ExpansionPanel widget is, how to implement it in your Flutter apps, and view its advantages first-hand. Furthermore, I did a comparison study with its sibling - ExpansionTile that distinguishes these two. I hope this article served its purpose of making you familiar with complex widgets.
For further reading on Flutter, you can refer to the official Flutter documentation and our previous article Flutter ExpansionTile.
Tired of manually designing screens, coding on weekends, and technical debt? Let DhiWise handle it for you!
You can build an e-commerce store, healthcare app, portfolio, blogging website, social media or admin panel right away. Use our library of 40+ pre-built free templates to create your first application using DhiWise.