**How to Navigate to New Page and Back in Flutter: A Beginner’s Guide**
As a beginner in Flutter development, navigating between pages can seem like a daunting task. But don’t worry, I’ve got you covered! In this blog post, we’ll explore the simple steps to navigate to a new page and back to the previous one in Flutter.
**What is Navigation in Flutter?**
In Flutter, navigation refers to the process of moving from one page or screen to another. This is done using routes, which are essentially a mapping of URLs to widgets (the UI components of your app). When a user navigates between screens, Flutter uses these routes to load the corresponding widgets and render the new screen.
**Creating a New Page (Route) in Flutter**
To create a new page or route in Flutter, you need to define a new widget that will serve as the content for this page. Let’s create a new Dart file called `second_screen.dart` and add the following code:
“`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: Text(‘This is the Second Screen’),
),
);
}
}
“`
In this example, we’ve created a `SecondScreen` widget that extends `StatelessWidget`. This widget will display a simple “Second Screen” title and some text in the center of the page.
**Navigating to the New Page**
To navigate to the new page, we’ll use the `Navigator` class provided by Flutter. Create a new Dart file called `main.dart` and add the following code:
“`dart
import ‘package:flutter/material.dart’;
import ‘./second_screen.dart’;
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: ‘Navigation Demo’,
home: FirstScreen(),
);
}
}
class FirstScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(‘First Screen’),
),
body: Center(
child: ElevatedButton(
child: Text(‘Go to Second Screen’),
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => SecondScreen()),
);
},
),
),
);
}
}
“`
In this example, we’ve created a `FirstScreen` widget that displays a “Go to Second Screen” button. When the button is pressed, Flutter will use the `Navigator` class to push a new route onto the navigator’s stack, effectively navigating to the `SecondScreen`.
**Returning to the Previous Page**
To return to the previous page, we can use the `Navigator.pop` method. Update the `SecondScreen` widget to add a button that will pop the current route and return to the previous one:
“`dart
class SecondScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(‘Second Screen’),
),
body: Center(
child: Column(
children: [
Text(‘This is the Second Screen’),
ElevatedButton(
child: Text(‘Go Back’),
onPressed: () {
Navigator.pop(context);
},
),
],
),
),
);
}
}
“`
When the “Go Back” button is pressed, Flutter will pop the current route from the navigator’s stack, effectively returning to the previous screen.
**Conclusion**
In this blog post, we’ve covered the basics of navigation in Flutter. We’ve learned how to create a new page or route, navigate to it, and return to the previous page. With these skills, you’re ready to build more complex apps with multiple screens.
I hope you found this tutorial helpful! If you have any questions or need further assistance, please don’t hesitate to reach out. Happy coding!