A Side Drawer
Part of the Lists and Screens section of Coddy's Flutter journey. Lesson 25 of 33.
A Drawer is a panel that slides in from the side, for the places an app has that do not fit in tabs. Give it to the Scaffold:
Scaffold(
appBar: AppBar(title: Text(folder)),
drawer: Drawer(
child: ListView(
children: [
const DrawerHeader(child: Text('Mail')),
ListTile(
title: const Text('Sent'),
onTap: () {
setState(() {
folder = 'Sent';
});
Navigator.pop(context);
},
),
],
),
),
)With a drawer, the app bar shows a menu button with three lines at its start. Tapping it, or swiping in from the edge, opens the drawer.
The drawer stays open until something closes it. An open drawer is like a screen on top of the page, so Navigator.pop(context) closes it. Call it after a row is tapped, or the drawer covers the page you picked.
A DrawerHeader is the big area at the top, for the app's name or the signed in account.
Challenge
EasyOpen the drawer with the menu button: it has only the Inbox. Add two more rows like the Inbox row, each setting folder and closing the drawer:
Sent, withIcons.sendTrash, withIcons.delete
Try it yourself
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return const MaterialApp(
debugShowCheckedModeBanner: false,
home: HomeScreen(),
);
}
}
class HomeScreen extends StatefulWidget {
const HomeScreen({super.key});
@override
State<HomeScreen> createState() => _HomeScreenState();
}
class _HomeScreenState extends State<HomeScreen> {
String folder = 'Inbox';
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text(folder)),
body: Center(
child: Text('No messages in $folder'),
),
drawer: Drawer(
child: ListView(
children: [
const DrawerHeader(
child: Text('Mail', style: TextStyle(fontSize: 24)),
),
ListTile(
leading: const Icon(Icons.inbox),
title: const Text('Inbox'),
onTap: () {
setState(() {
folder = 'Inbox';
});
Navigator.pop(context);
},
),
],
),
),
);
}
}
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Lists and Screens
1Scrolling Lists
A List That ScrollsList TilesLeading and TrailingTap a List TileDividers Between Rows6Tabs, Drawers and Swipes
Tabs With a Navigation BarFilled Icons for the Open TabA Side DrawerSwipe to DeleteUndo a SwipePractice on your own: Online Flutter compiler