Skip to content

Conversation

@getBoolean
Copy link
Contributor

@getBoolean getBoolean commented Dec 22, 2023

This PR exposes the current top route to GoRouterState, this allows ShellRoute to know what is the current child and process the state accordingly.

This could be used like this, given that each GoRoute had the name parameter given

StatefulShellRoute.indexedStack(
  parentNavigatorKey: rootNavigatorKey,
  builder: (
    BuildContext context,
    GoRouterState state,
    StatefulNavigationShell navigationShell,
  ) {
    final String? routeName =
        GoRouterState.of(context).topRoute.name;
    final String title = switch (routeName) {
      'a' => 'A',
      'b' => 'B',
      _ => 'Unknown',
    };
    return Column(
      children: <Widget>[
        Text(title),
        Expanded(child: navigationShell),
      ],
    );
  },
  ...
}

If you had to change anything in the flutter/tests repo, include a link to the migration guide as per the breaking change policy.

Pre-launch Checklist

  • I read the Contributor Guide and followed the process outlined there for submitting PRs.
  • I read the Tree Hygiene wiki page, which explains my responsibilities.
  • I read and followed the relevant style guides and ran the auto-formatter. (Unlike the flutter/flutter repo, the flutter/packages repo does use dart format.)
  • I signed the CLA.
  • The title of the PR starts with the name of the package surrounded by square brackets, e.g. [shared_preferences]
  • I listed at least one issue that this PR fixes in the description above.
  • I updated pubspec.yaml with an appropriate new version according to the pub versioning philosophy, or this PR is exempt from version changes.
  • I updated CHANGELOG.md to add a description of the change, following repository CHANGELOG style.
  • I updated/added relevant documentation (doc comments with ///).
  • I added new tests to check the change I am making, or this PR is test-exempt.
  • All existing and new tests are passing.

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

Copy link
Contributor

@chunhtai chunhtai left a comment

Choose a reason for hiding this comment

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

I think the code LGTM, would be good to add an example in the example/ to showcase how to use this new property as well

/// ```
final ValueKey<String> pageKey;

/// The current matched top route
Copy link
Contributor

Choose a reason for hiding this comment

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

This needs more description. May be mention what this mean if you this is a this GoRouteState represent a shellroute.

Copy link
Contributor Author

@getBoolean getBoolean Jan 6, 2024

Choose a reason for hiding this comment

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

I've updated the documentation with the following, is this what you were thinking of?

  /// The current matched top route associated with this state.
  ///
  /// If this state represents a [ShellRoute], the top [GoRoute] will be the current
  /// matched location associated with the [ShellRoute]. This allows the [ShellRoute]'s
  /// associated GoRouterState to be uniquely identified using [GoRoute.name]
  final GoRoute? topRoute;

@chunhtai chunhtai requested a review from hannah-hyj December 27, 2023 22:41
Comment on lines 204 to 216
Widget _buildLeadingButton(BuildContext context) {
final RouteMatchList currentConfiguration =
GoRouter.of(context).routerDelegate.currentConfiguration;
final RouteMatch lastMatch = currentConfiguration.last;
final Uri location = lastMatch is ImperativeRouteMatch
? lastMatch.matches.uri
: currentConfiguration.uri;
final bool canPop = location.pathSegments.length > 1;
final Widget button = canPop
? BackButton(onPressed: GoRouter.of(context).pop)
: const SizedBox.shrink(key: ValueKey<String>('NoLeadingButton'));
return button;
}
Copy link
Contributor Author

@getBoolean getBoolean Jan 6, 2024

Choose a reason for hiding this comment

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

In the example, I had to manually create the leading button since this is in a scaffold above the children. Is there a better way of doing this? For context, auto_route has AutoLeadingButton

Copy link
Member

@hannah-hyj hannah-hyj left a comment

Choose a reason for hiding this comment

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

LGTM

@hannah-hyj hannah-hyj requested a review from johnpryan January 11, 2024 00:45
@hannah-hyj
Copy link
Member

@johnpryan hi john, chun-heng is on vacation, can you take a second look at this PR?

@johnpryan
Copy link
Contributor

Yep I can review.

Copy link
Contributor

@johnpryan johnpryan left a comment

Choose a reason for hiding this comment

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

Looks great!

Copy link
Contributor

@johnpryan johnpryan left a comment

Choose a reason for hiding this comment

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

LGTM, we should also add this API to go_router_builder if we haven't already.

@getBoolean
Copy link
Contributor Author

I don't use go_router_builder so i haven't checked. Should that be done in a separate PR?

@hannah-hyj
Copy link
Member

hannah-hyj commented Jan 19, 2024

yes, go_router_builder changes are usually done in separated PRs.

@johnpryan i don't think we need to change go_router_builder to add this topRoute API. topRoute here is just part of GoRouterState. we don't need to set it when GoRouter is built.

@getBoolean
Copy link
Contributor Author

Are we waiting for @chunhtai to return before merging this?

@hannah-hyj hannah-hyj added the autosubmit Merge PR when tree becomes green via auto submit App label Jan 28, 2024
@auto-submit auto-submit bot merged commit 516648a into flutter:main Jan 28, 2024
@getBoolean
Copy link
Contributor Author

Thanks!

engine-flutter-autoroll added a commit to engine-flutter-autoroll/flutter that referenced this pull request Jan 29, 2024
auto-submit bot pushed a commit to flutter/flutter that referenced this pull request Jan 29, 2024
flutter/packages@cbe8100...516648a

2024-01-28 19920697+getBoolean@users.noreply.github.com [go_router] Add `topRoute` to `GoRouterState` (flutter/packages#5736)
2024-01-27 stuartmorgan@google.com Add Kotlin to CONTRIBUTING.md (flutter/packages#5970)
2024-01-26 engine-flutter-autoroll@skia.org Roll Flutter (stable) from 67457e6 to 4145645 (1 revision) (flutter/packages#5979)

If this roll has caused a breakage, revert this CL and stop the roller
using the controls here:
https://autoroll.skia.org/r/flutter-packages-flutter-autoroll
Please CC flutter-ecosystem@google.com,rmistry@google.com on the revert to ensure that a human
is aware of the problem.

To file a bug in Flutter: https://github.com/flutter/flutter/issues/new/choose

To report a problem with the AutoRoller itself, please file a bug:
https://issues.skia.org/issues/new?component=1389291&template=1850622

Documentation for the AutoRoller is here:
https://skia.googlesource.com/buildbot/+doc/main/autoroll/README.md
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

autosubmit Merge PR when tree becomes green via auto submit App p: go_router

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants