Navigate to a new screen and back in Flutter

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
Search for a command to run...

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
No comments yet. Be the first to comment.
Most of you know me for my consistency, a golden arrow in my blog series. I've written 1000 articles in 1008 days! Almost an article a day, and my honeymoon was the only holiday I ever took. I'm super proud of this achievement; it has been a fantasti...

It's not the first time I'll be talking about community. I think it's an essential aspect of any successful tool. This shows in my previous explorations of Astro, Medusa, and now Vendure as well. All these products thrive in a super open, welcoming, ...

The cool part about Vendure is how easy it is to set up and how abstract each layer is. Basically, we get the following elements: External database Server Worker Admin UI Frontend While this is amazing, it also brings a bit of complexity when it co...

The previous article looked at customizing Vendure on a data and process level. In this article, we'll look at customizing emails, as they are often a big part of a webshop system. We'll be looking at two different layers of customization for customi...

Even though Vendure is a pretty significant project out of the box, in some cases, we might want to go in and modify some elements to work to our specific use case. In this article, I'll take a high-level look at some elements we can customize within...

Today we'll look into a pretty standard concept in mobile app development, navigating between pages (routes).
We'll take the basic example today and create two routes. Then we use the Navigator class to navigate between them.
The result for today will look like this:

Let's start by setting up the main structure.
Flutter always starts with the main function, so let's set that up first.
void main() async {
runApp(
MaterialApp(debugShowCheckedModeBanner: false,
initialRoute: '/',
routes: {
'/': (context) => RouteOne(),
'/detail': (context) => RouteTwo(),
}),
);
}
In this example, we define named routes, which is very similar to other hybrid app frameworks.
With this, we get the option to define the initial route, which in our case is the named route: /.
Then we add a routes object, including all our routes. We define two routes:
/ which will render a widget called: RouteOne/detail, which renders a widget called: RouterTwoNow we need to create these routes (widgets).
class RouteOne extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Screen one ☝️'),
),
body: Center(
child: ElevatedButton(
// Within the `FirstScreen` widget
onPressed: () {
// Navigate to the second screen using a named route.
Navigator.pushNamed(context, '/detail');
},
child: Text('Open detail'),
),
),
);
}
}
Here we see the widget for route one. On this screen is a button that shows the text 'Open detail'.
Once we click this button, we call the Navigator class and push a named route called /detail.
As you can see, this Navigator class makes this work and keeps track of all the logic behind it.
We make a similar-looking page for the second screen, but with a button that states 'Go back'.
class RouteTwo extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Screen two ✌️'),
),
body: Center(
child: ElevatedButton(
// Within the `FirstScreen` widget
onPressed: () {
// Navigate to the second screen using a named route.
Navigator.pop(context);
},
child: Text('Go back'),
),
),
);
}
}
And here, we use the navigator again, but we'll call the pop function, which will remove the last pushed route from the stack.
Resulting in an application that can navigate between two routes.
You can also find the complete code on this GitHub link.
Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter