free geoip
40

Create Navigation Drawer in Flutter App

Creating a navigation drawer in a Flutter app is essential for apps that require side-menu navigation for multiple pages. Flutter,…

Creating a navigation drawer in a Flutter app is essential for apps that require side-menu navigation for multiple pages. Flutter, being a powerful UI toolkit by Google, provides a built-in Drawer widget to help developers add a customizable sidebar easily.

Create Navigation Drawer in Flutter

In this article, you’ll learn how to create a basic navigation drawer in Flutter, integrate it with multiple pages, and apply styling for a better user experience.

What is a Navigation Drawer?

A navigation drawer is a sliding panel that appears from the left edge of the screen, commonly used to show app sections like Home, Settings, Profile, etc. It’s useful for apps with complex navigation.

Step-by-Step Guide to Implement Drawer:

1. Set Up Your Flutter App

Make sure you’ve installed Flutter and set up your development environment. You can check the official installation guide here.

2. Sample Flutter Code for Navigation Drawer:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends <a href="https://aliendro.id/create-ui-with-statelesswidget-in-dart/">StatelessWidget</a> {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: '<a href="https://aliendro.id/bottomnavigationbar-tutorial-in-dart/">Flutter Navigation</a> Drawer',
home: HomeScreen(),
);
}
}
class HomeScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Navigation Drawer Example'),
),
drawer: Drawer(
child: ListView(
padding: EdgeInsets.zero,
children: <Widget>[
DrawerHeader(
decoration: BoxDecoration(color: Colors.blue),
child: Text(
'Drawer Header',
style: TextStyle(color: Colors.white, fontSize: 24),
),
),
ListTile(
leading: Icon(Icons.home),
title: Text('Home'),
onTap: () {
Navigator.pop(context);
},
),
ListTile(
leading: Icon(Icons.settings),
title: Text('Settings'),
onTap: () {
Navigator.pop(context);
},
),
],
),
),
body: Center(
child: Text('Swipe from the left to see the drawer.'),
),
);
}
}
import 'package:flutter/material.dart'; void main() { runApp(MyApp()); } class MyApp extends <a href="https://aliendro.id/create-ui-with-statelesswidget-in-dart/">StatelessWidget</a> { @override Widget build(BuildContext context) { return MaterialApp( title: '<a href="https://aliendro.id/bottomnavigationbar-tutorial-in-dart/">Flutter Navigation</a> Drawer', home: HomeScreen(), ); } } class HomeScreen extends StatelessWidget { @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text('Navigation Drawer Example'), ), drawer: Drawer( child: ListView( padding: EdgeInsets.zero, children: <Widget>[ DrawerHeader( decoration: BoxDecoration(color: Colors.blue), child: Text( 'Drawer Header', style: TextStyle(color: Colors.white, fontSize: 24), ), ), ListTile( leading: Icon(Icons.home), title: Text('Home'), onTap: () { Navigator.pop(context); }, ), ListTile( leading: Icon(Icons.settings), title: Text('Settings'), onTap: () { Navigator.pop(context); }, ), ], ), ), body: Center( child: Text('Swipe from the left to see the drawer.'), ), ); } }
import 'package:flutter/material.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Navigation Drawer',
      home: HomeScreen(),
    );
  }
}

class HomeScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Navigation Drawer Example'),
      ),
      drawer: Drawer(
        child: ListView(
          padding: EdgeInsets.zero,
          children: <Widget>[
            DrawerHeader(
              decoration: BoxDecoration(color: Colors.blue),
              child: Text(
                'Drawer Header',
                style: TextStyle(color: Colors.white, fontSize: 24),
              ),
            ),
            ListTile(
              leading: Icon(Icons.home),
              title: Text('Home'),
              onTap: () {
                Navigator.pop(context);
              },
            ),
            ListTile(
              leading: Icon(Icons.settings),
              title: Text('Settings'),
              onTap: () {
                Navigator.pop(context);
              },
            ),
          ],
        ),
      ),
      body: Center(
        child: Text('Swipe from the left to see the drawer.'),
      ),
    );
  }
}

Key Features of This Drawer:

  • Simple and clean layout
  • Integrated with Scaffold for automatic drawer handling
  • Customizable DrawerHeader and menu items

Tips for Better UX:

  • Use icons and labels clearly
  • Highlight active page
  • Add logout or settings at the bottom for better navigation hierarchy

rysasahrial

Leave a Reply

Your email address will not be published. Required fields are marked *