Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to show Flutter Web route name in the URL with onGenerateRoute?

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.

like image 352
mirkancal Avatar asked Oct 18 '19 08:10

mirkancal


People also ask

How do you use onGenerateRoute in flutter?

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.

How do you pass a URL in flutter?

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.


2 Answers

You can add name to generated route, and it will appear in the URL:

  MaterialPageRoute(
    builder: ... ,
    settings: RouteSettings(name: 'SOMENAME'))
like image 156
Spatz Avatar answered Nov 15 '22 09:11

Spatz


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()
);
like image 20
Sagnik Bhattacharya Avatar answered Nov 15 '22 10:11

Sagnik Bhattacharya