Flutter / Introduction to Flutter

Navigation Setup

This tutorial covers navigation in Flutter. It explains what navigation is, how to set up navigation, and how to navigate between screens in a Flutter app.

Tutorial 3 of 4 4 resources in this section

Section overview

4 resources

Introduce the Flutter SDK and its basic principles.

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.

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

Percentage Calculator

Easily calculate percentages, discounts, and more.

Use tool

Meta Tag Analyzer

Analyze and generate meta tags for SEO.

Use tool

Random Name Generator

Generate realistic names with customizable options.

Use tool

Markdown to HTML Converter

Convert Markdown to clean HTML.

Use tool

Random String Generator

Generate random alphanumeric strings for API keys or unique IDs.

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