Skip to content
Readerstacks logo Readerstacks
  • Home
  • Softwares
  • Angular
  • Php
  • Laravel
  • Flutter
Readerstacks logo
Readerstacks
Create Drawer in flutter

How to create drawer in flutter with example ?

Aman Jain, January 3, 2022January 15, 2022

Flutter itself provides various widgets to create the industry level standard layout. One of the feature of flutter is creating default drawer in flutter with Scaffold. Using the drawer we can show our application menus list and hide show in sidebar.

In this s tutorial i will create a simple Drawer widget using Scaffold and will add some menus to it.

Let’s start the tutorial of drawer in flutter

Step 1 : Create flutter project

Very first step is to create the fluter application using command line tool or in Android studio.

flutter create example_app

This will create a new project with name my_app then go in to the folder .

Step 2 : Create a widget for drawer

Although we can create drawer in the same widget without creating a separate widget for it but i recommend to create a separate widget since its going to use in every other page of our application so let’s create a widget.

File : lib/drawer.dart

import 'package:flutter/material.dart';

class DrawerWidget extends StatelessWidget {
  final BuildContext context;

  const DrawerWidget({Key? key, required this.context}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Drawer(
      // Add a ListView to the drawer. This ensures the user can scroll
      // through the options in the drawer if there isn't enough vertical
      // space to fit everything.
      child: ListView(
        // Important: Remove any padding from the ListView.
        padding: EdgeInsets.zero,
        children: [
          const DrawerHeader(
            decoration: BoxDecoration(
              color: Colors.blue,
            ),
            child: Text('Drawer Header'),
          ),
          ListTile(
            title: const Text('Home'),
            onTap: () {
              // Update the state of the app
              // ...
              // Then close the drawer
              Navigator.pop(context);
            },
          ),
          ListTile(
            title: const Text('Page 2'),
            onTap: () {
              // Update the state of the app
              // ...
              // Then close the drawer
              Navigator.pop(context);
            },
          ),
        ],
      ),
    );
  }
}

Here we added Drawer, DrawerHeader and ListTile widget to design the layout.

Step 3 : Add Drawer to main scaffold

Now add our new widget to scaffold thus in our main.dart file change as below

File : lib/main.dart

import 'package:example_app/drawer.dart';
import 'package:flutter/material.dart';

void main() {
  runApp( MyApp());
}

class MyApp extends StatelessWidget {

  final GlobalKey<ScaffoldState> _scaffoldKey =  GlobalKey<ScaffoldState>();

   MyApp({Key? key}) : super(key: key);

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(

      title: 'Flutter Demo',
      theme: ThemeData(

        primarySwatch: Colors.blue,
      ),
      home: Scaffold(
        key: _scaffoldKey,

        drawer: DrawerWidget(context:context),
          appBar: AppBar(title: const Text("Readerstack")),
          body:  Center(
            child: Row(
              children: [
                Text('This is drawer example'),
                TextButton(onPressed: (){
                  _scaffoldKey.currentState!.openDrawer();
                }, child: Text("Open Drawer"))
              ],
            ),
          ),

        )

    );
  }
}

Here we used Scaffold drawer

Scaffold(
  drawer: // Add a Drawer layout here.
);

and Drawer Material widget

Scaffold(
  drawer: Drawer(
    child: // add the layout for sidebar
  ),
);

How to open close drawer from button?

To open the drawer from any button as you can see we have used scaffold GlobalKey and then we used it in TextButton in home page to open the drawer programmatically.

 TextButton(onPressed: (){
                  _scaffoldKey.currentState!.openDrawer();
                }, child: Text("Open Drawer"))

Step 4: Run the project

Simply run the project using command line or in android studio to check the implementation.

flutter run lib/main.dart

Screenshot :

  • Screenshot 2022 01 03 at 8.14.32 AM
  • Screenshot 2022 01 03 at 8.19.53 AM 1

Related

Dart Flutter drawerFluttersidebar

Post navigation

Previous post
Next post

Related Posts

Dart How to Subscribe for Flutter NavigatorRouter Events

How to Subscribe for Flutter Navigator/Router Events ?

April 29, 2022September 11, 2022

Navigator is used to navigate between the pages and sometimes we want to subscribe for flutter Navigator/Router Events to update the widget state or to perform the specific task during the push and pop of page. Flutter provider RouteObserver mixin to subscribe the events of navigator by which we can…

Read More
Flutter Create a top tab in flutter

How to create top tab bar in flutter ?

January 9, 2022January 15, 2022

In this tutorial i will show you to create top tab in flutter. Top tab bar is useful when we wanted to switch on different content easily by sliding the tabs like whatsapp in android. I will show you in this article to change the color of tab , icon,…

Read More
Dart Create a slider in flutter

How to create custom slider in flutter ?

January 8, 2022January 15, 2022

In this tutorial i will show you to create you simple slider in flutter. Flutter has many inbuilt widgets to create beautiful UI and manage them with state and stateless widgets. There is many packages to create the sliders in flutter application but today here i am going to share…

Read More

Aman Jain
Aman Jain

With years of hands-on experience in the realm of web and mobile development, they have honed their skills in various technologies, including Laravel, PHP CodeIgniter, mobile app development, web app development, Flutter, React, JavaScript, Angular, Devops and so much more. Their proficiency extends to building robust REST APIs, AWS Code scaling, and optimization, ensuring that your applications run seamlessly on the cloud.

Categories

  • Angular
  • CSS
  • Dart
  • Devops
  • Flutter
  • HTML
  • Javascript
  • jQuery
  • Laravel
  • Laravel 10
  • Laravel 11
  • Laravel 9
  • Mysql
  • Php
  • Softwares
  • Ubuntu
  • Uncategorized

Archives

  • June 2025
  • May 2025
  • April 2025
  • October 2024
  • July 2024
  • February 2024
  • January 2024
  • December 2023
  • November 2023
  • October 2023
  • July 2023
  • March 2023
  • November 2022
  • October 2022
  • September 2022
  • August 2022
  • July 2022
  • June 2022
  • May 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021

Recent Posts

  • The Resilience of Nature: How Forests Recover After Fires
  • Understanding Laravel Cookie Consent for GDPR Compliance
  • Understanding High Vulnerabilities: A Critical Overview of the Week of May 12, 2025
  • Installing a LAMP Stack on Ubuntu: A Comprehensive Guide
  • Understanding High Vulnerabilities: A Deep Dive into Recent Security Concerns
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version