Skip to content

Commit 062e469

Browse files
authored
Page-subclasses to take children instead of builder (#66694)
1 parent b8397f6 commit 062e469

File tree

14 files changed

+244
-472
lines changed

14 files changed

+244
-472
lines changed

packages/flutter/lib/src/cupertino/route.dart

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,8 @@ final DecorationTween _kGradientShadowTween = DecorationTween(
9393
/// * [CupertinoPageRoute], which is a [PageRoute] that leverages this mixin.
9494
mixin CupertinoRouteTransitionMixin<T> on PageRoute<T> {
9595
/// Builds the primary contents of the route.
96-
WidgetBuilder get builder;
96+
@protected
97+
Widget buildContent(BuildContext context);
9798

9899
/// {@template flutter.cupertino.cupertinoRouteTransitionMixin.title}
99100
/// A title string for this route.
@@ -224,7 +225,7 @@ mixin CupertinoRouteTransitionMixin<T> on PageRoute<T> {
224225

225226
@override
226227
Widget buildPage(BuildContext context, Animation<double> animation, Animation<double> secondaryAnimation) {
227-
final Widget child = builder(context);
228+
final Widget child = buildContent(context);
228229
final Widget result = Semantics(
229230
scopesRoute: true,
230231
explicitChildNodes: true,
@@ -351,9 +352,12 @@ class CupertinoPageRoute<T> extends PageRoute<T> with CupertinoRouteTransitionMi
351352
assert(opaque),
352353
super(settings: settings, fullscreenDialog: fullscreenDialog);
353354

354-
@override
355+
/// Builds the primary contents of the route.
355356
final WidgetBuilder builder;
356357

358+
@override
359+
Widget buildContent(BuildContext context) => builder(context);
360+
357361
@override
358362
final String? title;
359363

@@ -378,7 +382,7 @@ class _PageBasedCupertinoPageRoute<T> extends PageRoute<T> with CupertinoRouteTr
378382
CupertinoPage<T> get _page => settings as CupertinoPage<T>;
379383

380384
@override
381-
WidgetBuilder get builder => _page.builder;
385+
Widget buildContent(BuildContext context) => _page.child;
382386

383387
@override
384388
String? get title => _page.title;
@@ -412,20 +416,20 @@ class _PageBasedCupertinoPageRoute<T> extends PageRoute<T> with CupertinoRouteTr
412416
class CupertinoPage<T> extends Page<T> {
413417
/// Creates a cupertino page.
414418
const CupertinoPage({
415-
required this.builder,
419+
required this.child,
416420
this.maintainState = true,
417421
this.title,
418422
this.fullscreenDialog = false,
419423
LocalKey? key,
420424
String? name,
421425
Object? arguments,
422-
}) : assert(builder != null),
426+
}) : assert(child != null),
423427
assert(maintainState != null),
424428
assert(fullscreenDialog != null),
425429
super(key: key, name: name, arguments: arguments);
426430

427-
/// Builds the primary contents of the route.
428-
final WidgetBuilder builder;
431+
/// The content to be shown in the [Route] created by this page.
432+
final Widget child;
429433

430434
/// {@macro flutter.cupertino.cupertinoRouteTransitionMixin.title}
431435
final String? title;

packages/flutter/lib/src/material/page.dart

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,12 @@ class MaterialPageRoute<T> extends PageRoute<T> with MaterialRouteTransitionMixi
4848
assert(opaque),
4949
super(settings: settings, fullscreenDialog: fullscreenDialog);
5050

51-
@override
51+
/// Builds the primary contents of the route.
5252
final WidgetBuilder builder;
5353

54+
@override
55+
Widget buildContent(BuildContext context) => builder(context);
56+
5457
@override
5558
final bool maintainState;
5659

@@ -77,7 +80,8 @@ class MaterialPageRoute<T> extends PageRoute<T> with MaterialRouteTransitionMixi
7780
/// by the [MaterialRouteTransitionMixin.buildTransitions].
7881
mixin MaterialRouteTransitionMixin<T> on PageRoute<T> {
7982
/// Builds the primary contents of the route.
80-
WidgetBuilder get builder;
83+
@protected
84+
Widget buildContent(BuildContext context);
8185

8286
@override
8387
Duration get transitionDuration => const Duration(milliseconds: 300);
@@ -101,7 +105,7 @@ mixin MaterialRouteTransitionMixin<T> on PageRoute<T> {
101105
Animation<double> animation,
102106
Animation<double> secondaryAnimation,
103107
) {
104-
final Widget result = builder(context);
108+
final Widget result = buildContent(context);
105109
assert(() {
106110
if (result == null) {
107111
throw FlutterError(
@@ -148,19 +152,19 @@ mixin MaterialRouteTransitionMixin<T> on PageRoute<T> {
148152
class MaterialPage<T> extends Page<T> {
149153
/// Creates a material page.
150154
const MaterialPage({
151-
@required this.builder,
155+
@required this.child,
152156
this.maintainState = true,
153157
this.fullscreenDialog = false,
154158
LocalKey key,
155159
String name,
156160
Object arguments,
157-
}) : assert(builder != null),
161+
}) : assert(child != null),
158162
assert(maintainState != null),
159163
assert(fullscreenDialog != null),
160164
super(key: key, name: name, arguments: arguments);
161165

162-
/// Builds the primary contents of the route.
163-
final WidgetBuilder builder;
166+
/// The content to be shown in the [Route] created by this page.
167+
final Widget child;
164168

165169
/// {@macro flutter.widgets.modalRoute.maintainState}
166170
final bool maintainState;
@@ -188,7 +192,9 @@ class _PageBasedMaterialPageRoute<T> extends PageRoute<T> with MaterialRouteTran
188192
MaterialPage<T> get _page => settings as MaterialPage<T>;
189193

190194
@override
191-
WidgetBuilder get builder => _page.builder;
195+
Widget buildContent(BuildContext context) {
196+
return _page.child;
197+
}
192198

193199
@override
194200
bool get maintainState => _page.maintainState;

packages/flutter/lib/src/widgets/navigator.dart

Lines changed: 4 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,6 @@ import 'ticker_provider.dart';
3737
/// * [Navigator], which is where all the [Route]s end up.
3838
typedef RouteFactory = Route<dynamic>? Function(RouteSettings settings);
3939

40-
/// Creates a route for the given context and route settings.
41-
///
42-
/// Used by [CustomBuilderPage.routeBuilder].
43-
typedef RouteBuilder<T> = Route<T> Function(BuildContext context, RouteSettings settings);
44-
4540
/// Creates a series of one or more routes.
4641
///
4742
/// Used by [Navigator.onGenerateInitialRoutes].
@@ -507,8 +502,6 @@ class RouteSettings {
507502
///
508503
/// * [Navigator.pages], which accepts a list of [Page]s and updates its routes
509504
/// history.
510-
/// * [CustomBuilderPage], a [Page] subclass that provides the API to build a
511-
/// customized route.
512505
abstract class Page<T> extends RouteSettings {
513506
/// Creates a page and initializes [key] for subclasses.
514507
///
@@ -543,38 +536,6 @@ abstract class Page<T> extends RouteSettings {
543536
String toString() => '${objectRuntimeType(this, 'Page')}("$name", $key, $arguments)';
544537
}
545538

546-
/// A [Page] that builds a customized [Route] based on the [routeBuilder].
547-
///
548-
/// The type argument `T` is the corresponding [Route]'s return type, as
549-
/// used by [Route.currentResult], [Route.popped], and [Route.didPop].
550-
class CustomBuilderPage<T> extends Page<T> {
551-
/// Creates a page with a custom route builder.
552-
///
553-
/// Use [routeBuilder] to specify the route that will be created from this
554-
/// page.
555-
const CustomBuilderPage({
556-
required LocalKey key,
557-
required this.routeBuilder,
558-
String? name,
559-
Object? arguments,
560-
}) : assert(key != null),
561-
assert(routeBuilder != null),
562-
super(key: key, name: name, arguments: arguments);
563-
564-
/// A builder that will be called during [createRoute] to create a [Route].
565-
///
566-
/// The routes returned from this builder must have their settings equal to
567-
/// the input `settings`.
568-
final RouteBuilder<T> routeBuilder;
569-
570-
@override
571-
Route<T> createRoute(BuildContext context) {
572-
final Route<T> route = routeBuilder(context, this);
573-
assert(route.settings == this);
574-
return route;
575-
}
576-
}
577-
578539
/// An interface for observing the behavior of a [Navigator].
579540
class NavigatorObserver {
580541
/// The navigator that the observer is observing, if any.
@@ -1045,10 +1006,10 @@ class DefaultTransitionDelegate<T> extends TransitionDelegate<T> {
10451006
/// The [Navigator] will convert its [Navigator.pages] into a stack of [Route]s
10461007
/// if it is provided. A change in [Navigator.pages] will trigger an update to
10471008
/// the stack of [Route]s. The [Navigator] will update its routes to match the
1048-
/// new configuration of its [Navigator.pages]. To use this API, one can use
1049-
/// [CustomBuilderPage] or create a [Page] subclass and defines a list of
1050-
/// [Page]s for [Navigator.pages]. A [Navigator.onPopPage] callback is also
1051-
/// required to properly clean up the input pages in case of a pop.
1009+
/// new configuration of its [Navigator.pages]. To use this API, one can create
1010+
/// a [Page] subclass and defines a list of [Page]s for [Navigator.pages]. A
1011+
/// [Navigator.onPopPage] callback is also required to properly clean up the
1012+
/// input pages in case of a pop.
10521013
///
10531014
/// By Default, the [Navigator] will use [DefaultTransitionDelegate] to decide
10541015
/// how routes transition in or out of the screen. To customize it, define a

packages/flutter/lib/src/widgets/pages.dart

Lines changed: 0 additions & 127 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,6 @@ Widget _defaultTransitionsBuilder(BuildContext context, Animation<double> animat
4646
///
4747
/// Callers must define the [pageBuilder] function which creates the route's
4848
/// primary contents. To add transitions define the [transitionsBuilder] function.
49-
///
50-
/// See also:
51-
///
52-
/// * [TransitionBuilderPage], which is a [Page] of this class.
5349
class PageRouteBuilder<T> extends PageRoute<T> {
5450
/// Creates a route that delegates to builder callbacks.
5551
///
@@ -120,126 +116,3 @@ class PageRouteBuilder<T> extends PageRoute<T> {
120116
return transitionsBuilder(context, animation, secondaryAnimation, child);
121117
}
122118
}
123-
124-
/// A page that creates a [PageRoute] with customizable transition.
125-
///
126-
/// Similar to the [PageRouteBuilder], callers must define the [pageBuilder]
127-
/// function which creates the route's primary contents. To add transitions
128-
/// define the [transitionsBuilder] function.
129-
///
130-
/// See also:
131-
///
132-
/// * [PageRouteBuilder], which is a [PageRoute] version of this class.
133-
class TransitionBuilderPage<T> extends Page<T> {
134-
/// Creates a [TransitionBuilderPage].
135-
const TransitionBuilderPage({
136-
required this.pageBuilder,
137-
this.transitionsBuilder = _defaultTransitionsBuilder,
138-
this.transitionDuration = const Duration(milliseconds: 300),
139-
this.reverseTransitionDuration = const Duration(milliseconds: 300),
140-
this.opaque = true,
141-
this.barrierDismissible = false,
142-
this.barrierColor,
143-
this.barrierLabel,
144-
this.maintainState = true,
145-
this.fullscreenDialog = false,
146-
LocalKey? key,
147-
String? name,
148-
Object? arguments,
149-
}) : assert(pageBuilder != null),
150-
assert(transitionsBuilder != null),
151-
assert(opaque != null),
152-
assert(barrierDismissible != null),
153-
assert(maintainState != null),
154-
assert(fullscreenDialog != null),
155-
super(key: key, name: name, arguments: arguments);
156-
157-
/// {@macro flutter.widgets.pageRouteBuilder.pageBuilder}
158-
final RoutePageBuilder pageBuilder;
159-
160-
/// {@macro flutter.widgets.pageRouteBuilder.transitionsBuilder}
161-
final RouteTransitionsBuilder transitionsBuilder;
162-
163-
/// {@macro flutter.widgets.transitionRoute.transitionDuration}
164-
final Duration transitionDuration;
165-
166-
/// {@macro flutter.widgets.transitionRoute.reverseTransitionDuration}
167-
final Duration reverseTransitionDuration;
168-
169-
/// {@macro flutter.widgets.transitionRoute.opaque}
170-
final bool opaque;
171-
172-
/// {@macro flutter.widgets.modalRoute.barrierDismissible}
173-
final bool barrierDismissible;
174-
175-
/// {@macro flutter.widgets.modalRoute.barrierColor}
176-
///
177-
/// See also:
178-
///
179-
/// * [barrierDismissible], which controls the behavior of the barrier when
180-
/// tapped.
181-
/// * [ModalBarrier], the widget that implements this feature.
182-
final Color? barrierColor;
183-
184-
/// {@macro flutter.widgets.modalRoute.barrierLabel}
185-
///
186-
/// See also:
187-
///
188-
/// * [barrierDismissible], which controls the behavior of the barrier when
189-
/// tapped.
190-
/// * [ModalBarrier], the widget that implements this feature.
191-
final String? barrierLabel;
192-
193-
/// {@macro flutter.widgets.modalRoute.maintainState}
194-
final bool maintainState;
195-
196-
/// {@macro flutter.widgets.pageRoute.fullscreenDialog}
197-
final bool fullscreenDialog;
198-
199-
@override
200-
Route<T> createRoute(BuildContext context) => _PageBasedPageRouteBuilder<T>(this);
201-
}
202-
203-
// A page-based version of the [PageRouteBuilder].
204-
//
205-
// This class gets its builder and settings directly from the [TransitionBuilderPage],
206-
// so that its content updates accordingly to the [TransitionBuilderPage].
207-
class _PageBasedPageRouteBuilder<T> extends PageRoute<T>{
208-
_PageBasedPageRouteBuilder(
209-
TransitionBuilderPage<T> page,
210-
) : assert(page != null),
211-
super(settings: page, fullscreenDialog: page.fullscreenDialog);
212-
213-
TransitionBuilderPage<T> get _page => settings as TransitionBuilderPage<T>;
214-
215-
@override
216-
Duration get transitionDuration => _page.transitionDuration;
217-
218-
@override
219-
Duration get reverseTransitionDuration => _page.reverseTransitionDuration;
220-
221-
@override
222-
bool get opaque => _page.opaque;
223-
224-
@override
225-
bool get barrierDismissible => _page.barrierDismissible;
226-
227-
@override
228-
Color? get barrierColor => _page.barrierColor;
229-
230-
@override
231-
String? get barrierLabel => _page.barrierLabel;
232-
233-
@override
234-
bool get maintainState => _page.maintainState;
235-
236-
@override
237-
Widget buildPage(BuildContext context, Animation<double> animation, Animation<double> secondaryAnimation) {
238-
return _page.pageBuilder(context, animation, secondaryAnimation);
239-
}
240-
241-
@override
242-
Widget buildTransitions(BuildContext context, Animation<double> animation, Animation<double> secondaryAnimation, Widget child) {
243-
return _page.transitionsBuilder(context, animation, secondaryAnimation, child);
244-
}
245-
}

packages/flutter/test/cupertino/app_test.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -236,12 +236,12 @@ class SimpleNavigatorRouterDelegate extends RouterDelegate<RouteInformation> wit
236236
pages: <Page<void>>[
237237
// We need at least two pages for the pop to propagate through.
238238
// Otherwise, the navigator will bubble the pop to the system navigator.
239-
CupertinoPage<void>(
240-
builder: (BuildContext context) => const Text('base'),
239+
const CupertinoPage<void>(
240+
child: Text('base'),
241241
),
242242
CupertinoPage<void>(
243243
key: ValueKey<String>(routeInformation?.location),
244-
builder: (BuildContext context) => builder(context, routeInformation),
244+
child: builder(context, routeInformation),
245245
)
246246
],
247247
);

0 commit comments

Comments
 (0)