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:
Prerequisites:
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:
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.
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.
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!