Skip to content
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

[go_router_builder] Add support for relative routes #7174

Closed
wants to merge 14 commits into from

Conversation

ThangVuNguyenViet
Copy link
Contributor

Add supports for relative routes by allowing going to a path relatively, like go('./$path')

This PR is a 2nd part of #6825 to fully resolves flutter/flutter#108177, which allow users to use TypedRelativeGoRoute to construct the route relatively.

Example:

import 'package:go_router/go_router.dart';

const TypedRelativeGoRoute<RelativeRoute> relativeRoute =
    TypedRelativeGoRoute<RelativeRoute>(
  path: 'relative-route',
  routes: <TypedRoute<RouteData>>[
    TypedRelativeGoRoute<InnerRelativeRoute>(path: 'inner-relative-route')
  ],
);

@TypedGoRoute<Route1>(
  path: 'route-1',
  routes: <TypedRoute<RouteData>>[relativeRoute],
)
class Route1 extends GoRouteData {
  const Route1();
}

@TypedGoRoute<Route2>(
  path: 'route-2',
  routes: <TypedRoute<RouteData>>[relativeRoute],
)
class Route2 extends GoRouteData {
  const Route2();
}

class RelativeRoute extends GoRouteData {
  const RelativeRoute();
}

class InnerRelativeRoute extends GoRouteData {
  const InnerRelativeRoute();
}

Basically the added TypedRelativeGoRoute allows us to construct the route tree above. If we replace it with the existing TypedGoRoute it will declare multiple extensions of a same thing and produce build time error

Pre-launch Checklist

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

@ThangVuNguyenViet
Copy link
Contributor Author

I temporarily added the dependency_overrides until the go_router PR gets merged. However, imho that should be there to avoid this type of PR split anyway

@@ -390,6 +390,28 @@ class TypedGoRoute<T extends GoRouteData> extends TypedRoute<T> {
final List<TypedRoute<RouteData>> routes;
}

/// A superclass for each typed go route descendant
@Target(<TargetKind>{TargetKind.library, TargetKind.classType})
class TypedRelativeGoRoute<T extends GoRouteData> extends TypedRoute<T> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't see the difference between this and TypedGoRoute it seems to me that if user uses go_router_builder they are not using url string to navigate so they don't really need relative routing?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well they won't be using './$path' directly, but they'll use route.goRelative(), which uses that under the hood.

Not sure if I got your question right thou..

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I meant they could define a TypedGoRoute directly and do GoRouteData.go, why would they want to define a TypedRelativeRoute ?

Copy link

@thangmoxielabs thangmoxielabs Aug 1, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They can't define TypedGoRoute in multiple places. Take the example in the description, the relativeRoute was declared in a variable and passed in 2 routes. Had that been normal TypedGoRoute, it would've produced 2 extensions with the same name, of which locations are different, and results in build time error

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems to be two different feature right?

  1. is to be able to do go('./relative')
  2. is to be able to reuse the same RouteData with same relative path in different sub route.

and 2 needs to build on top of 1.
If so I think we should separate the 2 into a new pr to make review simpler.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh looks like you already did #6825.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

okay... so do I need to change anything else?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would suggest removing the TypeRelativeRoute from #6825 and do all the TypeRelativeRoute thing in a separate pr

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TypedRelativeRoute needs to be landed in order for this PR to work. The reason this PR is working right now is because of the dependency_overrides, which I meant to remove after the #6825 is merged. Can you suggest how do I remove the TypedRelativeRoute? Do I create a PR with only TypedRelativeRoute declaration, wait for it to get merged and then continue with this PR?

Imo it's feels weird to me that an annotation of a builder package belongs to the main package and not in the builder package, but maybe there is a technical reason for that that I don't know

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No I meant the TypedRelativeRoute should be separate from #6825 since they are not related. and then we can review and merge #6825 first before we review TypedRelativeRoute separately.

@chunhtai chunhtai self-requested a review August 1, 2024 21:45
@chunhtai
Copy link
Contributor

I am going to close this pr since inactive, feel free to reopen when you have time

@chunhtai chunhtai closed this Sep 19, 2024
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