navigation drawer in flutter example

Flutter Navigation Drawer Example

By Robert Johnson
AD

Welcome to a comprehensive guide on implementing a navigation drawer in Flutter, a popular framework for building natively compiled applications for mobile, web, desktop, and embedded devices. flutter. Flutter's rich widget library offers a variety of navigation patterns, with the navigation drawer being a common choice for mobile apps. Let's dive into creating a sleek and functional navigation drawer for your Flutter application.

Before we start, ensure you have Flutter installed and are familiar with its basics. This guide assumes a fundamental understanding of Dart, Flutter's programming language. If you're new to Flutter, consider checking out their official tutorials for a solid foundation.

Understanding Navigation Drawer

The navigation drawer is a standard UI element in mobile apps, providing quick access to different parts of the app. In Flutter, implementing a navigation drawer involves using the 'Drawer' widget, which automatically provides the proper darker appearance and unique swipe gesture recognition.

AD
Navigation Drawer using Flutter
Navigation Drawer using Flutter
Navigation Drawer in Flutter
Navigation Drawer in Flutter
Flutter Android iOS Tutorial on Navigation drawer
Flutter Android iOS Tutorial on Navigation drawer

In this guide, we'll create a simple app with a navigation drawer, demonstrating its functionality and customization. By the end, you'll have a solid understanding of how to implement and style a navigation drawer in your Flutter projects.

Setting Up the Project and Importing Packages

Let's begin by setting up a new Flutter project and importing the necessary packages. In your terminal, run:

Flutter Navigation Drawer (Fragments)
Flutter Navigation Drawer (Fragments)
Flutter And Dart - Drawer Navigation
Flutter And Dart - Drawer Navigation
Flutter : NavigationRail Widget - New in v1.17
Flutter : NavigationRail Widget - New in v1.17
```bash flutter create navigation_drawer_example cd navigation_drawer_example ```

Then, open the project in your favorite code editor. In the 'pubspec.yaml' file, add the following dependencies at the end:

```yaml dependencies: flutter: sdk: flutter ```

The 'flutter' package contains the essential widgets and classes for your Flutter application. Save the file and proceed with setting up the app's structure.

AD

Creating the Main App Structure

Replace the contents of 'lib/main.dart' with the following code to set up the basic app structure with a 'Scaffold' widget that includes a 'Drawer' widget:

an iphone screen with food items on it and the text popular food is displayed
an iphone screen with food items on it and the text popular food is displayed
an iphone screen showing the interface for different types of buttons and numbers, including symbols
an iphone screen showing the interface for different types of buttons and numbers, including symbols
the remote control is shown with icons on it
the remote control is shown with icons on it
GitHub - mitesh77/Best-Flutter-UI-Templates: completely free for everyone. Its build-in Flutter Dart.
GitHub - mitesh77/Best-Flutter-UI-Templates: completely free for everyone. Its build-in Flutter Dart.
Flutter AppBar Navigation Guide: Back Buttons, Leading Widgets & Route Control
Flutter AppBar Navigation Guide: Back Buttons, Leading Widgets & Route Control
Flutter Widgets (GridView) The Whole Picture
Flutter Widgets (GridView) The Whole Picture
```dart import 'package:flutter/material.dart'; void main() { runApp(MyApp()); } class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( title: 'Navigation Drawer Example', home: MyHomePage(), ); } } class MyHomePage extends StatelessWidget { @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text('Home'), ), body: Center( child: Text('Welcome to the Home page!'), ), drawer: Drawer( child: ListView( children: [ ListTile( title: Text('Item 1'), onTap: () {}, ), ListTile( title: Text('Item 2'), onTap: () {}, ), ], ), ), ); } } ```

Here, we've set up a basic Flutter app with a 'Scaffold' widget containing an 'AppBar', a 'Center' widget for the body, and a 'Drawer' widget. The 'Drawer' contains two 'ListTile' widgets, representing items in our navigation drawer.

Styling and Customizing the Navigation Drawer

Now that you have a functional navigation drawer, let's make it visually appealing. We'll customize the drawer's header and add an image, change the theme, and modify the list tile appearance.

Customizing the Drawer Header and Adding an Image

To customize the drawer's header, wrap the 'ListView' with a 'Container' or 'Decoration' widget. You can also add an image and modify its appearance. Update the 'Drawer' widget as follows:

```dart drawer: Drawer( child: Column( children: [ Container( height: 200, alignment: Alignment.bottomCenter, color: Colors.blue, child: Text( 'Drawer Header', style: TextStyle(color: Colors.white, fontSize: 24), ), ), ListView( physics: NeverScrollableScrollPhysics(), shrinkWrap: true, children: [ ListTile( leading: CircleAvatar( backgroundImage: AssetImage('assets/images/avatar.png'), ), title: Text('Item 1'), onTap: () {}, ), ListTile( title: Text('Item 2'), onTap: () {}, ), ], ), ], ), ), ```

In this example, we've added a custom header container with a blue background, white text, and an aligned bottom center alignment. We've also added a 'CircleAvatar' widget with a user's avatar image, making the list tile more visually appealing.

Changing the App's Theme and List Tile Appearance

Flutter allows you to change the app's theme for the entire application or specific widgets. Let's modify the app's theme and list tile appearance. First, update the 'MyApp' widget:

```dart class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( title: 'Navigation Drawer Example', theme: ThemeData( primarySwatch: Colors.blue, highlightColor: Colors.blue.withOpacity(0.1), ), home: MyHomePage(), ); } } ```

