flutter
  1. flutter-drawer

Flutter Drawer

A Drawer is an important navigation component in Flutter that slides smoothly from left or right and provides a persistent navigation for your app. It’s often used to provide a navigation menu to the users.

Syntax

Drawer(
  child: ListView(
    children: [
      UserAccountsDrawerHeader(
        accountName: Text("Name"),
        accountEmail: Text("email@example.com"),
      ),
      ListTile(
        title: Text("Home"),
        onTap: () {
          // navigate to home screen
        },
      ),
      ListTile(
        title: Text("Settings"),
        onTap: () {
          // navigate to settings screen
        },
      ),
    ],
  ),
);

Example

import 'package:flutter/material.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Drawer Demo',
      home: Scaffold(
        appBar: AppBar(
          title: Text('Flutter Drawer Demo'),
        ),
        drawer: Drawer(
          child: ListView(
            children: [
              UserAccountsDrawerHeader(
                accountName: Text("John Doe"),
                accountEmail: Text("johndoe@example.com"),
                currentAccountPicture: CircleAvatar(
                  child: Icon(Icons.person),
                ),
              ),
              ListTile(
                title: Text("Home"),
                onTap: () {
                  Navigator.pop(context); // close the drawer
                  // navigate to home screen
                },
              ),
              ListTile(
                title: Text("Settings"),
                onTap: () {
                  Navigator.pop(context); // close the drawer
                  // navigate to settings screen
                },
              ),
            ],
          ),
        ),
        body: Center(
          child: Text('Welcome to my app!'),
        ),
      ),
    );
  }
}

Output

The output will be a screen displayed with an app bar containing the app's name and a hamburger icon on the left. When the user taps the icon, the drawer slides out from the left side of the screen, revealing a user account header and a list of navigation items.

Explanation

The above example demonstrates a simple drawer implementation in Flutter. The Drawer widget is wrapped around a ListView widget, which contains a header (UserAccountsDrawerHeader) and a list of items (ListTile).

When an item in the drawer is tapped, the onTap function is called, which can be used to navigate to another screen or to perform some other action.

Use

Developers can use the Flutter Drawer component to:

  • Provide navigation items to the user
  • Display user information and functions
  • Provide shortcuts to frequently accessed functions or locations

Important Points

  • The Drawer widget is a key navigation component in Flutter.
  • Developers can customize the appearance and behavior of the Drawer to suit their needs.
  • Care should be taken to ensure that the Drawer is easily accessible and provides relevant information and functions to the user.

Summary

The Flutter Drawer component provides a simple and effective way to add a navigation menu to your apps. It's highly customizable and can help improve the user experience by providing quick access to frequently used functions or locations.

Published on: