I'm using onGenerateRoute parameter on MaterialApp for routing.
MaterialApp(
...
onGenerateRoute: Router.generateRoute,
...
)
And then use pushNamed()
.
I want to show my route names in the url but I can't achieve that with onGenerateRoute.
If I use routes:
in MaterialApp,
like this:
MaterialApp(
...
routes: {sliverScreen: (context) => SliverScreen()},
...
)
It works. But I think it's redundant if I have provided onGenerateRoute on MaterialApp.
To show how we use onGenerateRoute in Flutter, we must have two pages. The first page and the second page. Consequently, when the app opens up, it takes us to the second page. If we want to get back to the first page, we just tick the cross mark and it navigates back to the first page.
You can use go_router package from https://pub.dev. it is made by flutter team for this kind of routing. class App extends StatelessWidget { App({Key? key}) : super(key: key); @override Widget build(BuildContext context) => MaterialApp.
You can add name
to generated route, and it will appear in the URL:
MaterialPageRoute(
builder: ... ,
settings: RouteSettings(name: 'SOMENAME'))
Use this on onGenerateRoute
:
onGenerateRoute: (settings) => RouteGenerator.generateRoute(settings),
And while returning MaterialPageRoute
from the route file just add settings:
return MaterialPageRoute(
settings: settings,
builder:(_) => HomeWeb()
);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With