Here, we've set the primary color to blue and changed the highlight color to a lighter shade of blue. Now, update the 'ListTile' widgets in the 'MyHomePage' widget:

```dart ListTile( leading: CircleAvatar( backgroundImage: AssetImage('assets/images/avatar.png'), ), title: Text( 'Item 1', style: TextStyle(color: Colors.blue), ), onTap: () {}, ), ListTile( title: Text( 'Item 2', style: TextStyle(color: Colors.grey), ), onTap: () {}, ), ```

In this update, we've changed the text color of each list tile. The first item's text color is set to blue, while the second item's text color is set to grey, providing contrast between the two items.

Populating the Navigation Drawer with Screens

So far, we've created a functional yet static navigation drawer. Let's populate it with actual screens for a more interactive experience. We'll create new screens, update the 'ListTile' widgets to navigate to these screens, and modify the 'onTap' callbacks.

Creating New Screens

Create a new directory named 'screens' in the 'lib' folder. Inside 'screens', create two new files: 'first_screen.dart' and 'second_screen.dart'. Add the following code to each file:

first_screen.dart: ```dart import 'package:flutter/material.dart'; class FirstScreen extends StatelessWidget { static const routeName = '/first-screen'; @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar(title: Text('First Screen')), body: Center( child: Text('Welcome to the First Screen!'), ), ); } } ``` second_screen.dart: ```dart import 'package:flutter/material.dart'; class SecondScreen extends StatelessWidget { static const routeName = '/second-screen'; @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar(title: Text('Second Screen')), body: Center( child: Text('Welcome to the Second Screen!'), ), ); } } ```

Updating the Navigation Drawer

Now, update the 'ListTile' widgets in the 'MyHomePage' widget to navigate to the new screens:

```dart ListTile( leading: CircleAvatar( backgroundImage: AssetImage('assets/images/avatar.png'), ), title: Text( 'Item 1', style: TextStyle(color: Colors.blue), ), onTap: () { Navigator.pushNamed(context, FirstScreen.routeName); }, ), ListTile( title: Text( 'Item 2', style: TextStyle(color: Colors.grey), ), onTap: () { Navigator.pushNamed(context, SecondScreen.routeName); }, ), ```

In this update, we've changed the 'onTap' callbacks to navigate to the corresponding screens using the 'Navigator.pushNamed' method.

Updating the 'runApp' Method

Finally, update the 'runApp' method in the 'main.dart' file to use 'MaterialApp' with 'routes' and 'initialRoute' properties:

```dart void main() { runApp(MyApp()); } class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( title: 'Navigation Drawer Example', theme: ThemeData( primarySwatch: Colors.blue, highlightColor: Colors.blue.withOpacity(0.1), ), initialRoute: '/', routes: { '/': (context) => MyHomePage(), FirstScreen.routeName: (context) => FirstScreen(), SecondScreen.routeName: (context) => SecondScreen(), }, ); } } ```

With this update, the app will now navigate to the 'MyHomePage' using the initial route. Additionally, it will route to the 'FirstScreen' or 'SecondScreen' when tapping on the corresponding list tiles in the navigation drawer.

Congratulations! You've successfully created a functional and customizable navigation drawer in Flutter. With this guide, you should now be able to create navigation drawers tailored to your app's needs. Happy coding!

User Enhancement With Animated Flutter Drawer
User Enhancement With Animated Flutter Drawer
Bottom Navigation Bar Widget in Flutter
Bottom Navigation Bar Widget in Flutter
an animated botton navigation in figma, with the caption's text below it
an animated botton navigation in figma, with the caption's text below it
Your Flutter Development Roadmap: Master Mobile App Creation 📱
Your Flutter Development Roadmap: Master Mobile App Creation 📱
Main Drawer
Main Drawer
Adding a Drawer Navigation feature in your React Native app - MindxMaster
Adding a Drawer Navigation feature in your React Native app - MindxMaster
Tenniswood Inspiration
Tenniswood Inspiration
🚀 𝗙𝗹𝘂𝘁𝘁𝗲𝗿 𝘃𝘀. 𝗥𝗲𝗮𝗰𝘁 𝗡𝗮𝘁𝗶𝘃𝗲 𝗡𝗮𝘃𝗶𝗴𝗮𝘁𝗶𝗼𝗻 𝗣𝗲𝗿𝗳𝗼𝗿𝗺𝗮𝗻𝗰𝗲 🚀
🚀 𝗙𝗹𝘂𝘁𝘁𝗲𝗿 𝘃𝘀. 𝗥𝗲𝗮𝗰𝘁 𝗡𝗮𝘁𝗶𝘃𝗲 𝗡𝗮𝘃𝗶𝗴𝗮𝘁𝗶𝗼𝗻 𝗣𝗲𝗿𝗳𝗼𝗿𝗺𝗮𝗻𝗰𝗲 🚀
Moving to Flutter? First App (Experiment)
Moving to Flutter? First App (Experiment)
navigation drawer in flutter example
navigation drawer in flutter example
QuickstersCode - Overview
QuickstersCode - Overview
Flutter bottom navigation tutorial
Flutter bottom navigation tutorial
Flutter Widgets & Layouts Explained Simply
Flutter Widgets & Layouts Explained Simply
Flutter Flight Booking UI | Flutter UI Tutorial | Speed Code
Flutter Flight Booking UI | Flutter UI Tutorial | Speed Code
AD