Updated 2 May 2023
In this blog, We are going to learn how we can create an expandable listview in our Flutter applications. 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.
You may also check our Flutter app development services.
ExpansionTiles is used to produce two-level or multi-level lists in the app. ExpansionTiles widget is a detailed tile. It is like a ListTile widget which will expand by tapping the tile. We can display some detailed information on the list with ExpansionTiles.
Here’s the complete code ->
main.dart file
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
import 'package:expanded_listview_demo/subCategory.dart'; import 'package:flutter/material.dart'; import 'dataModel.dart'; void main() { runApp(MyApp()); } class MyApp extends StatefulWidget { @override State<MyApp> createState() => _MyAppState(); } class _MyAppState extends State<MyApp> { List<Menu> data = []; @override void initState() { dataList.forEach((element) { data.add(Menu.fromJson(element)); }); super.initState(); } @override Widget build(BuildContext context) { return MaterialApp( home: Scaffold( drawer: _drawer(data), appBar: AppBar( title: const Text('Expandable ListView'), ), body: ListView.builder( itemCount: data.length, itemBuilder: (BuildContext context, int index) => _buildList(data[index]), ), ), ); } Widget _drawer (List<Menu> data){ return Drawer( child: SafeArea( child: SingleChildScrollView( child: Column( children: [ UserAccountsDrawerHeader(margin: EdgeInsets.only(bottom: 0.0), ListView.builder( shrinkWrap: true, physics: NeverScrollableScrollPhysics(), itemCount: data.length, itemBuilder:(context, index){return _buildList(data[index]);},) ], ), ), )); } Widget _buildList(Menu list) { if (list.subMenu.isEmpty) return Builder( builder: (context) { return ListTile( onTap:() => Navigator.push(context, MaterialPageRoute(builder: (context) => SubCategory(list.name))), leading: SizedBox(), title: Text(list.name) ); } ); return ExpansionTile( leading: Icon(list.icon), title: Text( list.name, style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold), ), children: list.subMenu.map(_buildList).toList(), ); } } |
datamodel.dart file ->
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
import 'package:flutter/material.dart'; List dataList = [ { "name": "Sales", "icon": Icons.payment, "subMenu": [ {"name": "Orders"}, {"name": "Invoices"} ] }, { "name": "Marketing", "icon": Icons.volume_up, "subMenu": [ { "name": "Promotions", "subMenu": [ {"name": "Catalog Price Rule"}, {"name": "Cart Price Rules"} ] }, { "name": "Communications", "subMenu": [ {"name": "Newsletter Subscribers"} ] }, { "name": "SEO & Search", "subMenu": [ {"name": "Search Terms"}, {"name": "Search Synonyms"} ] }, { "name": "User Content", "subMenu": [ {"name": "All Reviews"}, {"name": "Pending Reviews"} ] } ] } ]; class Menu { String name; IconData icon; List<Menu> subMenu = []; Menu({this.name, this.subMenu, this.icon}); Menu.fromJson(Map<String, dynamic> json) { name = json['name']; icon = json['icon']; if (json['subMenu'] != null) { subMenu.clear(); json['subMenu'].forEach((v) { subMenu?.add(new Menu.fromJson(v)); }); } } } |
Outputs:
In this blog, we have discussed how we can create an expandable listview on our app screen. I hope it will help you out in understanding and get a brief idea about it.
If you have more details or questions, you can reply to the received confirmation email.
Back to Home
import ‘package:expanded_listview_demo/subCategory.dart’;
which is not available in the tutorial. Could You please provide it.