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
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.
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).
Screen2 from Screen1, use Navigator.pushNamed(context, '/second')// Screen1.dart
FlatButton(
child: Text('Go to second screen'),
onPressed: () {
Navigator.pushNamed(context, '/second');
},
);
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).
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
Create a Flutter app with three screens. Add navigation to move between these screens.
In the app created in exercise 1, add a button in the third screen to navigate back to the first screen.
Create a login screen that navigates to a home screen only when a specific username and password are entered.
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.