Navigation Drawer in Flutter

This Article is posted by seven.srikanth at 29-07-2018 13:49:40


In this example, you can find how to create a navigation drawer using flutter.
Things like Opening new page along with parameters, adding some space to the next of drawer, adding username and email information using UserAccountsDrawerHeader constructor.
Here are the final output and main.dart code. All you need to do is create a default flutter program and copy paste the below content and run it.
Final main.dart code below.
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 Menu Demo',
theme: new ThemeData(
// This is the theme of your application.
//
// Try running your application with "flutter run". You'll see the
// application has a blue toolbar. Then, without quitting the app, try
// changing the primarySwatch below to Colors.green and then invoke
// "hot reload" (press "r" in the console where you ran "flutter run",
// or press Run > Flutter Hot Reload in IntelliJ). Notice that the
// counter didn't reset back to zero; the application is not restarted.
primarySwatch: Colors.blue,
),
home: new MyHomePage(title: 'Flutter Menu Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
// This widget is the home page of your application. It is stateful, meaning
// that it has a State object (defined below) that contains fields that affect
// how it looks.
// This class is the configuration for the state. It holds the values (in this
// case the title) provided by the parent (in this case the App widget) and
// used by the build method of the State. Fields in a Widget subclass are
// always marked "final".
final String title;
@override
_MyHomePageState createState() => new _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
// This method is rerun every time setState is called, for instance as done
// by the _incrementCounter method above.
//
// The Flutter framework has been optimized to make rerunning build methods
// fast, so that you can just rebuild anything that needs updating rather
// than having to individually change instances of widgets.
return new Scaffold(
appBar: new AppBar(
// Here we take the value from the MyHomePage object that was created by
// the App.build method, and use it to set our appbar title.
title: new Text(widget.title),
),
body: new Center(
// Center is a layout widget. It takes a single child and positions it
// in the middle of the parent.
child: new Column(
// Column is also layout widget. It takes a list of children and
// arranges them vertically. By default, it sizes itself to fit its
// children horizontally, and tries to be as tall as its parent.
//
// Invoke "debug paint" (press "p" in the console where you ran
// "flutter run", or select "Toggle Debug Paint" from the Flutter tool
// window in IntelliJ) to see the wireframe for each widget.
//
// Column has various properties to control how it sizes itself and
// how it positions its children. Here we use mainAxisAlignment to
// center the children vertically; the main axis here is the vertical
// axis because Columns are vertical (the cross axis would be
// horizontal).
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
new Text(
'Navigation Drawer example',
),
],
),
),
// drawer: new Drawer(
// child:
//
// ), // This trailing comma makes auto-formatting nicer for build methods.
drawer:
new Container(
constraints: new BoxConstraints.expand(
width: MediaQuery.of(context).size.width - 20,
),
color: Colors.white,
alignment: Alignment.center,
child:
new ListView(
padding: EdgeInsets.zero,
children: <Widget>[
new DrawerHeader(
padding: const EdgeInsets.all(16.0),
child:
new UserAccountsDrawerHeader(
accountName: new Text('Someusername', style: TextStyle(
color: Colors.black
),), accountEmail: new Text('Someemail@flutter.com', style: TextStyle(
color: Colors.black
),),
currentAccountPicture: FlutterLogo(),
decoration: new BoxDecoration(
color: Colors.amberAccent,
),),
decoration: new BoxDecoration(
color: Colors.amberAccent
)
),
new ListTile(
leading: new Icon(Icons.info),
title: new Text("Page 1"),
onTap: () {_opennewpage("page 1");}),
new ListTile(
leading: new Icon(Icons.info),
title: new Text("Page 2"),
onTap: () {_opennewpage("page 2");}),
new ListTile(
leading: new Icon(Icons.info),
title: new Text("Page 3"),
onTap: () {_opennewpage("page 3");}),
new ListTile(
leading: new Icon(Icons.close),
title: new Text("Close"),
onTap: () {
Navigator.pop(context);
}),
],
),
)
);
}
void _opennewpage(s) {
Navigator.of(context).push(
new MaterialPageRoute<void>(
builder: (BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: new Text('Success'),
),
body: new Center(
child: new Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Padding(
padding: const EdgeInsets.symmetric(horizontal: 8.0, vertical: 19.0),
child: Column(children: <Widget>[
Padding(
padding: const EdgeInsets.symmetric(horizontal: 8.0, vertical: 19.0),
child: FlutterLogo( size: 70.0,),
),
Text('This is a New Page and you click on ' + s,
textAlign: TextAlign.center,
style: new TextStyle(fontWeight: FontWeight.w300),
),
],
),
),
],
),
),
);
},
),
);
}
}
Thanks,
Srikanth

Tags: Navigation Drawer in Flutter; Opening new page along with parameters; adding some space to the next of drawer; adding username and email information using UserAccountsDrawerHeader constructor;



0 Comments
Login to comment.
Recent Comments

Be the first to Comment. You can ask a Query or Share your toughts or Just say thanks.



Recent Articles


How to deploy flutter web site on GitHub using peanut

Posted by nikhilchaudhary12155 on 13 Oct 2019 Article


AnimatedPositioned Widget Example in Flutter

Posted by seven.srikanth on 10 Oct 2019 Article


Flutter Open-Source and directory lists

Posted by nishantkotatech on 09 Oct 2019 Article


Flutter Travelling Couple Leading Page - Flutter Web Series 3

Posted by nikhilchaudhary12155 on 05 Oct 2019 Article



© 2018 - Fluttercentral | Email to me - Seven.srikanth@gmail.com