free geoip
42

Drawer Navigation Example in Dart for Beginners

Looking to learn how to implement drawer navigation in a Flutter app using Dart? This beginner-friendly guide will walk you…

Looking to learn how to implement drawer navigation in a Flutter app using Dart? This beginner-friendly guide will walk you through building a simple drawer navigation using Android Studio. This tutorial is perfect for developers just starting with Flutter and Dart programming.

Drawer Navigation Dart

We’ll explore how to set up a Drawer, add navigation items, and route between pages. By the end, you’ll have a working sidebar navigation menu in your app.

Here’s the full working code with the necessary classes separated for clarity.

1. main.dart

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

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Drawer Navigation Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: HomePage(),
    );
  }
}

2. home_page.dart

import 'package:flutter/material.dart';
import 'profile_page.dart';
import 'settings_page.dart';

class HomePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Home"),
      ),
      drawer: Drawer(
        child: ListView(
          padding: EdgeInsets.zero,
          children: <Widget>[
            DrawerHeader(
              decoration: BoxDecoration(
                color: Colors.blue,
              ),
              child: Text(
                'Menu',
                style: TextStyle(
                  color: Colors.white,
                  fontSize: 24,
                ),
              ),
            ),
            ListTile(
              leading: Icon(Icons.home),
              title: Text('Home'),
              onTap: () {
                Navigator.pop(context);
              },
            ),
            ListTile(
              leading: Icon(Icons.person),
              title: Text('Profile'),
              onTap: () {
                Navigator.push(context, MaterialPageRoute(builder: (context) => ProfilePage()));
              },
            ),
            ListTile(
              leading: Icon(Icons.settings),
              title: Text('Settings'),
              onTap: () {
                Navigator.push(context, MaterialPageRoute(builder: (context) => SettingsPage()));
              },
            ),
          ],
        ),
      ),
      body: Center(
        child: Text("Welcome to the Home Page!"),
      ),
    );
  }
}

3. profile_page.dart

import 'package:flutter/material.dart';

class ProfilePage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Profile"),
      ),
      body: Center(
        child: Text("This is the Profile Page"),
      ),
    );
  }
}

4. settings_page.dart

import 'package:flutter/material.dart';

class SettingsPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Settings"),
      ),
      body: Center(
        child: Text("This is the Settings Page"),
      ),
    );
  }
}

With this structure, you can add more pages and customize your drawer as needed. Flutter makes it easy to create responsive and clean UIs for Android apps using the Dart language.

For further UI tips and customization ideas, visit the Flutter official documentation.

rysasahrial

Leave a Reply

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