Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
b1ec0ad
:sparkles: Add `buildPageWithState`
ValentinVignal Aug 12, 2022
1a4a22a
:arrow_up: Upgrade version number
ValentinVignal Aug 12, 2022
8d90548
Merge remote-tracking branch 'upstream/main' into go-router/expose-go…
ValentinVignal Aug 19, 2022
d61584b
:wastebasket: Deprecate buildPage
ValentinVignal Aug 19, 2022
b67e5f8
:recycle: buildPageWithState uses buildPage
ValentinVignal Aug 19, 2022
2b80b52
:white_check_mark: Add tests for build buildPage and buildPageWithState
ValentinVignal Aug 19, 2022
b473d7a
:memo: Update the CHANGELOG
ValentinVignal Aug 19, 2022
ef2d9fc
:rotating_light: Add licence
ValentinVignal Aug 19, 2022
32b94eb
:rotating_light: Fix linter
ValentinVignal Aug 19, 2022
12bad41
:bulb: Update comments and doc
ValentinVignal Aug 20, 2022
f89bb7e
:memo: Update example
ValentinVignal Aug 20, 2022
d40e8b9
:arrow_up: Upgrade go_router_builder version
ValentinVignal Aug 20, 2022
0b51946
:rotating_light: Add ignore in go_router_builder example
ValentinVignal Aug 20, 2022
d5f9c92
:rewind: Continue to use buildPage in go_router_builder and ignore fu…
ValentinVignal Aug 20, 2022
4d3f7b6
Merge remote-tracking branch 'upstream/main' into go-router/expose-go…
ValentinVignal Aug 24, 2022
03b9e52
Merge remote-tracking branch 'upstream/main' into go-router/expose-go…
ValentinVignal Aug 26, 2022
8a72d8d
🔀 Fix merge conflict
ValentinVignal Aug 26, 2022
d782698
Merge remote-tracking branch 'upstream/main' into go-router/expose-go…
ValentinVignal Aug 30, 2022
93c849a
Merge branch 'main' into go-router/expose-go-router-state-in-page-bui…
ValentinVignal Aug 31, 2022
4aa9150
:rewind: revert changes on go_router_builder
ValentinVignal Aug 31, 2022
66b94df
:rewind: revert changes on go_router_builder
ValentinVignal Aug 31, 2022
732927e
Update packages/go_router/CHANGELOG.md
ValentinVignal Sep 1, 2022
3802759
Merge remote-tracking branch 'upstream/main' into go-router/expose-go…
ValentinVignal Sep 6, 2022
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: 5 additions & 0 deletions packages/go_router/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 4.4.0

- Adds `buildPageWithState` to `GoRouteData`.
- `GoRouteData.buildPage` is now deprecated in favor of `GoRouteData.buildPageWithState`.

## 4.3.0

