Skip to content

Conversation

@getBoolean
Copy link
Contributor

This is something I hacked together, bringing the titleBuilder from auto_route to go_router. I suspect it will need some changes, as I am not familiar with the internals of GoRouter.

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.

@chunhtai
Copy link
Contributor

This seems to be a very limited use case, I think the root problem is that the shellroute doesn't have information about the current child. I think this is something we should figure out a better API to expose such thing. WDYT?

@getBoolean
Copy link
Contributor Author

getBoolean commented Dec 20, 2023

@chunhtai yes that would work, though I wasn't able to come up with a more generic solution without casting. Perhaps a solution could involve generics on the GoRoute class, but I'm not sure how that would be done without loosing the types in the routes list

@getBoolean
Copy link
Contributor Author

getBoolean commented Dec 21, 2023

I experimented with having adding generic type that the custom data builder can return. it is possible, but i had to add generics to nearly every go_router class. (Probably not what we want, but what are your thoughts?)
getBoolean@80a7022, see test GoRoute titleBuilder accessible from StatefulShellRoute for usage

The result is really good though, the titleBuilder can return any data type and access it from the ShellRoute.builder

@getBoolean
Copy link
Contributor Author

getBoolean commented Dec 21, 2023

In 0ddddb6, I converted the title builder to extensions, similar to Flutter's theme extensions

Usage:

GoRoute(
  path: 'b',
  extensions: <GoRouterStateExtensionBuilder>[
    (_, __) => const RouteTitleExtension('B'),
    (_, __) => const OtherCustomDataExtension('B.2'),
  ],
  builder: (BuildContext context, GoRouterState state) {
    return const Scaffold(
      body: Text('Screen 2'),
    );
  },
)

class RouteTitleExtension
    implements GoRouterStateExtension<RouteTitleExtension> {
  const RouteTitleExtension(this.title);

  final String title;
}

GoRouterState.of(context).extension<RouteTitleExtension>(context).title

@getBoolean getBoolean closed this Dec 22, 2023
@getBoolean
Copy link
Contributor Author

New PR opened based on 13.0.0 #5736

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.

2 participants