From 062e469f1b9f6721d144c7ab015e938e1295d0bc Mon Sep 17 00:00:00 2001 From: Michael Goderbauer Date: Sun, 27 Sep 2020 15:32:07 -0700 Subject: [PATCH] Page-subclasses to take children instead of builder (#66694) --- packages/flutter/lib/src/cupertino/route.dart | 20 +- packages/flutter/lib/src/material/page.dart | 22 ++- .../flutter/lib/src/widgets/navigator.dart | 47 +---- packages/flutter/lib/src/widgets/pages.dart | 127 ------------- packages/flutter/test/cupertino/app_test.dart | 6 +- .../flutter/test/cupertino/page_test.dart | 48 +++++ .../flutter/test/cupertino/route_test.dart | 28 ++- packages/flutter/test/material/app_test.dart | 6 +- packages/flutter/test/material/page_test.dart | 60 +++++- packages/flutter/test/widgets/app_test.dart | 6 +- .../flutter/test/widgets/navigator_test.dart | 173 +++++++----------- .../route_notification_messages_test.dart | 31 ++-- .../flutter/test/widgets/router_test.dart | 6 +- .../flutter/test/widgets/routes_test.dart | 136 -------------- 14 files changed, 244 insertions(+), 472 deletions(-) diff --git a/packages/flutter/lib/src/cupertino/route.dart b/packages/flutter/lib/src/cupertino/route.dart index 0d6674ffa1..003b2e043c 100644 --- a/packages/flutter/lib/src/cupertino/route.dart +++ b/packages/flutter/lib/src/cupertino/route.dart @@ -93,7 +93,8 @@ final DecorationTween _kGradientShadowTween = DecorationTween( /// * [CupertinoPageRoute], which is a [PageRoute] that leverages this mixin. mixin CupertinoRouteTransitionMixin on PageRoute { /// Builds the primary contents of the route. - WidgetBuilder get builder; + @protected + Widget buildContent(BuildContext context); /// {@template flutter.cupertino.cupertinoRouteTransitionMixin.title} /// A title string for this route. @@ -224,7 +225,7 @@ mixin CupertinoRouteTransitionMixin on PageRoute { @override Widget buildPage(BuildContext context, Animation animation, Animation secondaryAnimation) { - final Widget child = builder(context); + final Widget child = buildContent(context); final Widget result = Semantics( scopesRoute: true, explicitChildNodes: true, @@ -351,9 +352,12 @@ class CupertinoPageRoute extends PageRoute with CupertinoRouteTransitionMi assert(opaque), super(settings: settings, fullscreenDialog: fullscreenDialog); - @override + /// Builds the primary contents of the route. final WidgetBuilder builder; + @override + Widget buildContent(BuildContext context) => builder(context); + @override final String? title; @@ -378,7 +382,7 @@ class _PageBasedCupertinoPageRoute extends PageRoute with CupertinoRouteTr CupertinoPage get _page => settings as CupertinoPage; @override - WidgetBuilder get builder => _page.builder; + Widget buildContent(BuildContext context) => _page.child; @override String? get title => _page.title; @@ -412,20 +416,20 @@ class _PageBasedCupertinoPageRoute extends PageRoute with CupertinoRouteTr class CupertinoPage extends Page { /// Creates a cupertino page. const CupertinoPage({ - required this.builder, + required this.child, this.maintainState = true, this.title, this.fullscreenDialog = false, LocalKey? key, String? name, Object? arguments, - }) : assert(builder != null), + }) : assert(child != null), assert(maintainState != null), assert(fullscreenDialog != null), super(key: key, name: name, arguments: arguments); - /// Builds the primary contents of the route. - final WidgetBuilder builder; + /// The content to be shown in the [Route] created by this page. + final Widget child; /// {@macro flutter.cupertino.cupertinoRouteTransitionMixin.title} final String? title; diff --git a/packages/flutter/lib/src/material/page.dart b/packages/flutter/lib/src/material/page.dart index 9aadd28cb3..278990bd8e 100644 --- a/packages/flutter/lib/src/material/page.dart +++ b/packages/flutter/lib/src/material/page.dart @@ -48,9 +48,12 @@ class MaterialPageRoute extends PageRoute with MaterialRouteTransitionMixi assert(opaque), super(settings: settings, fullscreenDialog: fullscreenDialog); - @override + /// Builds the primary contents of the route. final WidgetBuilder builder; + @override + Widget buildContent(BuildContext context) => builder(context); + @override final bool maintainState; @@ -77,7 +80,8 @@ class MaterialPageRoute extends PageRoute with MaterialRouteTransitionMixi /// by the [MaterialRouteTransitionMixin.buildTransitions]. mixin MaterialRouteTransitionMixin on PageRoute { /// Builds the primary contents of the route. - WidgetBuilder get builder; + @protected + Widget buildContent(BuildContext context); @override Duration get transitionDuration => const Duration(milliseconds: 300); @@ -101,7 +105,7 @@ mixin MaterialRouteTransitionMixin on PageRoute { Animation animation, Animation secondaryAnimation, ) { - final Widget result = builder(context); + final Widget result = buildContent(context); assert(() { if (result == null) { throw FlutterError( @@ -148,19 +152,19 @@ mixin MaterialRouteTransitionMixin on PageRoute { class MaterialPage extends Page { /// Creates a material page. const MaterialPage({ - @required this.builder, + @required this.child, this.maintainState = true, this.fullscreenDialog = false, LocalKey key, String name, Object arguments, - }) : assert(builder != null), + }) : assert(child != null), assert(maintainState != null), assert(fullscreenDialog != null), super(key: key, name: name, arguments: arguments); - /// Builds the primary contents of the route. - final WidgetBuilder builder; + /// The content to be shown in the [Route] created by this page. + final Widget child; /// {@macro flutter.widgets.modalRoute.maintainState} final bool maintainState; @@ -188,7 +192,9 @@ class _PageBasedMaterialPageRoute extends PageRoute with MaterialRouteTran MaterialPage get _page => settings as MaterialPage; @override - WidgetBuilder get builder => _page.builder; + Widget buildContent(BuildContext context) { + return _page.child; + } @override bool get maintainState => _page.maintainState; diff --git a/packages/flutter/lib/src/widgets/navigator.dart b/packages/flutter/lib/src/widgets/navigator.dart index 0cd849ea68..b787a9f1f3 100644 --- a/packages/flutter/lib/src/widgets/navigator.dart +++ b/packages/flutter/lib/src/widgets/navigator.dart @@ -37,11 +37,6 @@ import 'ticker_provider.dart'; /// * [Navigator], which is where all the [Route]s end up. typedef RouteFactory = Route? Function(RouteSettings settings); -/// Creates a route for the given context and route settings. -/// -/// Used by [CustomBuilderPage.routeBuilder]. -typedef RouteBuilder = Route Function(BuildContext context, RouteSettings settings); - /// Creates a series of one or more routes. /// /// Used by [Navigator.onGenerateInitialRoutes]. @@ -507,8 +502,6 @@ class RouteSettings { /// /// * [Navigator.pages], which accepts a list of [Page]s and updates its routes /// history. -/// * [CustomBuilderPage], a [Page] subclass that provides the API to build a -/// customized route. abstract class Page extends RouteSettings { /// Creates a page and initializes [key] for subclasses. /// @@ -543,38 +536,6 @@ abstract class Page extends RouteSettings { String toString() => '${objectRuntimeType(this, 'Page')}("$name", $key, $arguments)'; } -/// A [Page] that builds a customized [Route] based on the [routeBuilder]. -/// -/// The type argument `T` is the corresponding [Route]'s return type, as -/// used by [Route.currentResult], [Route.popped], and [Route.didPop]. -class CustomBuilderPage extends Page { - /// Creates a page with a custom route builder. - /// - /// Use [routeBuilder] to specify the route that will be created from this - /// page. - const CustomBuilderPage({ - required LocalKey key, - required this.routeBuilder, - String? name, - Object? arguments, - }) : assert(key != null), - assert(routeBuilder != null), - super(key: key, name: name, arguments: arguments); - - /// A builder that will be called during [createRoute] to create a [Route]. - /// - /// The routes returned from this builder must have their settings equal to - /// the input `settings`. - final RouteBuilder routeBuilder; - - @override - Route createRoute(BuildContext context) { - final Route route = routeBuilder(context, this); - assert(route.settings == this); - return route; - } -} - /// An interface for observing the behavior of a [Navigator]. class NavigatorObserver { /// The navigator that the observer is observing, if any. @@ -1045,10 +1006,10 @@ class DefaultTransitionDelegate extends TransitionDelegate { /// The [Navigator] will convert its [Navigator.pages] into a stack of [Route]s /// if it is provided. A change in [Navigator.pages] will trigger an update to /// the stack of [Route]s. The [Navigator] will update its routes to match the -/// new configuration of its [Navigator.pages]. To use this API, one can use -/// [CustomBuilderPage] or create a [Page] subclass and defines a list of -/// [Page]s for [Navigator.pages]. A [Navigator.onPopPage] callback is also -/// required to properly clean up the input pages in case of a pop. +/// new configuration of its [Navigator.pages]. To use this API, one can create +/// a [Page] subclass and defines a list of [Page]s for [Navigator.pages]. A +/// [Navigator.onPopPage] callback is also required to properly clean up the +/// input pages in case of a pop. /// /// By Default, the [Navigator] will use [DefaultTransitionDelegate] to decide /// how routes transition in or out of the screen. To customize it, define a diff --git a/packages/flutter/lib/src/widgets/pages.dart b/packages/flutter/lib/src/widgets/pages.dart index 78c2d4c52d..d30f0d80b3 100644 --- a/packages/flutter/lib/src/widgets/pages.dart +++ b/packages/flutter/lib/src/widgets/pages.dart @@ -46,10 +46,6 @@ Widget _defaultTransitionsBuilder(BuildContext context, Animation animat /// /// Callers must define the [pageBuilder] function which creates the route's /// primary contents. To add transitions define the [transitionsBuilder] function. -/// -/// See also: -/// -/// * [TransitionBuilderPage], which is a [Page] of this class. class PageRouteBuilder extends PageRoute { /// Creates a route that delegates to builder callbacks. /// @@ -120,126 +116,3 @@ class PageRouteBuilder extends PageRoute { return transitionsBuilder(context, animation, secondaryAnimation, child); } } - -/// A page that creates a [PageRoute] with customizable transition. -/// -/// Similar to the [PageRouteBuilder], callers must define the [pageBuilder] -/// function which creates the route's primary contents. To add transitions -/// define the [transitionsBuilder] function. -/// -/// See also: -/// -/// * [PageRouteBuilder], which is a [PageRoute] version of this class. -class TransitionBuilderPage extends Page { - /// Creates a [TransitionBuilderPage]. - const TransitionBuilderPage({ - required this.pageBuilder, - this.transitionsBuilder = _defaultTransitionsBuilder, - this.transitionDuration = const Duration(milliseconds: 300), - this.reverseTransitionDuration = const Duration(milliseconds: 300), - this.opaque = true, - this.barrierDismissible = false, - this.barrierColor, - this.barrierLabel, - this.maintainState = true, - this.fullscreenDialog = false, - LocalKey? key, - String? name, - Object? arguments, - }) : assert(pageBuilder != null), - assert(transitionsBuilder != null), - assert(opaque != null), - assert(barrierDismissible != null), - assert(maintainState != null), - assert(fullscreenDialog != null), - super(key: key, name: name, arguments: arguments); - - /// {@macro flutter.widgets.pageRouteBuilder.pageBuilder} - final RoutePageBuilder pageBuilder; - - /// {@macro flutter.widgets.pageRouteBuilder.transitionsBuilder} - final RouteTransitionsBuilder transitionsBuilder; - - /// {@macro flutter.widgets.transitionRoute.transitionDuration} - final Duration transitionDuration; - - /// {@macro flutter.widgets.transitionRoute.reverseTransitionDuration} - final Duration reverseTransitionDuration; - - /// {@macro flutter.widgets.transitionRoute.opaque} - final bool opaque; - - /// {@macro flutter.widgets.modalRoute.barrierDismissible} - final bool barrierDismissible; - - /// {@macro flutter.widgets.modalRoute.barrierColor} - /// - /// See also: - /// - /// * [barrierDismissible], which controls the behavior of the barrier when - /// tapped. - /// * [ModalBarrier], the widget that implements this feature. - final Color? barrierColor; - - /// {@macro flutter.widgets.modalRoute.barrierLabel} - /// - /// See also: - /// - /// * [barrierDismissible], which controls the behavior of the barrier when - /// tapped. - /// * [ModalBarrier], the widget that implements this feature. - final String? barrierLabel; - - /// {@macro flutter.widgets.modalRoute.maintainState} - final bool maintainState; - - /// {@macro flutter.widgets.pageRoute.fullscreenDialog} - final bool fullscreenDialog; - - @override - Route createRoute(BuildContext context) => _PageBasedPageRouteBuilder(this); -} - -// A page-based version of the [PageRouteBuilder]. -// -// This class gets its builder and settings directly from the [TransitionBuilderPage], -// so that its content updates accordingly to the [TransitionBuilderPage]. -class _PageBasedPageRouteBuilder extends PageRoute{ - _PageBasedPageRouteBuilder( - TransitionBuilderPage page, - ) : assert(page != null), - super(settings: page, fullscreenDialog: page.fullscreenDialog); - - TransitionBuilderPage get _page => settings as TransitionBuilderPage; - - @override - Duration get transitionDuration => _page.transitionDuration; - - @override - Duration get reverseTransitionDuration => _page.reverseTransitionDuration; - - @override - bool get opaque => _page.opaque; - - @override - bool get barrierDismissible => _page.barrierDismissible; - - @override - Color? get barrierColor => _page.barrierColor; - - @override - String? get barrierLabel => _page.barrierLabel; - - @override - bool get maintainState => _page.maintainState; - - @override - Widget buildPage(BuildContext context, Animation animation, Animation secondaryAnimation) { - return _page.pageBuilder(context, animation, secondaryAnimation); - } - - @override - Widget buildTransitions(BuildContext context, Animation animation, Animation secondaryAnimation, Widget child) { - return _page.transitionsBuilder(context, animation, secondaryAnimation, child); - } -} diff --git a/packages/flutter/test/cupertino/app_test.dart b/packages/flutter/test/cupertino/app_test.dart index 3abf497e02..9a1a03dad1 100644 --- a/packages/flutter/test/cupertino/app_test.dart +++ b/packages/flutter/test/cupertino/app_test.dart @@ -236,12 +236,12 @@ class SimpleNavigatorRouterDelegate extends RouterDelegate wit pages: >[ // We need at least two pages for the pop to propagate through. // Otherwise, the navigator will bubble the pop to the system navigator. - CupertinoPage( - builder: (BuildContext context) => const Text('base'), + const CupertinoPage( + child: Text('base'), ), CupertinoPage( key: ValueKey(routeInformation?.location), - builder: (BuildContext context) => builder(context, routeInformation), + child: builder(context, routeInformation), ) ], ); diff --git a/packages/flutter/test/cupertino/page_test.dart b/packages/flutter/test/cupertino/page_test.dart index 06feb6c614..fe0f4d4417 100644 --- a/packages/flutter/test/cupertino/page_test.dart +++ b/packages/flutter/test/cupertino/page_test.dart @@ -464,6 +464,19 @@ void main() { expect(find.text('Page 1'), findsNothing); expect(find.text('Page 2'), isOnstage); }); + + testWidgets('CupertinoPage does not lose its state when transitioning out', (WidgetTester tester) async { + final GlobalKey navigator = GlobalKey(); + await tester.pumpWidget(KeepsStateTestWidget(navigatorKey: navigator)); + expect(find.text('subpage'), findsOneWidget); + expect(find.text('home'), findsNothing); + + navigator.currentState.pop(); + await tester.pump(); + + expect(find.text('subpage'), findsOneWidget); + expect(find.text('home'), findsOneWidget); + }); } class RtlOverrideWidgetsDelegate extends LocalizationsDelegate { @@ -481,3 +494,38 @@ class RtlOverrideWidgetsLocalization implements WidgetsLocalizations { @override TextDirection get textDirection => TextDirection.rtl; } + +class KeepsStateTestWidget extends StatefulWidget { + const KeepsStateTestWidget({this.navigatorKey}); + + final Key navigatorKey; + + @override + State createState() => _KeepsStateTestWidgetState(); +} + +class _KeepsStateTestWidgetState extends State { + String _subpage = 'subpage'; + + @override + Widget build(BuildContext context) { + return CupertinoApp( + home: Navigator( + key: widget.navigatorKey, + pages: >[ + const CupertinoPage(child: Text('home')), + if (_subpage != null) CupertinoPage(child: Text(_subpage)), + ], + onPopPage: (Route route, dynamic result) { + if (!route.didPop(result)) { + return false; + } + setState(() { + _subpage = null; + }); + return true; + }, + ), + ); + } +} diff --git a/packages/flutter/test/cupertino/route_test.dart b/packages/flutter/test/cupertino/route_test.dart index c7a357658d..a2a2df3233 100644 --- a/packages/flutter/test/cupertino/route_test.dart +++ b/packages/flutter/test/cupertino/route_test.dart @@ -1382,12 +1382,10 @@ void main() { CupertinoPage( key: pageKey, title: 'title one', - builder: (BuildContext context) { - return CupertinoPageScaffold( - navigationBar: CupertinoNavigationBar(key: UniqueKey()), - child: const Text('first'), - ); - } + child: CupertinoPageScaffold( + navigationBar: CupertinoNavigationBar(key: UniqueKey()), + child: const Text('first'), + ), ), ]; await tester.pumpWidget( @@ -1406,12 +1404,10 @@ void main() { CupertinoPage( key: pageKey, title: 'title two', - builder: (BuildContext context) { - return CupertinoPageScaffold( - navigationBar: CupertinoNavigationBar(key: UniqueKey()), - child: const Text('second'), - ); - } + child: CupertinoPageScaffold( + navigationBar: CupertinoNavigationBar(key: UniqueKey()), + child: const Text('second'), + ), ), ]; @@ -1437,8 +1433,8 @@ void main() { final LocalKey pageKeyTwo = UniqueKey(); final TransitionDetector detector = TransitionDetector(); List> myPages = >[ - CupertinoPage(key: pageKeyOne, maintainState: false, builder: (BuildContext context) => const Text('first')), - CupertinoPage(key: pageKeyTwo, builder: (BuildContext context) => const Text('second')), + CupertinoPage(key: pageKeyOne, maintainState: false, child: const Text('first')), + CupertinoPage(key: pageKeyTwo, child: const Text('second')), ]; await tester.pumpWidget( buildNavigator( @@ -1454,8 +1450,8 @@ void main() { expect(find.text('second'), findsOneWidget); myPages = >[ - CupertinoPage(key: pageKeyOne, maintainState: true, builder: (BuildContext context) => const Text('first')), - CupertinoPage(key: pageKeyTwo, builder: (BuildContext context) => const Text('second')), + CupertinoPage(key: pageKeyOne, maintainState: true, child: const Text('first')), + CupertinoPage(key: pageKeyTwo, child: const Text('second')), ]; await tester.pumpWidget( diff --git a/packages/flutter/test/material/app_test.dart b/packages/flutter/test/material/app_test.dart index 232f869cb3..af03d72a42 100644 --- a/packages/flutter/test/material/app_test.dart +++ b/packages/flutter/test/material/app_test.dart @@ -1056,12 +1056,12 @@ class SimpleNavigatorRouterDelegate extends RouterDelegate wit pages: >[ // We need at least two pages for the pop to propagate through. // Otherwise, the navigator will bubble the pop to the system navigator. - MaterialPage( - builder: (BuildContext context) => const Text('base'), + const MaterialPage( + child: Text('base'), ), MaterialPage( key: ValueKey(routeInformation?.location), - builder: (BuildContext context) => builder(context, routeInformation), + child: builder(context, routeInformation), ) ], ); diff --git a/packages/flutter/test/material/page_test.dart b/packages/flutter/test/material/page_test.dart index 3bc6a377bd..d9861b6e16 100644 --- a/packages/flutter/test/material/page_test.dart +++ b/packages/flutter/test/material/page_test.dart @@ -841,7 +841,7 @@ void main() { final LocalKey pageKey = UniqueKey(); final TransitionDetector detector = TransitionDetector(); List> myPages = >[ - MaterialPage(key: pageKey, builder: (BuildContext context) => const Text('first')), + MaterialPage(key: pageKey, child: const Text('first')), ]; await tester.pumpWidget( buildNavigator( @@ -855,7 +855,7 @@ void main() { expect(find.text('first'), findsOneWidget); myPages = >[ - MaterialPage(key: pageKey, builder: (BuildContext context) => const Text('second')), + MaterialPage(key: pageKey, child: const Text('second')), ]; await tester.pumpWidget( @@ -877,8 +877,8 @@ void main() { final LocalKey pageKeyTwo = UniqueKey(); final TransitionDetector detector = TransitionDetector(); List> myPages = >[ - MaterialPage(key: pageKeyOne, maintainState: false, builder: (BuildContext context) => const Text('first')), - MaterialPage(key: pageKeyTwo, builder: (BuildContext context) => const Text('second')), + MaterialPage(key: pageKeyOne, maintainState: false, child: const Text('first')), + MaterialPage(key: pageKeyTwo, child: const Text('second')), ]; await tester.pumpWidget( buildNavigator( @@ -894,8 +894,8 @@ void main() { expect(find.text('second'), findsOneWidget); myPages = >[ - MaterialPage(key: pageKeyOne, maintainState: true, builder: (BuildContext context) => const Text('first')), - MaterialPage(key: pageKeyTwo, builder: (BuildContext context) => const Text('second')), + MaterialPage(key: pageKeyOne, maintainState: true, child: const Text('first')), + MaterialPage(key: pageKeyTwo, child: const Text('second')), ]; await tester.pumpWidget( @@ -912,6 +912,19 @@ void main() { expect(find.text('first', skipOffstage: false), findsOneWidget); expect(find.text('second'), findsOneWidget); }); + + testWidgets('MaterialPage does not lose its state when transitioning out', (WidgetTester tester) async { + final GlobalKey navigator = GlobalKey(); + await tester.pumpWidget(KeepsStateTestWidget(navigatorKey: navigator)); + expect(find.text('subpage'), findsOneWidget); + expect(find.text('home'), findsNothing); + + navigator.currentState.pop(); + await tester.pump(); + + expect(find.text('subpage'), findsOneWidget); + expect(find.text('home'), findsOneWidget); + }); } class TransitionDetector extends DefaultTransitionDelegate { @@ -957,3 +970,38 @@ Widget buildNavigator({ ), ); } + +class KeepsStateTestWidget extends StatefulWidget { + const KeepsStateTestWidget({this.navigatorKey}); + + final Key navigatorKey; + + @override + State createState() => _KeepsStateTestWidgetState(); +} + +class _KeepsStateTestWidgetState extends State { + String _subpage = 'subpage'; + + @override + Widget build(BuildContext context) { + return MaterialApp( + home: Navigator( + key: widget.navigatorKey, + pages: >[ + const MaterialPage(child: Text('home')), + if (_subpage != null) MaterialPage(child: Text(_subpage)), + ], + onPopPage: (Route route, dynamic result) { + if (!route.didPop(result)) { + return false; + } + setState(() { + _subpage = null; + }); + return true; + }, + ), + ); + } +} diff --git a/packages/flutter/test/widgets/app_test.dart b/packages/flutter/test/widgets/app_test.dart index 2336e510de..9cfd065420 100644 --- a/packages/flutter/test/widgets/app_test.dart +++ b/packages/flutter/test/widgets/app_test.dart @@ -366,12 +366,12 @@ class SimpleNavigatorRouterDelegate extends RouterDelegate wit pages: >[ // We need at least two pages for the pop to propagate through. // Otherwise, the navigator will bubble the pop to the system navigator. - MaterialPage( - builder: (BuildContext context) => const Text('base'), + const MaterialPage( + child: Text('base'), ), MaterialPage( key: ValueKey(routeInformation?.location), - builder: (BuildContext context) => builder(context, routeInformation), + child: builder(context, routeInformation), ) ], ); diff --git a/packages/flutter/test/widgets/navigator_test.dart b/packages/flutter/test/widgets/navigator_test.dart index d2870e6b00..4db588302d 100644 --- a/packages/flutter/test/widgets/navigator_test.dart +++ b/packages/flutter/test/widgets/navigator_test.dart @@ -520,9 +520,9 @@ void main() { // Regression Test for https://github.com/flutter/flutter/issues/61346. Widget buildNavigator() { return Navigator( - pages: >[ + pages: const >[ MaterialPage( - builder: (BuildContext context) => const Placeholder(), + child: Placeholder(), ) ], onPopPage: (Route route, dynamic result) => false, @@ -550,14 +550,14 @@ void main() { testWidgets('Pages update does update overlay correctly', (WidgetTester tester) async { // Regression Test for https://github.com/flutter/flutter/issues/64941. - List> pages = >[ + List> pages = const >[ MaterialPage( - key: const ValueKey(0), - builder: (BuildContext context) => const Text('page 0'), + key: ValueKey(0), + child: Text('page 0'), ), MaterialPage( - key: const ValueKey(1), - builder: (BuildContext context) => const Text('page 1'), + key: ValueKey(1), + child: Text('page 1'), ), ]; Widget buildNavigator() { @@ -577,10 +577,10 @@ void main() { expect(find.text('page 0'), findsNothing); // Removes the first page. - pages = >[ + pages = const >[ MaterialPage( - key: const ValueKey(1), - builder: (BuildContext context) => const Text('page 1'), + key: ValueKey(1), + child: Text('page 1'), ), ]; @@ -2509,18 +2509,13 @@ void main() { Animation primaryAnimationOfRouteThree; final GlobalKey navigator = GlobalKey(); List> myPages = >[ - CustomBuilderPage( + BuilderPage( key: const ValueKey('1'), name:'initial', - routeBuilder: (BuildContext context, RouteSettings settings) { - return PageRouteBuilder( - settings: settings, - pageBuilder: (_, Animation animation, Animation secondaryAnimation) { - secondaryAnimationOfRouteOne = secondaryAnimation; - primaryAnimationOfRouteOne = animation; - return const Text('initial'); - }, - ); + pageBuilder: (_, Animation animation, Animation secondaryAnimation) { + secondaryAnimationOfRouteOne = secondaryAnimation; + primaryAnimationOfRouteOne = animation; + return const Text('initial'); }, ), ]; @@ -2536,48 +2531,33 @@ void main() { expect(find.text('initial'), findsOneWidget); myPages = >[ - CustomBuilderPage( + BuilderPage( key: const ValueKey('1'), name:'initial', - routeBuilder: (BuildContext context, RouteSettings settings) { - return PageRouteBuilder( - settings: settings, - pageBuilder: (_, Animation animation, Animation secondaryAnimation) { - secondaryAnimationOfRouteOne = secondaryAnimation; - primaryAnimationOfRouteOne = animation; - return const Text('initial'); - }, - ); + pageBuilder: (_, Animation animation, Animation secondaryAnimation) { + secondaryAnimationOfRouteOne = secondaryAnimation; + primaryAnimationOfRouteOne = animation; + return const Text('initial'); }, ), - CustomBuilderPage( + BuilderPage( key: const ValueKey('2'), name:'second', - routeBuilder: (BuildContext context, RouteSettings settings) { - return PageRouteBuilder( - settings: settings, - pageBuilder: (_, Animation animation, Animation secondaryAnimation) { - secondaryAnimationOfRouteTwo = secondaryAnimation; - primaryAnimationOfRouteTwo = animation; - return const Text('second'); - }, - ); + pageBuilder: (_, Animation animation, Animation secondaryAnimation) { + secondaryAnimationOfRouteTwo = secondaryAnimation; + primaryAnimationOfRouteTwo = animation; + return const Text('second'); }, ), - CustomBuilderPage( + BuilderPage( key: const ValueKey('3'), name:'third', - routeBuilder: (BuildContext context, RouteSettings settings) { - return PageRouteBuilder( - settings: settings, - pageBuilder: (_, Animation animation, Animation secondaryAnimation) { - secondaryAnimationOfRouteThree = secondaryAnimation; - primaryAnimationOfRouteThree = animation; - return const Text('third'); - }, - ); + pageBuilder: (_, Animation animation, Animation secondaryAnimation) { + secondaryAnimationOfRouteThree = secondaryAnimation; + primaryAnimationOfRouteThree = animation; + return const Text('third'); }, - ) + ), ]; await tester.pumpWidget( @@ -2615,32 +2595,22 @@ void main() { // correctly. myPages = >[ - CustomBuilderPage( + BuilderPage( key: const ValueKey('1'), name:'initial', - routeBuilder: (BuildContext context, RouteSettings settings) { - return PageRouteBuilder( - settings: settings, - pageBuilder: (_, Animation animation, Animation secondaryAnimation) { - secondaryAnimationOfRouteOne = secondaryAnimation; - primaryAnimationOfRouteOne = animation; - return const Text('initial'); - }, - ); + pageBuilder: (_, Animation animation, Animation secondaryAnimation) { + secondaryAnimationOfRouteOne = secondaryAnimation; + primaryAnimationOfRouteOne = animation; + return const Text('initial'); }, ), - CustomBuilderPage( + BuilderPage( key: const ValueKey('2'), name:'second', - routeBuilder: (BuildContext context, RouteSettings settings) { - return PageRouteBuilder( - settings: settings, - pageBuilder: (_, Animation animation, Animation secondaryAnimation) { - secondaryAnimationOfRouteTwo = secondaryAnimation; - primaryAnimationOfRouteTwo = animation; - return const Text('second'); - }, - ); + pageBuilder: (_, Animation animation, Animation secondaryAnimation) { + secondaryAnimationOfRouteTwo = secondaryAnimation; + primaryAnimationOfRouteTwo = animation; + return const Text('second'); }, ), ]; @@ -2672,47 +2642,32 @@ void main() { Animation primaryAnimationOfRouteTwo; Animation secondaryAnimationOfRouteThree; Animation primaryAnimationOfRouteThree; - List> myPages = >[ - CustomBuilderPage( + List> myPages = >[ + BuilderPage( key: const ValueKey('1'), name:'initial', - routeBuilder: (BuildContext context, RouteSettings settings) { - return PageRouteBuilder( - settings: settings, - pageBuilder: (_, Animation animation, Animation secondaryAnimation) { - secondaryAnimationOfRouteOne = secondaryAnimation; - primaryAnimationOfRouteOne = animation; - return const Text('initial'); - }, - ); + pageBuilder: (_, Animation animation, Animation secondaryAnimation) { + secondaryAnimationOfRouteOne = secondaryAnimation; + primaryAnimationOfRouteOne = animation; + return const Text('initial'); }, ), - CustomBuilderPage( + BuilderPage( key: const ValueKey('2'), name:'second', - routeBuilder: (BuildContext context, RouteSettings settings) { - return PageRouteBuilder( - settings: settings, - pageBuilder: (_, Animation animation, Animation secondaryAnimation) { - secondaryAnimationOfRouteTwo = secondaryAnimation; - primaryAnimationOfRouteTwo = animation; - return const Text('second'); - }, - ); + pageBuilder: (_, Animation animation, Animation secondaryAnimation) { + secondaryAnimationOfRouteTwo = secondaryAnimation; + primaryAnimationOfRouteTwo = animation; + return const Text('second'); }, ), - CustomBuilderPage( + BuilderPage( key: const ValueKey('3'), name:'third', - routeBuilder: (BuildContext context, RouteSettings settings) { - return PageRouteBuilder( - settings: settings, - pageBuilder: (_, Animation animation, Animation secondaryAnimation) { - secondaryAnimationOfRouteThree = secondaryAnimation; - primaryAnimationOfRouteThree = animation; - return const Text('third'); - }, - ); + pageBuilder: (_, Animation animation, Animation secondaryAnimation) { + secondaryAnimationOfRouteThree = secondaryAnimation; + primaryAnimationOfRouteThree = animation; + return const Text('third'); }, ), ]; @@ -3512,3 +3467,17 @@ class NavigatorObservation { final String current; final String operation; } + +class BuilderPage extends Page { + const BuilderPage({LocalKey key, String name, this.pageBuilder}) : super(key: key, name: name); + + final RoutePageBuilder pageBuilder; + + @override + Route createRoute(BuildContext context) { + return PageRouteBuilder( + settings: this, + pageBuilder: pageBuilder, + ); + } +} diff --git a/packages/flutter/test/widgets/route_notification_messages_test.dart b/packages/flutter/test/widgets/route_notification_messages_test.dart index 46346e8e35..8aaf3e6563 100644 --- a/packages/flutter/test/widgets/route_notification_messages_test.dart +++ b/packages/flutter/test/widgets/route_notification_messages_test.dart @@ -119,11 +119,8 @@ void main() { await tester.pumpWidget(Directionality( textDirection: TextDirection.ltr, child: Navigator( - pages: >[ - TransitionBuilderPage( - name: '/', - pageBuilder: (BuildContext context, Animation animation, Animation secondaryAnimation) => const Placeholder(), - ), + pages: const >[ + TestPage(name: '/'), ], onPopPage: (Route route, void result) => false, ) @@ -134,15 +131,9 @@ void main() { await tester.pumpWidget(Directionality( textDirection: TextDirection.ltr, child: Navigator( - pages: >[ - TransitionBuilderPage( - name: '/', - pageBuilder: (BuildContext context, Animation animation, Animation secondaryAnimation) => const Placeholder(), - ), - TransitionBuilderPage( - name: '/abc', - pageBuilder: (BuildContext context, Animation animation, Animation secondaryAnimation) => const Placeholder(), - ), + pages: const >[ + TestPage(name: '/'), + TestPage(name: '/abc',), ], onPopPage: (Route route, void result) => false, ) @@ -371,3 +362,15 @@ class SimpleRouterDelegate extends RouterDelegate with ChangeN @override Widget build(BuildContext context) => builder(context, routeInformation); } + +class TestPage extends Page { + const TestPage({LocalKey key, String name}) : super(key: key, name: name); + + @override + Route createRoute(BuildContext context) { + return PageRouteBuilder( + settings: this, + pageBuilder: (BuildContext context, Animation animation, Animation secondaryAnimation) => const Placeholder(), + ); + } +} diff --git a/packages/flutter/test/widgets/router_test.dart b/packages/flutter/test/widgets/router_test.dart index e14f32fd74..9725ad8050 100644 --- a/packages/flutter/test/widgets/router_test.dart +++ b/packages/flutter/test/widgets/router_test.dart @@ -663,12 +663,12 @@ class SimpleNavigatorRouterDelegate extends RouterDelegate wit pages: >[ // We need at least two pages for the pop to propagate through. // Otherwise, the navigator will bubble the pop to the system navigator. - MaterialPage( - builder: (BuildContext context) => const Text('base'), + const MaterialPage( + child: Text('base'), ), MaterialPage( key: ValueKey(routeInformation?.location), - builder: (BuildContext context) => builder(context, routeInformation), + child: builder(context, routeInformation), ) ], ); diff --git a/packages/flutter/test/widgets/routes_test.dart b/packages/flutter/test/widgets/routes_test.dart index a53ec4d318..6517d1e486 100644 --- a/packages/flutter/test/widgets/routes_test.dart +++ b/packages/flutter/test/widgets/routes_test.dart @@ -542,142 +542,6 @@ void main() { expect(focusNode.hasPrimaryFocus, isTrue); }); - testWidgets('TransitionBuilderPage works', (WidgetTester tester) async { - final LocalKey pageKey = UniqueKey(); - final TransitionDetector detector = TransitionDetector(); - List> myPages = >[ - TransitionBuilderPage( - key: pageKey, - pageBuilder: (BuildContext context, Animation animation, Animation secondaryAnimation) { - return const Text('first'); - }, - ) - ]; - await tester.pumpWidget( - buildNavigator( - pages: myPages, - onPopPage: (Route route, dynamic result) => null, - transitionDelegate: detector, - ) - ); - - expect(detector.hasTransition, isFalse); - expect(find.text('first'), findsOneWidget); - - myPages = >[ - TransitionBuilderPage( - key: pageKey, - pageBuilder: (BuildContext context, Animation animation, Animation secondaryAnimation) { - return const Text('second'); - }, - ) - ]; - - await tester.pumpWidget( - buildNavigator( - pages: myPages, - onPopPage: (Route route, dynamic result) => null, - transitionDelegate: detector, - ) - ); - // There should be no transition because the page has the same key. - expect(detector.hasTransition, isFalse); - // The content does update. - expect(find.text('first'), findsNothing); - expect(find.text('second'), findsOneWidget); - - myPages = >[ - TransitionBuilderPage( - key: pageKey, - pageBuilder: (BuildContext context, Animation animation, Animation secondaryAnimation) { - return const Text('dummy'); - }, - transitionsBuilder: (BuildContext context, Animation animation, Animation secondaryAnimation, Widget child) { - // Purposely discard the input child. - return const Text('third'); - } - ) - ]; - - await tester.pumpWidget( - buildNavigator( - pages: myPages, - onPopPage: (Route route, dynamic result) => null, - transitionDelegate: detector, - ) - ); - - // There should be no transition because the page has the same key. - expect(detector.hasTransition, isFalse); - // Makes sure transitionsBuilder works. - expect(find.text('second'), findsNothing); - expect(find.text('dummy'), findsNothing); - expect(find.text('third'), findsOneWidget); - }); - - testWidgets('TransitionBuilderPage can toggle MaintainState', (WidgetTester tester) async { - final LocalKey pageKeyOne = UniqueKey(); - final LocalKey pageKeyTwo = UniqueKey(); - final TransitionDetector detector = TransitionDetector(); - List> myPages = >[ - TransitionBuilderPage( - key: pageKeyOne, - maintainState: false, - pageBuilder: (BuildContext context, Animation animation, Animation secondaryAnimation) { - return const Text('first'); - }, - ), - TransitionBuilderPage( - key: pageKeyTwo, - pageBuilder: (BuildContext context, Animation animation, Animation secondaryAnimation) { - return const Text('second'); - }, - ) - ]; - await tester.pumpWidget( - buildNavigator( - pages: myPages, - onPopPage: (Route route, dynamic result) => null, - transitionDelegate: detector, - ) - ); - - expect(detector.hasTransition, isFalse); - // Page one does not maintain state. - expect(find.text('first', skipOffstage: false), findsNothing); - expect(find.text('second'), findsOneWidget); - - myPages = >[ - TransitionBuilderPage( - key: pageKeyOne, - maintainState: true, - pageBuilder: (BuildContext context, Animation animation, Animation secondaryAnimation) { - return const Text('first'); - }, - ), - TransitionBuilderPage( - key: pageKeyTwo, - pageBuilder: (BuildContext context, Animation animation, Animation secondaryAnimation) { - return const Text('second'); - }, - ) - ]; - - await tester.pumpWidget( - buildNavigator( - pages: myPages, - onPopPage: (Route route, dynamic result) => null, - transitionDelegate: detector, - ) - ); - // There should be no transition because the page has the same key. - expect(detector.hasTransition, isFalse); - // Page one sets the maintain state to be true, its widget tree should be - // built. - expect(find.text('first', skipOffstage: false), findsOneWidget); - expect(find.text('second'), findsOneWidget); - }); - group('PageRouteBuilder', () { testWidgets('reverseTransitionDuration defaults to 300ms', (WidgetTester tester) async { // Default PageRouteBuilder reverse transition duration should be 300ms.