csells / go_router

The purpose of the go_router for Flutter is to use declarative routes to reduce complexity, regardless of the platform you're targeting (mobile, web, desktop), handling deep linking from Android, iOS and the web while still allowing an easy-to-use developer experience.
https://gorouter.dev
442 stars 96 forks source link

Add ability to get a stack of GoRoutes that match a given name #152

Closed esDotDev closed 2 years ago

esDotDev commented 2 years ago

There are times when we want to retrieve the .path value for a given route, using the name.

For example, I might have a view that need to check whether it is currently on the search page, but it only has the route name available to use.

So, given this route:

GoRoute(
  path: '/search/:keywords',
  name: SearchPage.routeName,
)

I want to check whether this is my current route somewhere down the tree, using SearchPage.routeName to do it:

string searchPath = router.routesForName(SearchPage.routeName).last.path;
searchPath = searchPath.split['/'].first(); // gets 'search'
if(goRouter.location.contains('/$searchPath/')){
  // we're on the search page
}
csells commented 2 years ago

I believe this can be built with GoRouter.of(context).routerDelegate.matches today.