- Allows `Map<String, dynamic>` maps as `queryParams` of `goNamed`, `replacedName`, `pushNamed` and `namedLocation`.
Expand Down
35 changes: 29 additions & 6 deletions packages/go_router/lib/src/route_data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ import 'state.dart';
/// Baseclass for supporting
/// [typed routing](https://gorouter.dev/typed-routing).
///
/// Subclasses must override one of [build], [buildPage], or [redirect].
/// Subclasses must override one of [build], [buildPageWithState], or
/// [redirect].
abstract class GoRouteData {
/// Allows subclasses to have `const` constructors.
///
Expand All @@ -21,28 +22,50 @@ abstract class GoRouteData {

/// Creates the [Widget] for `this` route.
///
/// Subclasses must override one of [build], [buildPage], or [redirect].
/// Subclasses must override one of [build], [buildPageWithState], or
/// [redirect].
///
/// Corresponds to [GoRoute.builder].
Widget build(BuildContext context) => throw UnimplementedError(
'One of `build` or `buildPage` must be implemented.',
'One of `build` or `buildPageWithState` must be implemented.',
);

/// A page builder for this route.
///
/// Subclasses can override this function to provide a custom [Page].
///
/// Subclasses must override one of [build], [buildPage], or [redirect].
/// Subclasses must override one of [build], [buildPageWithState] or
/// [redirect].
///
/// Corresponds to [GoRoute.pageBuilder].
///
/// By default, returns a [Page] instance that is ignored, causing a default
/// [Page] implementation to be used with the results of [build].
@Deprecated(
'This method has been deprecated in favor of buildPageWithState. '
'This feature was deprecated after v4.3.0.',
)
Page<void> buildPage(BuildContext context) => const NoOpPage();

/// A page builder for this route with [GoRouterState].
///
/// Subclasses can override this function to provide a custom [Page].
///
/// Subclasses must override one of [build], [buildPageWithState] or
/// [redirect].
///
/// Corresponds to [GoRoute.pageBuilder].
///
/// By default, returns a [Page] instance that is ignored, causing a default
/// [Page] implementation to be used with the results of [build].
Page<void> buildPageWithState(BuildContext context, GoRouterState state) =>

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should return buildPage by default

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, I did it in b67e5f8

// ignore: deprecated_member_use_from_same_package
buildPage(context);

/// An optional redirect function for this route.
///
/// Subclasses must override one of [build], [buildPage], or [redirect].
/// Subclasses must override one of [build], [buildPageWithState], or
/// [redirect].
///
/// Corresponds to [GoRoute.redirect].
String? redirect() => null;
Expand Down Expand Up @@ -83,7 +106,7 @@ abstract class GoRouteData {
factoryImpl(state).build(context);

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

String? redirect(GoRouterState state) => factoryImpl(state).redirect();

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: 4.3.0
version: 4.4.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
107 changes: 107 additions & 0 deletions packages/go_router/test/route_data_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
// 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.

import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:go_router/go_router.dart';

class _GoRouteDataBuild extends GoRouteData {
const _GoRouteDataBuild();
@override
Widget build(BuildContext context) => const SizedBox(key: Key('build'));
}

final GoRoute _goRouteDataBuild = GoRouteData.$route(
path: '/build',
factory: (GoRouterState state) => const _GoRouteDataBuild(),
);

class _GoRouteDataBuildPage extends GoRouteData {
const _GoRouteDataBuildPage();
@override
Page<void> buildPage(BuildContext context) => const MaterialPage<void>(
child: SizedBox(key: Key('buildPage')),
);
}

final GoRoute _goRouteDataBuildPage = GoRouteData.$route(
path: '/build-page',
factory: (GoRouterState state) => const _GoRouteDataBuildPage(),
);

class _GoRouteDataBuildPageWithState extends GoRouteData {
const _GoRouteDataBuildPageWithState();
@override
Page<void> buildPageWithState(BuildContext context, GoRouterState state) =>
const MaterialPage<void>(
child: SizedBox(key: Key('buildPageWithState')),
);
}

final GoRoute _goRouteDataBuildPageWithState = GoRouteData.$route(
path: '/build-page-with-state',
factory: (GoRouterState state) => const _GoRouteDataBuildPageWithState(),
);

final List<GoRoute> _routes = <GoRoute>[
_goRouteDataBuild,
_goRouteDataBuildPage,
_goRouteDataBuildPageWithState,
];

void main() {
testWidgets(
'It should build the page from the overridden build method',
(WidgetTester tester) async {
final GoRouter goRouter = GoRouter(
initialLocation: '/build',
routes: _routes,
);
await tester.pumpWidget(MaterialApp.router(
routeInformationProvider: goRouter.routeInformationProvider,
routeInformationParser: goRouter.routeInformationParser,
routerDelegate: goRouter.routerDelegate,
));
expect(find.byKey(const Key('build')), findsOneWidget);
expect(find.byKey(const Key('buildPage')), findsNothing);
expect(find.byKey(const Key('buildPageWithState')), findsNothing);
},
);

testWidgets(
'It should build the page from the overridden buildPage method',
(WidgetTester tester) async {
final GoRouter goRouter = GoRouter(
initialLocation: '/build-page',
routes: _routes,
);
await tester.pumpWidget(MaterialApp.router(
routeInformationProvider: goRouter.routeInformationProvider,
routeInformationParser: goRouter.routeInformationParser,
routerDelegate: goRouter.routerDelegate,
));
expect(find.byKey(const Key('build')), findsNothing);
expect(find.byKey(const Key('buildPage')), findsOneWidget);
expect(find.byKey(const Key('buildPageWithState')), findsNothing);
},
);

testWidgets(
'It should build the page from the overridden buildPageWithState method',
(WidgetTester tester) async {
final GoRouter goRouter = GoRouter(
initialLocation: '/build-page-with-state',
routes: _routes,
);
await tester.pumpWidget(MaterialApp.router(
routeInformationProvider: goRouter.routeInformationProvider,
routeInformationParser: goRouter.routeInformationParser,
routerDelegate: goRouter.routerDelegate,
));
expect(find.byKey(const Key('build')), findsNothing);
expect(find.byKey(const Key('buildPage')), findsNothing);
expect(find.byKey(const Key('buildPageWithState')), findsOneWidget);
},
);
}