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.
Section overview
5 resourcesExplores 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.
Navigatoris a widget that manages a stack ofRouteobjects and provides methods to manage the stack, likeNavigator.pushandNavigator.pop.Routeis an abstraction for a "screen" or "page" in a Flutter app, and it's used by Navigator.MaterialPageRouteis 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.
Related topics
Keep learning with adjacent tracks.
Popular tools
Helpful utilities for quick tasks.
Latest articles
Fresh insights from the CodiWiki team.
AI in Drug Discovery: Accelerating Medical Breakthroughs
In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…
Read articleAI in Retail: Personalized Shopping and Inventory Management
In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …
Read articleAI 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 articleAI 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 articleAI in Legal Compliance: Ensuring Regulatory Adherence
In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…
Read article