Navigation Setup

Tutorial 3 of 4

Navigation Setup in Flutter: A Detailed Guide

1. Introduction

In this tutorial, our main goal is to understand how navigation works in Flutter. You will learn how to set up navigation and navigate between different screens in a Flutter application.

By the end of this tutorial, you will have a solid understanding of:
- What navigation is in the context of Flutter
- How to set up navigation in a Flutter app
- How to navigate between screens

Prerequisites:

  • Basic understanding of Dart and Flutter
  • Flutter SDK installed on your machine

2. Step-by-Step Guide

Navigation in Flutter involves moving between different screens (known as routes in Flutter terminology). The basic idea is to configure a map of routes and then navigate to these routes by name.

Setting Up Navigation

  1. First, define routes in MaterialApp widget in your main.dart file:
MaterialApp(
  initialRoute: '/',
  routes: {
    '/': (context) => Screen1(),
    '/second': (context) => Screen2(),
  },
);

Here, '/' refers to the initial screen (i.e., Screen1), and '/second' refers to the second screen (Screen2).

  1. To navigate to Screen2 from Screen1, use Navigator.pushNamed(context, '/second')
// Screen1.dart
FlatButton(
  child: Text('Go to second screen'),
  onPressed: () {
    Navigator.pushNamed(context, '/second');
  },
);

Best Practices and Tips

  • Use meaningful names for your routes.
  • Try to keep your navigation logic in the same place, if possible.

3. Code Examples

Example 1: Simple Navigation

This example demonstrates simple navigation from the HomeScreen to the DetailsScreen.

// main.dart
void main() {
  runApp(MaterialApp(
    initialRoute: '/',
    routes: {
      '/': (context) => HomeScreen(),
      '/details': (context) => DetailsScreen(),
    },
  ));
}

// HomeScreen.dart
class HomeScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text("Home Screen")),
      body: Center(
        child: RaisedButton(
          child: Text('Go to Details Screen'),
          onPressed: () {
            Navigator.pushNamed(context, '/details');
          },
        ),
      ),
    );
  }
}

// DetailsScreen.dart
class DetailsScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text("Details Screen")),
      body: Center(
        child: RaisedButton(
          child: Text('Go Back'),
          onPressed: () {
            Navigator.pop(context);
          },
        ),
      ),
    );
  }
}

In the code above, we have two screens: HomeScreen and DetailsScreen. We navigate to DetailsScreen from HomeScreen using Navigator.pushNamed(context, '/details'). To go back to the HomeScreen, we use Navigator.pop(context).

4. Summary

In this tutorial, we learned about navigation in Flutter, how to set it up, and how to navigate between different screens. The key points covered include defining routes, navigating to a route, and returning to the previous route.

To continue learning about navigation in Flutter, you can explore the following topics:
- How to pass data between routes
- How to return data from a route

Refer to the official Flutter documentation for more information: Flutter Navigation and Routing

5. Practice Exercises

  1. Create a Flutter app with three screens. Add navigation to move between these screens.

  2. In the app created in exercise 1, add a button in the third screen to navigate back to the first screen.

  3. Create a login screen that navigates to a home screen only when a specific username and password are entered.

Tips for Further Practice

Try to create more complex navigation scenarios. For example, you could create a bottom navigation bar with multiple tabs, and each tab has its own navigation stack.