Navigation is the mechanism of moving between different screens in your application. Flutter provides a robust navigation and routing system to manage the transition between different pages.
1. The Navigator Widget π§
The Navigator widget is at the core of Flutter's navigation system. It manages a stack of Route objects and provides methods to manipulate the stack, like push and pop.
2. Basic Navigation π
Let's start with a simple example of navigating between two screens.
Named routes provide a way to define a mapping between a named identifier and a route. This is useful for more structured navigation and handling deep links.
You can pass data between routes by passing arguments to the Navigator.push method.
// Define a constructor for SecondPage to accept dataclassSecondPageextendsStatelessWidget {finalString data;SecondPage({required this.data});// ...}// Now pass data while navigatingNavigator.push( context,MaterialPageRoute( builder: (context) =>SecondPage(data:'Hello from First Page!'), ),);
Assignments π
Let's reinforce your understanding with some practical exercises:
By tackling these assignments, youβll get hands-on experience with Flutter's navigation system.