Mobile App Development / Flutter Development

Navigation and Routing in Flutter

In this tutorial, we'll go in-depth into navigation and routing in Flutter. We'll learn how to navigate between different screens and pass data between them.

Tutorial 4 of 5 5 resources in this section

Section overview

5 resources

Explores app development with Flutter, a popular UI toolkit by Google for building natively compiled applications.

Navigation and Routing in Flutter - A Detailed Tutorial

1. Introduction

In this tutorial, we aim to delve deep into the world of navigation and routing in Flutter. Navigation is a crucial part of any application as it allows users to move between different screens. Routing, on the other hand, is the mechanism to define the navigation paths.

By the end of this tutorial, you will learn:

  • The basics of navigation and routing in Flutter
  • How to navigate between different screens
  • How to pass data between screens

Prerequisites:

  • Basic knowledge of Dart programming language
  • Fundamental understanding of Flutter framework and widget tree

2. Step-by-Step Guide

The main concepts we'll cover are Navigator, Route and MaterialPageRoute.

  • Navigator is a widget that manages a stack of Route objects and provides methods to manage the stack, like Navigator.push and Navigator.pop.
  • Route is an abstraction for a "screen" or "page" in a Flutter app, and it's used by Navigator.
  • MaterialPageRoute is a type of modal route that replaces the entire screen with a platform-adaptive transition.

Best Practices:

  • Use named routes for navigation. It helps reduce bugs and makes the navigation logic more manageable.
  • Always return a value from your routes. It will make your code more predictable and easier to debug.

3. Code Examples

Let's create a basic Flutter app with two screens and see how to navigate between them.

Example 1: Basic Navigation

Here's the code for the first screen (main.dart):

import 'package:flutter/material.dart';
import 'second_screen.dart';

void main() {
  runApp(MaterialApp(
    title: 'Navigation Basics',
    home: FirstScreen(),
  ));
}

class FirstScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('First Screen'),
      ),
      body: Center(
        child: RaisedButton(
          child: Text('Launch second screen'),
          onPressed: () {
            Navigator.push(
              context,
              MaterialPageRoute(builder: (context) => SecondScreen()),
            );
          },
        ),
      ),
    );
  }
}

In the onPressed callback, we're using Navigator.push to navigate to SecondScreen. The MaterialPageRoute widget creates the route that adjusts to the platform.

Here's the code for the second screen (second_screen.dart):

import 'package:flutter/material.dart';

class SecondScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Second Screen"),
      ),
      body: Center(
        child: RaisedButton(
          onPressed: () {
            Navigator.pop(context);
          },
          child: Text('Go back!'),
        ),
      ),
    );
  }
}

On the second screen, we use Navigator.pop to go back to the previous screen.

4. Summary

In this tutorial, we've learned how to navigate between different screens in a Flutter app and pass data between them. We've seen how to use Navigator.push to navigate to a new screen, and Navigator.pop to go back to the previous one.

As next steps, you can explore more complex navigation patterns, like tab navigation or drawer navigation. The official Flutter documentation is a great resource for this.

5. Practice Exercises

Exercise 1: Create a Flutter app with three screens. Navigate from the first screen to the second, then from the second to the third.

Exercise 2: Enhance the app from the first exercise by passing data from the first screen to the second, then from the second to the third.

Exercise 3: Modify the app from the second exercise to return data from the third screen to the first.

Hints: Remember to use Navigator.push to navigate to a new screen, and Navigator.pop to go back to the previous one. You can pass data to a new screen through the constructor, and return data from a screen using Navigator.pop.

Keep practicing and happy coding!

Need Help Implementing This?

We build custom systems, plugins, and scalable infrastructure.

Discuss Your Project

Related topics

Keep learning with adjacent tracks.

View category

HTML

Learn the fundamental building blocks of the web using HTML.

Explore

CSS

Master CSS to style and format web pages effectively.

Explore

JavaScript

Learn JavaScript to add interactivity and dynamic behavior to web pages.

Explore

Python

Explore Python for web development, data analysis, and automation.

Explore

SQL

Learn SQL to manage and query relational databases.

Explore

PHP

Master PHP to build dynamic and secure web applications.

Explore

Popular tools

Helpful utilities for quick tasks.

Browse tools

URL Encoder/Decoder

Encode or decode URLs easily for web applications.

Use tool

Robots.txt Generator

Create robots.txt for better SEO management.

Use tool

PDF Splitter & Merger

Split, merge, or rearrange PDF files.

Use tool

Time Zone Converter

Convert time between different time zones.

Use tool

Random Number Generator

Generate random numbers between specified ranges.

Use tool

Latest articles

Fresh insights from the CodiWiki team.

Visit blog

AI in Drug Discovery: Accelerating Medical Breakthroughs

In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…

Read article

AI in Retail: Personalized Shopping and Inventory Management

In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …

Read article

AI in Public Safety: Predictive Policing and Crime Prevention

In the realm of public safety, the integration of Artificial Intelligence (AI) stands as a beacon of innovati…

Read article

AI in Mental Health: Assisting with Therapy and Diagnostics

In the realm of mental health, the integration of Artificial Intelligence (AI) stands as a beacon of hope and…

Read article

AI in Legal Compliance: Ensuring Regulatory Adherence

In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…

Read article

Need help implementing this?

Get senior engineering support to ship it cleanly and on time.

Get Implementation Help