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]: implemented helpers for ShellRoute #2730

Merged
Merged
Show file tree
Hide file tree
Changes from 24 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
eff98e6
[Feature]: implemented helpers for ShellRoute
hawkkiller Oct 23, 2022
8302cbb
[bugfix]: change export
hawkkiller Oct 23, 2022
f1ad347
Removed named private constructor for TypedRoute
hawkkiller Oct 23, 2022
beeab62
Update CHANGELOG.md
hawkkiller Oct 23, 2022
b432c30
Update pubspec.yaml
hawkkiller Oct 23, 2022
efe47d0
[Feature]: split routes per type
hawkkiller Oct 23, 2022
751cd20
[Feature]: add new exports
hawkkiller Oct 23, 2022
b5eac46
Tests, refactor routes
hawkkiller Oct 27, 2022
a8a7656
Merge branch 'main' into go_router/shell-route-go-router-builder
hawkkiller Nov 11, 2022
9868e98
Merge branch 'main' into go_router/shell-route-go-router-builder
hawkkiller Nov 15, 2022
0ac454d
remove line in changelog
hawkkiller Nov 15, 2022
a8b7beb
Merge branch 'main' into go_router/shell-route-go-router-builder
hawkkiller Dec 2, 2022
9f06bd8
Add navigatorKey for each route
hawkkiller Dec 2, 2022
3edfe0b
Fixed tests
hawkkiller Dec 2, 2022
a8a4670
Format
hawkkiller Dec 2, 2022
078c46f
Bump version in pubspec.yaml
hawkkiller Dec 2, 2022
c3ba9ba
Update packages/go_router/lib/src/route_data.dart
hawkkiller Dec 13, 2022
ff1ee61
Update packages/go_router/lib/src/route_data.dart
hawkkiller Dec 13, 2022
36fb257
Merge branch 'main' into go_router/shell-route-go-router-builder
hawkkiller Dec 13, 2022
f21079a
Merge branch 'main' of github.com:flutter/packages into pr/hawkkiller…
hawkkiller Jan 25, 2023
24d7a36
Removed buildPageWithState tests
hawkkiller Jan 30, 2023
909d07f
Merge branch 'main' into go_router/shell-route-go-router-builder
hawkkiller Jan 31, 2023
9cd060d
Bump version in pubspec
hawkkiller Jan 31, 2023
48da0e8
Merge branch 'main' into go_router/shell-route-go-router-builder
hawkkiller Feb 21, 2023
d068ae5
Merge branch 'main' into go_router/shell-route-go-router-builder
johnpryan Feb 22, 2023
3246f3e
Address comments from code review
johnpryan Feb 22, 2023
5052dd4
Merge branch 'main' of github.com:flutter/packages into go_router/she…
johnpryan Feb 22, 2023
2c4fd62
Merge branch 'main' into go_router/shell-route-go-router-builder
johnpryan Feb 22, 2023
298a601
Export ShellRouteData
johnpryan Feb 22, 2023
5e4a659
Merge branch 'go_router/shell-route-go-router-builder' of github.com:…
johnpryan Feb 22, 2023
c2d1f48
Remove unnecessary import
johnpryan Feb 22, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion packages/go_router/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 6.0.10

- Adds helpers for go_router_builder for ShellRoute support

## 6.0.9

- Fixes deprecation message for `GoRouterState.namedLocation`
Expand Down Expand Up @@ -28,7 +32,6 @@
- Makes `CustomTransitionPage.barrierDismissible` work

## 6.0.2

- Fixes missing result on pop in go_router extension.

## 6.0.1
Expand Down
2 changes: 1 addition & 1 deletion packages/go_router/lib/go_router.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export 'src/configuration.dart'
export 'src/misc/extensions.dart';
export 'src/misc/inherited_router.dart';
export 'src/pages/custom_transition_page.dart';
export 'src/route_data.dart' show GoRouteData, TypedGoRoute;
export 'src/route_data.dart' show GoRouteData, TypedGoRoute, TypedShellRoute;
johnpryan marked this conversation as resolved.
Show resolved Hide resolved
export 'src/router.dart';
export 'src/typedefs.dart'
show GoRouterPageBuilder, GoRouterRedirect, GoRouterWidgetBuilder;
148 changes: 139 additions & 9 deletions packages/go_router/lib/src/route_data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,21 @@ import 'package:meta/meta_meta.dart';
import 'route.dart';
import 'state.dart';

/// {@template route_data}
johnpryan marked this conversation as resolved.
Show resolved Hide resolved
/// A superclass for each route data
/// {@endtemplate}
abstract class RouteData {
/// {@macro route_data}
const RouteData();
}

