-
Notifications
You must be signed in to change notification settings - Fork 3.6k
[go_router_builder] Add ShellRoute support to go_router_builder #3439
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
Changes from all commits
54e1576
a52bad2
9797546
dadb98f
2df6775
b7441ed
5633d15
be0a39f
6ed5535
ce95e76
bfa3c2f
1e2606f
632bf68
548882a
7b1305b
75bff09
76b9b81
7694e5a
4600f37
3efeecd
fb449ff
f0924ed
6eab717
9f8b067
7f4c6e0
0e48f2a
b08bc6d
4c08623
61c3d9d
3013a05
3376c73
219f261
1832753
d7752fd
28e593f
6b4ccf1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,153 @@ | ||
| // Copyright 2013 The Flutter Authors. All rights reserved. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you add a simple test for this example? Just make sure things will compile and the shellRoute's widget is built
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added test in fb449ff |
||
| // Use of this source code is governed by a BSD-style license that can be | ||
| // found in the LICENSE file. | ||
|
|
||
| // ignore_for_file: public_member_api_docs | ||
|
|
||
| import 'package:flutter/material.dart'; | ||
| import 'package:go_router/go_router.dart'; | ||
|
|
||
| part 'shell_route_example.g.dart'; | ||
|
|
||
| void main() => runApp(App()); | ||
|
|
||
| class App extends StatelessWidget { | ||
| App({super.key}); | ||
|
|
||
| @override | ||
| Widget build(BuildContext context) => MaterialApp.router( | ||
| routerConfig: _router, | ||
| ); | ||
|
|
||
| final GoRouter _router = GoRouter( | ||
| routes: $appRoutes, | ||
| initialLocation: '/foo', | ||
| ); | ||
| } | ||
|
|
||
| class HomeScreen extends StatelessWidget { | ||
| const HomeScreen({super.key}); | ||
|
|
||
| @override | ||
| Widget build(BuildContext context) => Scaffold( | ||
| appBar: AppBar(title: const Text('foo')), | ||
| ); | ||
| } | ||
|
|
||
| @TypedShellRoute<MyShellRouteData>( | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I may have missed something, how would one specified navigator key for typedshellRoute and also the parent navigator key for typedGoRoute
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think what @johnpryan had in mind was to add a static final or getter called
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
It seems a bit not obvious. Why not just let it be a parameter into the
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree it's not obvious. I assumed that you two had agreed on the approach beforehand and I just picked up the work where it was left off.
I will first need to make a PR on go_router (since that's where these classes are defined) and then raise the minimum version of go_router in go_router_builder. Could you confirm that it's ok?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes, that is ok. Sorry about the miscommunication, the original PR was still in its initial state, so there may be some issue that hasn't been discovered yet.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No problem, I also should have double checked.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you explain a bit why it needs to be const? I can't find the places the enforce the requirement
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| routes: <TypedRoute<RouteData>>[ | ||
| TypedGoRoute<FooRouteData>(path: '/foo'), | ||
| TypedGoRoute<BarRouteData>(path: '/bar'), | ||
| ], | ||
| ) | ||
| class MyShellRouteData extends ShellRouteData { | ||
GP4cK marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| const MyShellRouteData(); | ||
|
|
||
| @override | ||
| Widget builder( | ||
| BuildContext context, | ||
| GoRouterState state, | ||
| Widget navigator, | ||
| ) { | ||
| return MyShellRouteScreen(child: navigator); | ||
| } | ||
| } | ||
|
|
||
| class FooRouteData extends GoRouteData { | ||
| const FooRouteData(); | ||
|
|
||
| @override | ||
| Widget build(BuildContext context, GoRouterState state) { | ||
| return const FooScreen(); | ||
| } | ||
| } | ||
|
|
||
| class BarRouteData extends GoRouteData { | ||
| const BarRouteData(); | ||
|
|
||
| @override | ||
| Widget build(BuildContext context, GoRouterState state) { | ||
| return const BarScreen(); | ||
| } | ||
| } | ||
|
|
||
| class MyShellRouteScreen extends StatelessWidget { | ||
| const MyShellRouteScreen({required this.child, super.key}); | ||
|
|
||
| final Widget child; | ||
|
|
||
| int getCurrentIndex(BuildContext context) { | ||
| final String location = GoRouter.of(context).location; | ||
| if (location == '/bar') { | ||
| return 1; | ||
| } | ||
| return 0; | ||
| } | ||
|
|
||
| @override | ||
| Widget build(BuildContext context) { | ||
| final int currentIndex = getCurrentIndex(context); | ||
| return Scaffold( | ||
| body: child, | ||
| bottomNavigationBar: BottomNavigationBar( | ||
| currentIndex: currentIndex, | ||
| items: const <BottomNavigationBarItem>[ | ||
| BottomNavigationBarItem( | ||
| icon: Icon(Icons.home), | ||
| label: 'Foo', | ||
| ), | ||
| BottomNavigationBarItem( | ||
| icon: Icon(Icons.business), | ||
| label: 'Bar', | ||
| ), | ||
| ], | ||
| onTap: (int index) { | ||
| switch (index) { | ||
| case 0: | ||
| const FooRouteData().go(context); | ||
| break; | ||
| case 1: | ||
| const BarRouteData().go(context); | ||
| break; | ||
| } | ||
| }, | ||
| ), | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| class FooScreen extends StatelessWidget { | ||
| const FooScreen({super.key}); | ||
|
|
||
| @override | ||
| Widget build(BuildContext context) { | ||
| return const Text('Foo'); | ||
| } | ||
| } | ||
|
|
||
| class BarScreen extends StatelessWidget { | ||
| const BarScreen({super.key}); | ||
|
|
||
| @override | ||
| Widget build(BuildContext context) { | ||
| return const Text('Bar'); | ||
| } | ||
| } | ||
|
|
||
| @TypedGoRoute<LoginRoute>(path: '/login') | ||
| class LoginRoute extends GoRouteData { | ||
| const LoginRoute(); | ||
|
|
||
| @override | ||
| Widget build(BuildContext context, GoRouterState state) => | ||
| const LoginScreen(); | ||
| } | ||
|
|
||
| class LoginScreen extends StatelessWidget { | ||
| const LoginScreen({super.key}); | ||
|
|
||
| @override | ||
| Widget build(BuildContext context) { | ||
| return const Text('Login'); | ||
| } | ||
| } | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe also add a link to the examples
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've added the link here: Add link to example
It will work when the PR is merged.