-
Notifications
You must be signed in to change notification settings - Fork 3.3k
[go_router_builder] add support to go_router_builder
for initializing a ShellRoute
with observers
#5563
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
Merged
Merged
[go_router_builder] add support to go_router_builder
for initializing a ShellRoute
with observers
#5563
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
622e270
feat: add observers to ShellRouteData
ClaireDavis de305a6
add example
ClaireDavis 9c46f95
remove print
ClaireDavis eec44ef
Merge pull request #1 from ClaireDavis/cd/shell-route-observers
ClaireDavis d1a222d
update changelog and version
ClaireDavis 79764d5
Merge branch 'main' into main
ClaireDavis 013eead
include next section in changelog
ClaireDavis 601f39b
Merge branch 'main' of https://github.com/ClaireDavis/packages
ClaireDavis 2e53d94
bump to rerun flaking workflow
ClaireDavis 6ab2fc6
Merge branch 'main' into main
ClaireDavis b76c843
Update packages/go_router_builder/CHANGELOG.md
ClaireDavis 2db3d19
bump minor version
ClaireDavis File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
170 changes: 170 additions & 0 deletions
170
packages/go_router_builder/example/lib/shell_route_with_observers_example.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,170 @@ | ||
// Copyright 2013 The Flutter Authors. All rights reserved. | ||
// 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_with_observers_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: '/home', | ||
); | ||
} | ||
|
||
@TypedShellRoute<MyShellRouteData>( | ||
routes: <TypedRoute<RouteData>>[ | ||
TypedGoRoute<HomeRouteData>(path: '/home'), | ||
TypedGoRoute<UsersRouteData>( | ||
path: '/users', | ||
routes: <TypedGoRoute<UserRouteData>>[ | ||
TypedGoRoute<UserRouteData>(path: ':id'), | ||
], | ||
), | ||
], | ||
) | ||
class MyShellRouteData extends ShellRouteData { | ||
const MyShellRouteData(); | ||
|
||
static final List<NavigatorObserver> $observers = <NavigatorObserver>[ | ||
MyNavigatorObserver() | ||
]; | ||
|
||
@override | ||
Widget builder(BuildContext context, GoRouterState state, Widget navigator) { | ||
return MyShellRouteScreen(child: navigator); | ||
} | ||
} | ||
|
||
class MyNavigatorObserver extends NavigatorObserver { | ||
@override | ||
void didPush(Route<dynamic> route, Route<dynamic>? previousRoute) {} | ||
} | ||
|
||
class MyShellRouteScreen extends StatelessWidget { | ||
const MyShellRouteScreen({required this.child, super.key}); | ||
|
||
final Widget child; | ||
|
||
int getCurrentIndex(BuildContext context) { | ||
final String location = GoRouterState.of(context).uri.toString(); | ||
if (location.startsWith('/users')) { | ||
return 1; | ||
} | ||
return 0; | ||
} | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
final int selectedIndex = getCurrentIndex(context); | ||
|
||
return Scaffold( | ||
body: Row( | ||
children: <Widget>[ | ||
NavigationRail( | ||
destinations: const <NavigationRailDestination>[ | ||
NavigationRailDestination( | ||
icon: Icon(Icons.home), | ||
label: Text('Home'), | ||
), | ||
NavigationRailDestination( | ||
icon: Icon(Icons.group), | ||
label: Text('Users'), | ||
), | ||
], | ||
selectedIndex: selectedIndex, | ||
onDestinationSelected: (int index) { | ||
switch (index) { | ||
case 0: | ||
const HomeRouteData().go(context); | ||
break; | ||
case 1: | ||
const UsersRouteData().go(context); | ||
break; | ||
} | ||
}, | ||
), | ||
const VerticalDivider(thickness: 1, width: 1), | ||
Expanded(child: child), | ||
], | ||
), | ||
); | ||
} | ||
} | ||
|
||
class HomeRouteData extends GoRouteData { | ||
const HomeRouteData(); | ||
|
||
@override | ||
Widget build(BuildContext context, GoRouterState state) { | ||
return const Center(child: Text('The home page')); | ||
} | ||
} | ||
|
||
class UsersRouteData extends GoRouteData { | ||
const UsersRouteData(); | ||
|
||
@override | ||
Widget build(BuildContext context, GoRouterState state) { | ||
return ListView( | ||
children: <Widget>[ | ||
for (int userID = 1; userID <= 3; userID++) | ||
ListTile( | ||
title: Text('User $userID'), | ||
onTap: () => UserRouteData(id: userID).go(context), | ||
), | ||
], | ||
); | ||
} | ||
} | ||
|
||
class DialogPage extends Page<void> { | ||
/// A page to display a dialog. | ||
const DialogPage({required this.child, super.key}); | ||
|
||
/// The widget to be displayed which is usually a [Dialog] widget. | ||
final Widget child; | ||
|
||
@override | ||
Route<void> createRoute(BuildContext context) { | ||
return DialogRoute<void>( | ||
context: context, | ||
settings: this, | ||
builder: (BuildContext context) => child, | ||
); | ||
} | ||
} | ||
|
||
class UserRouteData extends GoRouteData { | ||
const UserRouteData({required this.id}); | ||
|
||
// Without this static key, the dialog will not cover the navigation rail. | ||
final int id; | ||
|
||
@override | ||
Page<void> buildPage(BuildContext context, GoRouterState state) { | ||
return DialogPage( | ||
key: state.pageKey, | ||
child: Center( | ||
child: SizedBox( | ||
width: 300, | ||
height: 300, | ||
child: Card(child: Center(child: Text('User $id'))), | ||
), | ||
), | ||
); | ||
} | ||
} |
93 changes: 93 additions & 0 deletions
93
packages/go_router_builder/example/lib/shell_route_with_observers_example.g.dart
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Won't this no longer match against
navigatorKey
due to thetoLowerCase()
call? It also would require a space afternavigatorKey
or beforeobservers
in order to actually match