Navigation Drawer in Flutter

This Article is posted by seven.srikanth at 7/29/2018 1:49:40 PM



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(
        primarySwatch: Colors.blue,
      ),
      home: new MyHomePage(title: 'Flutter Menu Demo Home Page'),
    );
  }
}
class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);
  final String title;
  @override
  _MyHomePageState createState() => new _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    return new Scaffold(
        appBar: new AppBar(
          title: new Text(widget.title),
        ),
        body: new Center(
          child: new Column(
          
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              new Text(
                'Navigation Drawer example',
              ),
            ],
          ),
        ),
        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












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