Skip to content

[go_router_builder][go_router] Add support for relative routes #6823

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 10 commits into from

Conversation

ThangVuNguyenViet
Copy link
Contributor

@ThangVuNguyenViet ThangVuNguyenViet commented May 28, 2024

Add supports for [go_router_builder] relative routes by adding GoRouter.goRelative which enables TypedRelativeGoRoute to be used as relative route.

Previously it was impossible to construct relative route trees with TypedGoRouter, because the generator will generate duplicate extension class. The only options were either don't use go_router_builder, or (worse) declare each sub-route classes, e.g

@TypedGoRoute<HomeRoute>(
  path: '/',
  routes: [@TypedGoRoute<DetailRouteFromHome>(path: 'details',
    routes: [@TypedGoRoute<SettingsRouteFromDetailFromHome>(path: 'settings')]
  ),
],
)

Now with TypedRelativeGoRoute added, we can declare a common sub-route

/// The route configuration.
final GoRouter _router = GoRouter(
  debugLogDiagnostics: true,
  routes: $appRoutes,
);
const detailRoute = TypedRelativeGoRoute<DetailsRoute>(
  path: 'details/:detailId',
  routes: [
    TypedRelativeGoRoute<SettingsRoute>(path: 'settings/:settingId'),
  ],
);

@TypedGoRoute<HomeRoute>(
  path: '/',
  routes: [detailRoute],
)
class HomeRoute extends GoRouteData {
  @override
  Widget build(BuildContext context, GoRouterState state) {
    return const HomeScreen();
  }
}

@TypedGoRoute<DashboardRoute>(
  path: '/dashboard',
  routes: [detailRoute],
)
class DashboardRoute extends GoRouteData {
  @override
  Widget build(BuildContext context, GoRouterState state) {
    return const DashboardScreen();
  }
}

And simply use DetailsRoute(detailId: 'dId').goRelative(context)

Fixes #108177

Pre-launch Checklist

If you need help, consider asking for advice on the #hackers-new channel on Discord.

Copy link

google-cla bot commented May 28, 2024

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@ThangVuNguyenViet
Copy link
Contributor Author

Couple of notes here: I can't either add add tests or example for go_router_builder because it depends on the newly added TypedRelativeGoRoute in go_router. Further instructions on this matter is appreciated

@chunhtai
Copy link
Contributor

you need to separate out to two pr, one for go_router another one for go_router_builder. Merge go_router pr first and then you can bump the go_router version in this package so that you can write test and example for it

@ThangVuNguyenViet
Copy link
Contributor Author

gotcha. Imma temporarily close this PR and open a new go_router only PR and reopen this once the new PR is merged

auto-submit bot pushed a commit that referenced this pull request Nov 13, 2024
Add supports for relative routes by allowing going to a path relatively, like go('./$path')

This PR doesn't fully resolve any issue, but it's mandatory to further add examples & tests for `TypedRelativeGoRoute` (see [#7174](#6823)), which will resolves [#108177](flutter/flutter#108177)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[go_router] Add support for relative routes
3 participants