Flutter
  • Flutter Tutorials Handbook
  • 01 Flutter: Hello World
    • Related Article
  • 02 Flutter: Stateless Widgets
  • 03 Flutter: Buttons and Stateful widgets
  • 04 Flutter: Basic Navigation & Routes
    • Simple routes using StatelessWidget
    • Routes using StatefulWidget
  • 05 Flutter: Using onChanged to show input text
  • 06 Flutter: Using onSubmitted to show input text after submit
  • 07 Flutter: Adding-Deleting text in TextField
  • 08 Flutter: Tab Navigation
  • 09 Flutter: HTTP requests and Rest API
  • 10 Flutter: ListView with JSON or List Data
  • 11 Flutter: Sliding menu using a Drawer
    • Simple Drawer
    • Drawer using variables
  • 12 Flutter: Animations
  • 13 Flutter: JSON Storage
  • 14 Flutter: Friendly Chat App
  • 15 Flutter: Changing icon color onfocus
  • 16 Flutter: Horizontal ListView and Tabs
  • 17 Flutter: RaisedButton
  • 18 Flutter: RaisedButton with parameters
  • 19 Flutter: FlatButton
  • 20 Flutter: IconButton
  • 21 Flutter: Updating data in TextField
  • 22 Flutter: Checkbox
  • 23 Flutter: Radio with Functions
  • 24 Flutter: Switch
  • 25 Flutter: Slider
  • 26 Flutter: DatePicker
  • 27 Flutter: appBar
  • 28 Flutter: Floating Action Button
  • 29 Flutter: Drawer which shows Toggle Menu
  • 30 Flutter: Footer Buttons
  • 31 Flutter: BottomNavigationBar
  • 32 Flutter: Using BottomSheets
  • 33 Flutter: Using SnackBar
  • 34 Flutter: AlertDialog
  • 35 Flutter: SimpleDialog
  • 36 Flutter: Rows and Columns
  • 37 Flutter: Using Cards
  • 38 Flutter: Using Expanded
  • 39 Flutter: ListviewBuilder using Dart maps
  • 40 Flutter: ListviewBuilder using Dart lists
  • 41 Flutter: Using Custom Widgets
  • 42 Flutter: Using Image Assets
  • 43 Flutter: ListviewBuilder with Refresh Indicator
Powered by GitBook
On this page
  1. 11 Flutter: Sliding menu using a Drawer

Simple Drawer

main.dart
import 'package:flutter/material.dart';

import './pages/home_page.dart';

void main() {
  runApp(new MaterialApp(
    home: new HomePage()
  ));
}

We've created pages directory in our Flutter app to store our dart pages

home_page.dart
import 'package:flutter/material.dart';

import './other_page.dart';

class HomePage extends StatefulWidget {
  @override
  _HomePageState createState() => new _HomePageState();
}

class _HomePageState extends State<HomePage>{

  String mainProfilePicture = "https://randomuser.me/api/portraits/women/44.jpg";
  String otherProfilePicture = "https://randomuser.me/api/portraits/women/47.jpg";

  void switchUser(){
    String backupString = mainProfilePicture;
    this.setState((){
      mainProfilePicture = otherProfilePicture;
      otherProfilePicture = backupString;
    });
  }

  @override
  Widget build(BuildContext context){
    return new Scaffold(
      appBar: new AppBar(title: new Text("My Drawer App"),
      backgroundColor: Colors.redAccent,
      ),
      drawer: new Drawer(
        child: new ListView(
          children: <Widget>[
            new UserAccountsDrawerHeader(
              accountName: new Text("Raunak Hajela"),
              accountEmail: new Text("raunakhajela@gmail.com"),
              currentAccountPicture: new GestureDetector(
                onTap: () => switchUser(),
                child: new CircleAvatar(
                  backgroundImage: new NetworkImage(mainProfilePicture)
                ),
              ),
              otherAccountsPictures: <Widget>[
                new GestureDetector(
                  onTap: () => print("this is the other user"),
                  child: new CircleAvatar(
                    backgroundImage: new NetworkImage(otherProfilePicture)
                  ),
                ),
              ],
              decoration: new BoxDecoration(
                image: new DecorationImage(
                  fit: BoxFit.fill,
                  image: new NetworkImage("https://orig00.deviantart.net/20eb/f/2015/030/6/f/_minflat__dark_material_design_wallpaper__4k__by_dakoder-d8fjqzu.jpg")
                )
              )
            ),
            new ListTile(
              title: new Text("First Page"),
              trailing: new Icon(Icons.arrow_upward),
              onTap: () {
                Navigator.of(context).pop();
                Navigator.of(context).push(new MaterialPageRoute(builder: (BuildContext context) => new OtherPage("First Page")));
              }
            ),
            new ListTile(
              title: new Text("Second Page"),
              trailing: new Icon(Icons.arrow_right),
              onTap: () {
                Navigator.of(context).pop();
                Navigator.of(context).push(new MaterialPageRoute(builder: (BuildContext context) => new OtherPage("Second Page")));
              }
            ),
            new Divider(),
            new ListTile(
              title: new Text("Close"),
              trailing: new Icon(Icons.cancel),
              onTap: () => Navigator.of(context).pop(),
            )
          ],
        ),
      ),
      body: new Center(
        child: new Text("HomePage", style: new TextStyle(fontSize: 35.0))
      )
    );
  }
}

/**********************************************
***************Important Notes*****************
**********************************************/

//onTap: () => print("this is the other user")
// use this to display log messages on tap
other_page.dart
import 'package:flutter/material.dart';

class OtherPage extends StatelessWidget{

  final String pageText;

  OtherPage(this.pageText);

  @override
  Widget build(BuildContext context){
    return new Scaffold(
      appBar: new AppBar(title: new Text(pageText)),
      body: new Center(
        child: new Text(pageText),
      ),
    );
  }
}
Previous11 Flutter: Sliding menu using a DrawerNextDrawer using variables

Last updated 6 years ago