Flutter Navigation Drawer Layout Example Code using Scaffold and Drawer Widget.
Flutter Navigation Drawer Layout Example Code

In this tutorial you will learn how to add Navigation Drawer Layout in your Flutter App.

You will learn how to add Left Navigation Drawer ( Drawer which opens from start side or right side ) and right Navigation Drawer (Drawer layout which slides from end or left side of the screen).

We will be using following Widgets to add Drawer layout in our Flutter App:

1. Creating Scaffold Widget

Scaffold is used to implement Drawer, Snackbar,  App Bar, Bottom Sheet, Bottom Navigation Bar and Floating Action Button.

So to add Navigation Drawer in our Flutter App we are required to add Scaffold Widget first.

Than our Drawer Widget will become children of Scaffold Widget (We will add drawer inside Scaffold).

Scaffold Widget has 2 properties for Drawer layouts in Flutter.

Scaffold(
drawer: // Here we will add Drawer Layout.
);

Adding Drawer widget in Scaffold

Scaffold(
drawer: Drawer (
child: // here we will add Drawer layout items in next step.
), );

Adding List Items in Drawer

Here we will add ListView Widget as children of Drawer Widget.

We are adding ListView here so user can scroll drawer items vertically.

ListView Widget requires array of Wdgets as ListView items.

Scaffold(
drawer: Drawer(
elevation: 20.0,
child: ListView(
padding: EdgeInsets.zero,
children: <Widget>[


],
)),
)

Adding Header Layout in Drawer

At this step we are going to add first item in ListView which will be Drawer Header Layout.

For this purpose we have added UserAccountsDrawerHeader  Widget.

We can add user name, user email and user profile picture in our Drawer header.

Scaffold(
drawer: Drawer(
elevation: 20.0,
child: ListView(
padding: EdgeInsets.zero,
children: <Widget>[
UserAccountsDrawerHeader(
accountName: Text('Hammad Tariq'),
accountEmail: Text('developine.com@gmail.com'),
currentAccountPicture:
Image.network('https://hammad-tariq.com/img/profile.png'),
decoration: BoxDecoration(color: Colors.blueAccent),
),
],
)),
)

This will be output of above code:

Flutter Drawer Header layout Example using UserAccountsDrawerHeader Widget.
Flutter Drawer Header layout Example 


Adding Items in Drawer

Here we are

Scaffold(
drawer: Drawer(
elevation: 20.0,
child: ListView(
padding: EdgeInsets.zero,
children: <Widget>[
UserAccountsDrawerHeader(
accountName: Text('Hammad Tariq'),
accountEmail: Text('developine.com@gmail.com'),
currentAccountPicture:
Image.network('https://hammad-tariq.com/img/profile.png'),
decoration: BoxDecoration(color: Colors.blueAccent),
),

// Drawer layout first item.
ListTile(
leading: Icon(Icons.account_circle),
title: Text('Drawer layout Item 1'),
onTap: () {
// This line code will close drawer programatically....
Navigator.pop(context);
},
),
Divider(
height: 2.0,
),
],
)),
)

Adding Appbar (Toolbar) with Hamburger icon and Title

Flutter adding side menu icon (or hamburger icon) in flutter apps using appbar widget inside scaffold widget.
Flutter Appbar using Scaffold Widget

Simply add appBar using AppBar Widget and Flutter will make it like Toolbar and will also show hamburger icon.

If you want to center Appbar title use this property inside AppBar widget.

appBar: AppBar(
centerTitle: true,
title: Text('Developine App Bar'),
)

Hamburger icon is displayed by default because we have not provided any leading widget inside AppBar.

As our Scaffold widget has AppBar widget and our Drawer is inside AppBar so Flutter will place side menu icon (hamburger icon) automatically which will be used to open drawer.

If there is no drawer widget inside AppBar, Flutter will add back arrow here. which will be used to go back to screen or close app.

Completed Code

import 'package:flutter/material.dart';

void main() => runApp(new MyApp());

class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return new MaterialApp(
title: 'Flutter Drawer Layout Demo',
debugShowCheckedModeBanner: false,
theme: new ThemeData(
primarySwatch: Colors.blue,
),
home: new MyHomePage(title: 'Flutter Drawer Demo Home Page'),
);
}
}

