Navigation from one page to other is vital part in mobile app development.
Flutter comes with very easy solutions for navigation using π₯°Navigatorπ₯° class.
In this post I am going to cover two types of navigation in flutter.
1) Simple navigation
2) Named navigation
Our scenario is to move to the second page on button pressed, we were taking two pages
1) Home page
2) Second page
also we are passing a string to second page.
Simple navigation
Simple navigation can be achieved by using Navigator push method
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
the syntax for Navigator push is
'Navigator.push(context, MaterialPageRoute(builder: (context) => Widget()));'
Seems simple isnt it, See below for code snippet.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
I would say this is the best way to handle navigation, you will find the answer soooon π
Named navigation is using routing concept, now you get it why it is the best way ahhh π
All the navigation routes are isolated/dedicated in one place, so we don't need to worry about instantiating class in our dart code.
lets jump into the code.
Flutter Material app comes with function callback for generating the App routes
onGenerateRoute
We can define our routes in a function and set it to onGenerateRoute, so that flutter knows where to go on navigation.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Syntax for named routing using navigator: Navigator.of(context).pushNamed('/route', arguments: args);
even simple isn't it??
See below code snippet
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Join Lazar for a hands-on session where youβll build it, break it, debug it, and fix it. Youβll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good olβ AI to find and fix issues fast.
Hi @prakashselvaraj, care to give an example for named route when we want to send multiple arguments? I haven't found a good way to receive it on the destination screen.
For example, this is the destination screen constructor:
Navigator pushNamed accepts only one argument which is an Object type, So you can pass the desired argument as class/list or whatever object type which match your requirement.
Top comments (2)
Hi @prakashselvaraj, care to give an example for named route when we want to send multiple arguments? I haven't found a good way to receive it on the destination screen.
For example, this is the destination screen constructor:
And this is the arguments:
Navigator pushNamed accepts only one argument which is an Object type, So you can pass the desired argument as class/list or whatever object type which match your requirement.
also in your case
<String, String>{'bmiResult': '1', 'resultText': 'x', 'interpretation': 'y'}
you can use Map on the navigating page
Hope this answers you !!
Happy fluterring ππ!!