11. Flutter Tutorial for Beginners – Drawer Menu

0
900

There is a property of Scaffold widget called ‘drawer’. For the drawer menu in flutter app use the following code.

Scaffold(
  drawer: Drawer(
    child: ListView(
      children: [
        ListTile(
          leading: Icon(Icons.ac_unit),
          title: Text("First Item"),
          subtitle: Text('Subtitle 1'),
        ),
        ListTile(
          leading: Icon(Icons.message),
          title: Text("Second Item"),
          subtitle: Text('Subtitle 2'),
        ),
        ListTile(
          leading: Icon(Icons.event),
          title: Text("Third Item"),
          subtitle: Text('Subtitle 3'),
        ),
      ],
    ),
  ),
  appBar: AppBar(
    title: Text('First Page'),
  ),
  body: Center(
    child: Text("Hello World!"),
  ),
);

In this example we are using a ListView widget to contain the element of drawer.

You can also use UserAccountsDrawerHeader

UserAccountsDrawerHeader(
  accountName: Text("Solaiman Ahmed"),
  accountEmail: Text("[email protected]"),
  otherAccountsPictures: [
    IconButton(
      icon: Icon(Icons.ac_unit),
      onPressed: (){
        //write your code
      },
    ),
    IconButton(
      icon: Icon(Icons.print),
      onPressed: (){
        //write your code
      },
    ),
  ],
  currentAccountPicture: CircleAvatar(
    backgroundColor:
    Theme.of(context).platform == TargetPlatform.iOS
        ? Colors.blue
        : Colors.white,
    child: Text(
      "F",
      style: TextStyle(fontSize: 40.0),
    ),
  ),
),

Full Example:

import 'package:flutter/material.dart';
void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State {

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      drawer: Drawer(
        child: ListView(
          children: [
            UserAccountsDrawerHeader(
              accountName: Text("Solaiman Ahmed"),
              accountEmail: Text("[email protected]"),
              otherAccountsPictures: [
                IconButton(
                  icon: Icon(Icons.ac_unit),
                  onPressed: (){
                    //write your code
                  },
                ),
                IconButton(
                  icon: Icon(Icons.print),
                  onPressed: (){
                    //write your code
                  },
                ),
              ],
              currentAccountPicture: CircleAvatar(
                backgroundColor:
                Theme.of(context).platform == TargetPlatform.iOS
                    ? Colors.blue
                    : Colors.white,
                child: Text(
                  "F",
                  style: TextStyle(fontSize: 40.0),
                ),
              ),
            ),
            ListTile(
              leading: Icon(Icons.ac_unit),
              title: Text("First Item"),
              subtitle: Text('Subtitle 1'),
            ),
            ListTile(
              leading: Icon(Icons.message),
              title: Text("Second Item"),
              subtitle: Text('Subtitle 2'),
            ),
            ListTile(
              leading: Icon(Icons.event),
              title: Text("Third Item"),
              subtitle: Text('Subtitle 3'),
            ),
          ],
        ),
      ),
      appBar: AppBar(
        title: Text('First Page'),
      ),
      body: Center(
        child: Text("Hello World!"),
      ),
    );
  }

}

LEAVE A REPLY

Please enter your comment!
Please enter your name here