class MyHomePage extends StatelessWidget {
MyHomePage({Key key, this.title}) : super(key: key);

final String title;

@override
Widget build(BuildContext context) {
return Container(
child: new Scaffold(
appBar: AppBar(
title: Text('Developine App Bar'),
)
drawer: Drawer(
elevation: 20.0,
child: ListView(
padding: EdgeInsets.zero,
children: <Widget>[
UserAccountsDrawerHeader(
accountName: Text('Hammad Tariq'),
accountEmail: Text('developine.com@gmail.com'),
currentAccountPicture:
Image.network('https://hammad-tariq.com/img/profile.png'),
decoration: BoxDecoration(color: Colors.blueAccent),
),
ListTile(
leading: Icon(Icons.account_circle),
title: Text('Drawer layout Item 1'),
onTap: () {
// This line code will close drawer programatically....
Navigator.pop(context);
},
),
Divider(
height: 2.0,
),
ListTile(
leading: Icon(Icons.accessibility),
title: Text('Drawer layout Item 2'),
onTap: () {
Navigator.pop(context);
},
),
Divider(
height: 2.0,
),
ListTile(
leading: Icon(Icons.account_box),
title: Text('Drawer layout Item 3'),
onTap: () {
Navigator.pop(context);
},
)
],
)),
));
}
}

Adding Right Drawer in Flutter

If you want to open drawer from right side of app like below:

Flutter endDrawer using Scaffold Widget Example.
Flutter open drawer from right side code example.
Flutter endDrawer using Scaffold Widget Example

Code for Right Drawer layout:

This code has slight change than above code.

Above code is using drawer property of Scaffold Widget, whereas below code will be using endDrawer property of Scaffold Widget.

import 'package:flutter/material.dart';

void main() => runApp(new MyApp());

class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return new MaterialApp(
title: 'Flutter Drawer Layout Demo',
debugShowCheckedModeBanner: false,
theme: new ThemeData(
primarySwatch: Colors.blue,
),
home: new MyHomePage(title: 'Flutter Drawer Demo Home Page'),
);
}
}

class MyHomePage extends StatelessWidget {
MyHomePage({Key key, this.title}) : super(key: key);

final String title;

@override
Widget build(BuildContext context) {
return Container(
child: new Scaffold(
appBar: AppBar(
title: Text('Developine App Bar'),
)
endDrawer: Drawer(
elevation: 20.0,
child: ListView(
padding: EdgeInsets.zero,
children: <Widget>[
UserAccountsDrawerHeader(
accountName: Text('Hammad Tariq'),
accountEmail: Text('developine.com@gmail.com'),
currentAccountPicture:
Image.network('https://hammad-tariq.com/img/profile.png'),
decoration: BoxDecoration(color: Colors.blueAccent),
),
ListTile(
leading: Icon(Icons.account_circle),
title: Text('Drawer layout Item 1'),
onTap: () {
// This line code will close drawer programatically....
Navigator.pop(context);
},
),
Divider(
height: 2.0,
),
ListTile(
leading: Icon(Icons.accessibility),
title: Text('Drawer layout Item 2'),
onTap: () {
Navigator.pop(context);
},
),
Divider(
height: 2.0,
),
ListTile(
leading: Icon(Icons.account_box),
title: Text('Drawer layout Item 3'),
onTap: () {
Navigator.pop(context);
},
)
],
)),
));
}
}

copyright : http://developine.com/

24 Responses

  1. I know this site presents quality depending articles or reviews and extra material, is there any other website which gives these kinds of information in quality?

  2. You can definitely see your skills within the work you write.

    The arena hopes for more passionate writers such as you who are
    not afraid to mention how they believe. Always go after your heart.

  3. It’s actually very complicated in this full of activity life to listen news on TV, therefore I only use the web for that reason, and get the
    most up-to-date news.

  4. If some one wishes expert view regarding blogging and site-building afterward i
    suggest him/her to pay a quick visit this webpage, Keep
    up the good job.

  5. You can definitely see your expertise in the article you write.
    The world hopes for more passionate writers such as you who
    are not afraid to say how they believe. Always go after your heart.

  6. Wonderful website you have here but I was wondering if you knew
    of any forums that cover the same topics discussed here?
    I’d really like to be a part of group where I can get responses from other knowledgeable people that share the same
    interest. If you have any recommendations, please let me know.
    Cheers!

  7. It’s actually a nice and useful piece of info.
    I am satisfied that you shared this helpful info with
    us. Please keep us informed like this. Thanks for sharing.

  8. Can I just say what a comfort to discover somebody that truly understands what they are discussing over
    the internet. You certainly understand how to bring an issue to light and
    make it important. A lot more people must look at this and understand this
    side of your story. It’s surprising you are
    not more popular given that you most certainly have the gift.

  9. Appreciating thе tіme andd effort you put
    intfⲟ your sitе and in depth information yoᥙ offer.
    It’s good to come across a blog every once in a while tһat isn’t thе same unwanted rehashed
    information. Excellent read! I’ve bookmarked your site and I’m including your RSS feeԀs
    to my Gooɡle account. https://syiedarossum.blogspot.com/

  10. Simply wish to say your article is as astounding.
    The clarity on your put up is just excellent and that i could suppose you are knowledgeable in this
    subject. Fine along with your permission allow me
    to seize your RSS feed to keep updated with imminent post.

    Thank you one million and please carry on the rewarding work.