drawer property
A panel displayed to the side of the body, often hidden on mobile devices. Swipes in from either left-to-right (TextDirection.ltr) or right-to-left (TextDirection.rtl)
Typically a Drawer.
To open the drawer, use the ScaffoldState.openDrawer function.
To close the drawer, use either ScaffoldState.closeDrawer, Navigator.pop or press the escape key on the keyboard.
To disable the drawer edge swipe on mobile, set the Scaffold.drawerEnableOpenDragGesture to false. Then, use ScaffoldState.openDrawer to open the drawer and Navigator.pop to close it.
To see it in action, copy and run this code snippet on DartPad.
import 'package:material_ui/material_ui.dart';
/// Flutter code sample for [Scaffold.drawer].
void main() => runApp(const DrawerExampleApp());
class DrawerExampleApp extends StatelessWidget {
const DrawerExampleApp({super.key});
@override
Widget build(BuildContext context) {
return const MaterialApp(home: DrawerExample());
}
}
class DrawerExample extends StatefulWidget {
const DrawerExample({super.key});
@override
State<DrawerExample> createState() => _DrawerExampleState();
}
class _DrawerExampleState extends State<DrawerExample> {
final GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey<ScaffoldState>();
void _openDrawer() {
_scaffoldKey.currentState!.openDrawer();
}
void _closeDrawer() {
Navigator.of(context).pop();
}
@override
Widget build(BuildContext context) {
return Scaffold(
key: _scaffoldKey,
appBar: AppBar(title: const Text('Drawer Demo')),
body: Center(
child: ElevatedButton(
onPressed: _openDrawer,
child: const Text('Open Drawer'),
),
),
drawer: Drawer(
child: Center(
child: Column(
mainAxisAlignment: .center,
children: <Widget>[
const Text('This is the Drawer'),
ElevatedButton(
onPressed: _closeDrawer,
child: const Text('Close Drawer'),
),
],
),
),
),
// Disable opening the drawer with a swipe gesture.
drawerEnableOpenDragGesture: false,
);
}
}
Implementation
// TODO(framework): Replace the following block with a @dartpad directive
// when it's supported. https://github.com/dart-lang/dartdoc/issues/4123
/// {@macro material_ui.dartpad_guide}
///
/// {@example /example/lib/scaffold/scaffold.drawer.0.dart#body}
///
/// </callout-box>
final Widget? drawer;