/// Baseclass for supporting
/// [Type-safe routing](https://pub.dev/documentation/go_router/latest/topics/Type-safe%20routes-topic.html).
///
/// Subclasses must override one of [build], [buildPage], or
/// [redirect].
/// {@category Type-safe routes}
abstract class GoRouteData {
abstract class GoRouteData extends RouteData {
/// Allows subclasses to have `const` constructors.
///
/// [GoRouteData] is abstract and cannot be instantiated directly.
Expand Down Expand Up @@ -74,7 +82,8 @@ abstract class GoRouteData {
static GoRoute $route<T extends GoRouteData>({
required String path,
required T Function(GoRouterState) factory,
List<GoRoute> routes = const <GoRoute>[],
GlobalKey<NavigatorState>? parentNavigatorKey,
List<RouteBase> routes = const <RouteBase>[],
}) {
T factoryImpl(GoRouterState state) {
final Object? extra = state.extra;
Expand Down Expand Up @@ -103,6 +112,7 @@ abstract class GoRouteData {
pageBuilder: pageBuilder,
redirect: redirect,
routes: routes,
parentNavigatorKey: parentNavigatorKey,
);
}

Expand All @@ -111,26 +121,146 @@ abstract class GoRouteData {
static final Expando<GoRouteData> _stateObjectExpando = Expando<GoRouteData>(
'GoRouteState to GoRouteData expando',
);

/// [navigatorKey] is used to point to a certain navigator
///
/// It will use the given key to find the right navigator for [GoRoute]
GlobalKey<NavigatorState>? get navigatorKey => null;
}

/// {@template shell_route_data}
/// Baseclass for supporting
/// [nested navigation](https://pub.dev/packages/go_router#nested-navigation)
/// {@endtemplate}
abstract class ShellRouteData extends RouteData {
/// {@macro shell_route_data}
const ShellRouteData();

/// [pageBuilder] is used to build the page
Page<void> pageBuilder(
BuildContext context,
GoRouterState state,
Widget navigator,
) =>
const NoOpPage();

/// [pageBuilder] is used to build the page
Widget builder(
BuildContext context,
GoRouterState state,
Widget navigator,
) =>
throw UnimplementedError(
'One of `builder` or `pageBuilder` must be implemented.',
);

/// A helper function used by generated code.
///
/// Should not be used directly.
static ShellRoute $route<T extends ShellRouteData>({
required T Function(GoRouterState) factory,
GlobalKey<NavigatorState>? navigatorKey,
List<RouteBase> routes = const <RouteBase>[],
}) {
T factoryImpl(GoRouterState state) {
final Object? extra = state.extra;

// If the "extra" value is of type `T` then we know it's the source
// instance of `GoRouteData`, so it doesn't need to be recreated.
if (extra is T) {
return extra;
}

return (_stateObjectExpando[state] ??= factory(state)) as T;
}

Widget builder(
BuildContext context,
GoRouterState state,
Widget navigator,
) =>
factoryImpl(state).builder(
context,
state,
navigator,
);

Page<void> pageBuilder(
BuildContext context,
GoRouterState state,
Widget navigator,
) =>
factoryImpl(state).pageBuilder(
context,
state,
navigator,
);

return ShellRoute(
builder: builder,
pageBuilder: pageBuilder,
routes: routes,
navigatorKey: navigatorKey,
);
}

/// Used to cache [ShellRouteData] that corresponds to a given [GoRouterState]
/// to minimize the number of times it has to be deserialized.
static final Expando<ShellRouteData> _stateObjectExpando =
Expando<ShellRouteData>(
'GoRouteState to ShellRouteData expando',
);

/// It will be used to instantiate [Navigator] with the given key
GlobalKey<NavigatorState>? get navigatorKey => null;
}

/// {@template typed_route}
/// A superclass for each typed route descendant
/// {@endtemplate}
class TypedRoute<T extends RouteData> {
/// {@macro typed_route}
const TypedRoute();
}

/// Annotation for types that support typed routing.
/// {@template typed_go_route}
/// A superclass for each typed go route descendant
/// {@endtemplate}
@Target(<TargetKind>{TargetKind.library, TargetKind.classType})
class TypedGoRoute<T extends GoRouteData> {
/// Instantiates a new instance of [TypedGoRoute].
class TypedGoRoute<T extends GoRouteData> extends TypedRoute<T> {
/// {@macro typed_go_route}
const TypedGoRoute({
required this.path,
this.routes = const <TypedGoRoute<GoRouteData>>[],
this.routes = const <TypedRoute<RouteData>>[],
});

/// The path that corresponds to this rout.
/// The path that corresponds to this route.
///
/// See [GoRoute.path].
///
///
final String path;

/// Child route definitions.
///
/// See [GoRoute.routes].
final List<TypedGoRoute<GoRouteData>> routes;
/// See [RouteBase.routes].
final List<TypedRoute<RouteData>> routes;
}

/// {@template typed_shell_route}
/// A superclass for each typed shell route descendant
/// {@endtemplate}
@Target(<TargetKind>{TargetKind.library, TargetKind.classType})
class TypedShellRoute<T extends ShellRouteData> extends TypedRoute<T> {
/// {@macro typed_shell_route}
const TypedShellRoute({
this.routes = const <TypedRoute<RouteData>>[],
});

/// Child route definitions.
///
/// See [RouteBase.routes].
final List<TypedRoute<RouteData>> routes;
}

/// Internal class used to signal that the default page behavior should be used.
Expand Down
2 changes: 1 addition & 1 deletion packages/go_router/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: go_router
description: A declarative router for Flutter based on Navigation 2 supporting
deep linking, data-driven routes and more
version: 6.0.9
version: 6.0.10
repository: https://github.com/flutter/packages/tree/main/packages/go_router
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+go_router%22

Expand Down
Loading