Expandable List
Loading preview
Starting the Flutter app
ExpansionTile sections with child items
import 'package:flutter/material.dart';
const _sections = [
{'title': 'Settings', 'items': ['Account', 'Notifications', 'Privacy']},
{'title': 'Support', 'items': ['Help Center', 'Contact Us', 'FAQ']},
{'title': 'About', 'items': ['Version', 'Terms', 'Licenses']},
];
class ExpandableList extends StatelessWidget {
const ExpandableList({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Expandable List')),
body: ListView.builder(
itemCount: _sections.length,
itemBuilder: (context, index) {
final section = _sections[index];
return ExpansionTile(
title: Text(
section['title'] as String,
style: Theme.of(context).textTheme.titleMedium,
),
children: (section['items'] as List<String>)
.map((item) => ListTile(
leading: const Icon(Icons.circle, size: 8),
title: Text(item),
onTap: () => _showDemo(context, 'Open $item (demo)'),
))
.toList(),
);
},
),
);
}
}
void _showDemo(BuildContext context, String message) {
ScaffoldMessenger.of(context)
..hideCurrentSnackBar()
..showSnackBar(SnackBar(content: Text(message)));
}
Add it to your project
Copies the Dart source into your app, so you own it. Flutter SDK only, no extra packages.
First time? Run dart pub global activate fluttertemplates_dev, or read the CLI guide.
How it works
ListView.builder turns each entry of the const _sections list into an ExpansionTile whose children are ListTiles with a small dot icon. Expansion is handled entirely by ExpansionTile, so the page needs no state of its own. Item onTap handlers show a demo SnackBar.
Adapting it
- Define a section class to replace the map, which removes the
section['items'] as List<String>cast. - Add a route or callback per item and call it from
onTap. - Set
initiallyExpandedon theExpansionTileif one section should open by default.