Flutter - Open Drawer Programmatically
Last Updated :
24 Apr, 2025
In this article, we will see how to open the drawer Programmatically using the Scaffold.of(context).openDrawer(). A sample video is given below to get an idea about what we will do in this article.
How to Use?
Dart
Builder(
builder: (context) => // Ensure Scaffold is in context
MaterialButton(
child: Text('Open Drawer '),
onPressed: () => Scaffold.of(context).openDrawer()),
),
),
Step By Step Implementation
Step 1: Create a New Project in Android Studio
To set up Flutter Development on Android Studio please refer to Android Studio Setup for Flutter Development, and then create a new project in Android Studio please refer to Creating a Simple Application in Flutter.
Step 2: Add the Material package that gives us the important method to use and call the runApp method in the main function that will call our application.
import 'package:flutter/material.dart';
void main() {
runApp(RunMyApp());
}
Step 3: Now we have to make a Stateful widget RunMyApp Because our application does change its state and then return the MaterialApp widget which allows us the set the title and theme and has many more properties. Also, create the TextEditingController.
class RunMyApp extends StatefulWidget {
const RunMyApp({super.key});
@override
State<RunMyApp> createState() => _RunMyAppState();
}
class _RunMyAppState extends State<RunMyApp> {
final TextEditingController _controller = new TextEditingController();
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
theme: ThemeData(primarySwatch: Colors.green),
home: Scaffold(),
);
}
}
Step 4: Now in the Scaffold use the drawer Property to include the drawer in the app. And then create a Button in the center of the app. In onpressed property call the openDrawer() method with the help of the Scaffold.of().
drawer: Drawer(),
appBar: AppBar(
title: Text("Open Drawer Programmatically"),
),
body: Center(
child: Builder(
builder: (context) => // Ensure Scaffold is in context
MaterialButton(
child: Text('Open Drawer '),
onPressed: () => Scaffold.of(context).openDrawer()),
),
),
),
Complete Code
Dart
import 'package:flutter/material.dart';
void main() {
runApp(RunMyApp());
}
class RunMyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
theme: ThemeData(primarySwatch: Colors.green),
home: Scaffold(
// this to prevent the default sliding behaviour
drawerEnableOpenDragGesture: false,
drawer: Drawer(),
appBar: AppBar(
title: Text("Open Drawer Programmatically"),
),
body: Center(
child: Builder(
builder: (context) => // Ensure Scaffold is in context
MaterialButton(
child: Text('Open Drawer '),
onPressed: () => Scaffold.of(context).openDrawer()),
),
),
),
);
}
}
Output:
Code Explanation:
- The main() method is the entry point or principle method of the application, which runs the RunMyApp widget.
- The RunMyApp widget is a stateless widget that returns a MaterialApp widget.
- The debugShowCheckedModeBanner property of the MaterialApp widget is set to false, which hides the debug banner in the top right corner of the screen.
- The theme property of the MaterialApp widget is set to a ThemeData object with a primarySwatch of green, which sets the default color scheme of the application to green.
- The home property of the MaterialApp widget is set to a Scaffold widget.
- The drawerEnableOpenDragGesture property of the Scaffold widget is set to false, which prevents the default sliding behavior of the drawer when the user swipes from the left edge of the screen.
- The drawer property of the Scaffold widget is set to a Drawer widget, which is the widget that slides in from the left when the user taps the hamburger icon in the AppBar or swipes from the left edge of the screen.
- The appBar property of the Scaffold widget is set to an AppBar widget with the title of "Open Drawer Programmatically".
- The body property of the Scaffold widget is set to a Center widget with a child MaterialButton widget.
- The MaterialButton widget has a child Text widget with the label "Open Drawer", and an onPressed callback that opens the drawer programmatically using the openDrawer() method of the Scaffold widget.
Similar Reads
Flutter - Programmatically Exit From the Application
In this article, we are going to see how to programmatically close a Flutter application. SystemNavigator.pop(): Works and is the RECOMMENDED way of exiting the app. exit(0): Also works but it's NOT RECOMMENDED as it terminates the Dart VM process immediately and the user may think that the app just
2 min read
Flutter - Navigation Drawer
The mobile applications that use Material Designs have two primary options for navigation. These are namely the 'Tabs' and the 'Drawers'. This article will primarily focus on Drawers. A drawer is used to provide access to different destinations and functionalities provided in your application. It is
4 min read
Drawer Widget in Flutter
Drawer widget is used to provide access to different destinations and functionalities provided in your application. It is represented by three horizontal parallel lines on the upper end of the scaffold. It has a horizontal movement from the edge of the Scaffold that navigates the link to different r
5 min read
endDrawer Widget in Flutter
The endDrawer is the panel displayed to the side of the body (Scaffold Widget). It is generally hidden in mobile devices. We can open it by swiping in from right to left, or we can customise it to open on-press of an icon or a button. This widget is similar to the already present Drawer widget in fl
2 min read
Flutter - Grouping Navigation Drawer Menu Items
Grouping navigation drawer menu items involves organizing the items into sections or categories. This makes it easier for users to navigate through the app and find the options that the user wants. Grouping Drawer Menu Items, In Flutter, we can group navigation drawer menu items using the ExpansionT
5 min read
Flutter - KF Drawer
In many Android Applications, you may see Sidebar which has some items that perform some tasks like showing the profile UI, and displaying the About us page, In the application world it is called Drawer. Drawer is the side navigation bar that is used to access the different pages of the Application.
4 min read
MaterialApp class in Flutter
MaterialApp Class: MaterialApp is a predefined class or widget in flutter. It is likely the main or core component of a flutter app. The MaterialApp widget provides a wrapper around other Material Widgets. We can access all the other components and widgets provided by Flutter SDK like Text widget, D
7 min read
Flutter - Material Banner Widget
In this article, we will learn about a new feature called Material Banner which is a new feature in flutter and got released in flutter version 2.5.0. What is Material Banner? A banner displays an important message and asks the user to perform some action. Banner is displayed at top of the screen be
4 min read
Flutter - Implement Status Alert
In Flutter, a "status alert" typically refers to a visual feedback mechanism that informs the user about the status or outcome of a certain operation or event. The status_alert package in Flutter is a third-party package that provides an easy way to create and display such status alerts. These alert
4 min read
Step Circle Progress Bar in Flutter
A progress bar is the circular loading bar that is seen when data or something is loading from the database, API, etc., We can also use it when we are Starting our application. A sample output given below gives you an idea of what we are implementing in this article. Step By Step ImplementationStep
2 min read