Skip to content

Commit

Permalink
[go_router] Add GoRouter.maybeOf (flutter#3216)
Browse files Browse the repository at this point in the history
* feat: Add GoRouter.maybeOf

* docs: Update the version number

* test: Add tests on GoRouter.of and GoRouter.maybeOf
  • Loading branch information
ValentinVignal committed Feb 23, 2023
1 parent 13ee644 commit 8ad3fde
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 1 deletion.
4 changes: 4 additions & 0 deletions packages/go_router/CHANGELOG.md
@@ -1,3 +1,7 @@
## 6.1.0

- Adds `GoRouter.maybeOf` to get the closest `GoRouter` from the context, if there is any.

## 6.0.10

- Adds helpers for go_router_builder for ShellRoute support
Expand Down
7 changes: 7 additions & 0 deletions packages/go_router/lib/src/router.dart
Expand Up @@ -301,6 +301,13 @@ class GoRouter extends ChangeNotifier implements RouterConfig<RouteMatchList> {
return inherited!.goRouter;
}

/// The current GoRouter in the widget tree, if any.
static GoRouter? maybeOf(BuildContext context) {
final InheritedGoRouter? inherited =
context.dependOnInheritedWidgetOfExactType<InheritedGoRouter>();
return inherited?.goRouter;
}

@override
void dispose() {
_routeInformationProvider.dispose();
Expand Down
2 changes: 1 addition & 1 deletion packages/go_router/pubspec.yaml
@@ -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.10
version: 6.1.0
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
62 changes: 62 additions & 0 deletions packages/go_router/test/go_router_test.dart
Expand Up @@ -3275,6 +3275,68 @@ void main() {
});
});
});

group('of', () {
testWidgets(
'It should return the go router instance of the widget tree',
(WidgetTester tester) async {
const Key key = Key('key');
final List<RouteBase> routes = <RouteBase>[
GoRoute(
path: '/',
builder: (_, __) => const SizedBox(key: key),
),
];

final GoRouter router = await createRouter(routes, tester);
final Element context = tester.element(find.byKey(key));
final GoRouter foundRouter = GoRouter.of(context);
expect(foundRouter, router);
},
);

testWidgets(
'It should throw if there is no go router in the widget tree',
(WidgetTester tester) async {
const Key key = Key('key');
await tester.pumpWidget(const SizedBox(key: key));

final Element context = tester.element(find.byKey(key));
expect(() => GoRouter.of(context), throwsA(anything));
},
);
});

group('maybeOf', () {
testWidgets(
'It should return the go router instance of the widget tree',
(WidgetTester tester) async {
const Key key = Key('key');
final List<RouteBase> routes = <RouteBase>[
GoRoute(
path: '/',
builder: (_, __) => const SizedBox(key: key),
),
];

final GoRouter router = await createRouter(routes, tester);
final Element context = tester.element(find.byKey(key));
final GoRouter? foundRouter = GoRouter.maybeOf(context);
expect(foundRouter, router);
},
);

testWidgets(
'It should return null if there is no go router in the widget tree',
(WidgetTester tester) async {
const Key key = Key('key');
await tester.pumpWidget(const SizedBox(key: key));

final Element context = tester.element(find.byKey(key));
expect(GoRouter.maybeOf(context), isNull);
},
);
});
}

/// This allows a value of type T or T? to be treated as a value of type T?.
Expand Down

0 comments on commit 8ad3fde

Please sign in to comment.