diff --git a/packages/flutter/lib/src/widgets/pages.dart b/packages/flutter/lib/src/widgets/pages.dart index 12da358cad..1a8d530ebc 100644 --- a/packages/flutter/lib/src/widgets/pages.dart +++ b/packages/flutter/lib/src/widgets/pages.dart @@ -175,9 +175,21 @@ class TransitionBuilderPage extends Page { 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} diff --git a/packages/flutter/lib/src/widgets/routes.dart b/packages/flutter/lib/src/widgets/routes.dart index 6bda3bb58e..26cc034c4c 100644 --- a/packages/flutter/lib/src/widgets/routes.dart +++ b/packages/flutter/lib/src/widgets/routes.dart @@ -24,6 +24,7 @@ import 'transitions.dart'; // Examples can assume: // dynamic routeObserver; +// NavigatorState navigator; const Color _kTransparent = Color(0x00000000); @@ -193,7 +194,6 @@ abstract class TransitionRoute extends OverlayRoute { } break; } - changedInternalState(); } @override @@ -201,16 +201,19 @@ abstract class TransitionRoute extends OverlayRoute { assert(!_transitionCompleter.isCompleted, 'Cannot install a $runtimeType after disposing it.'); _controller = createAnimationController(); assert(_controller != null, '$runtimeType.createAnimationController() returned null.'); - _animation = createAnimation(); + _animation = createAnimation() + ..addStatusListener(_handleStatusChanged); assert(_animation != null, '$runtimeType.createAnimation() returned null.'); super.install(); + if (_animation.isCompleted && overlayEntries.isNotEmpty) { + overlayEntries.first.opaque = opaque; + } } @override TickerFuture didPush() { assert(_controller != null, '$runtimeType.didPush called before calling install() or after calling dispose().'); assert(!_transitionCompleter.isCompleted, 'Cannot reuse a $runtimeType after disposing it.'); - _didPushOrReplace(); super.didPush(); return _controller.forward(); } @@ -219,7 +222,6 @@ abstract class TransitionRoute extends OverlayRoute { void didAdd() { assert(_controller != null, '$runtimeType.didPush called before calling install() or after calling dispose().'); assert(!_transitionCompleter.isCompleted, 'Cannot reuse a $runtimeType after disposing it.'); - _didPushOrReplace(); super.didAdd(); _controller.value = _controller.upperBound; } @@ -230,19 +232,9 @@ abstract class TransitionRoute extends OverlayRoute { assert(!_transitionCompleter.isCompleted, 'Cannot reuse a $runtimeType after disposing it.'); if (oldRoute is TransitionRoute) _controller.value = oldRoute._controller.value; - _didPushOrReplace(); super.didReplace(oldRoute); } - void _didPushOrReplace() { - _animation.addStatusListener(_handleStatusChanged); - // If the animation is already completed, _handleStatusChanged will not get - // a chance to set opaqueness of OverlayEntry. - if (_animation.isCompleted && overlayEntries.isNotEmpty) { - overlayEntries.first.opaque = opaque; - } - } - @override bool didPop(T result) { assert(_controller != null, '$runtimeType.didPop called before calling install() or after calling dispose().'); @@ -878,7 +870,7 @@ abstract class ModalRoute extends TransitionRoute with LocalHistoryRoute of(BuildContext context) { final _ModalScopeStatus widget = context.dependOnInheritedWidgetOfExactType<_ModalScopeStatus>(); @@ -958,7 +950,8 @@ abstract class ModalRoute extends TransitionRoute with LocalHistoryRoute extends TransitionRoute with LocalHistoryRoute Theme.of(navigator.context).backgroundColor; + /// ``` + /// + /// The [Navigator] causes the [ModalRoute]'s modal barrier overlay entry + /// to rebuild any time its dependencies change. + /// + /// {@end-tool} + /// /// See also: /// /// * [barrierDismissible], which controls the behavior of the barrier when /// tapped. /// * [ModalBarrier], the widget that implements this feature. - /// {@endtemplate} Color get barrierColor; /// {@template flutter.widgets.modalRoute.barrierLabel} @@ -1180,6 +1187,7 @@ abstract class ModalRoute extends TransitionRoute with LocalHistoryRoute extends TransitionRoute with LocalHistoryRoute extends TransitionRoute with LocalHistoryRoute _offstage; bool _offstage = false; set offstage(bool value) { @@ -1257,6 +1266,7 @@ abstract class ModalRoute extends TransitionRoute with LocalHistoryRoute extends TransitionRoute with LocalHistoryRoute !isFirst || willHandlePopInternally; // Internals diff --git a/packages/flutter/test/widgets/navigator_test.dart b/packages/flutter/test/widgets/navigator_test.dart index 4808d1118d..597d871d6d 100644 --- a/packages/flutter/test/widgets/navigator_test.dart +++ b/packages/flutter/test/widgets/navigator_test.dart @@ -1208,7 +1208,50 @@ void main() { expect(log, ['building B', 'building C', 'found C', 'building D']); key.currentState.pop(); await tester.pumpAndSettle(const Duration(milliseconds: 10)); - expect(log, ['building B', 'building C', 'found C', 'building D', 'building C', 'found C']); + expect(log, ['building B', 'building C', 'found C', 'building D']); + }); + + testWidgets('Routes don\'t rebuild just because their animations ended', (WidgetTester tester) async { + final GlobalKey key = GlobalKey(); + final List log = []; + Route nextRoute = PageRouteBuilder( + pageBuilder: (BuildContext context, Animation animation, Animation secondaryAnimation) { + log.add('building page 1 - ${ModalRoute.of(context).canPop}'); + return const Placeholder(); + }, + ); + await tester.pumpWidget(MaterialApp( + navigatorKey: key, + onGenerateRoute: (RouteSettings settings) { + assert(nextRoute != null); + final Route result = nextRoute; + nextRoute = null; + return result; + }, + )); + expect(log, ['building page 1 - false']); + key.currentState.pushReplacement(PageRouteBuilder( + pageBuilder: (BuildContext context, Animation animation, Animation secondaryAnimation) { + log.add('building page 2 - ${ModalRoute.of(context).canPop}'); + return const Placeholder(); + }, + )); + expect(log, ['building page 1 - false']); + await tester.pump(); + expect(log, ['building page 1 - false', 'building page 2 - false']); + await tester.pump(const Duration(milliseconds: 150)); + expect(log, ['building page 1 - false', 'building page 2 - false']); + key.currentState.pushReplacement(PageRouteBuilder( + pageBuilder: (BuildContext context, Animation animation, Animation secondaryAnimation) { + log.add('building page 3 - ${ModalRoute.of(context).canPop}'); + return const Placeholder(); + }, + )); + expect(log, ['building page 1 - false', 'building page 2 - false']); + await tester.pump(); + expect(log, ['building page 1 - false', 'building page 2 - false', 'building page 3 - false']); + await tester.pump(const Duration(milliseconds: 200)); + expect(log, ['building page 1 - false', 'building page 2 - false', 'building page 3 - false']); }); testWidgets('route semantics', (WidgetTester tester) async { diff --git a/packages/flutter/test/widgets/page_route_builder_test.dart b/packages/flutter/test/widgets/page_route_builder_test.dart new file mode 100644 index 0000000000..f910bf4e09 --- /dev/null +++ b/packages/flutter/test/widgets/page_route_builder_test.dart @@ -0,0 +1,95 @@ +// Copyright 2014 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. + +// @dart = 2.8 + +import 'package:flutter/material.dart'; +import 'package:flutter/widgets.dart'; +import 'package:flutter_test/flutter_test.dart'; + +class TestPage extends StatelessWidget { + @override + Widget build(BuildContext context) { + return MaterialApp( + title: 'Test', + theme: ThemeData( + primarySwatch: Colors.blue, + ), + home: HomePage(), + ); + } +} + +class HomePage extends StatefulWidget { + @override + State createState() => _HomePageState(); +} + +class _HomePageState extends State { + void _presentModalPage() { + Navigator.of(context).push(PageRouteBuilder( + transitionDuration: const Duration(milliseconds: 300), + barrierColor: Colors.black54, + opaque: false, + pageBuilder: (BuildContext context, _, __) { + return ModalPage(); + }, + )); + } + + @override + Widget build(BuildContext context) { + return Scaffold( + body: const Center( + child: Text('Test Home'), + ), + floatingActionButton: FloatingActionButton( + onPressed: _presentModalPage, + child: const Icon(Icons.add), + ), + ); + } +} + +class ModalPage extends StatelessWidget { + @override + Widget build(BuildContext context) { + return Material( + type: MaterialType.transparency, + child: SafeArea( + child: Stack( + children: [ + InkWell( + highlightColor: Colors.transparent, + splashColor: Colors.transparent, + onTap: () { + Navigator.of(context).pop(); + }, + child: const SizedBox.expand(), + ), + Align( + alignment: Alignment.bottomCenter, + child: Container( + height: 150, + color: Colors.teal, + ), + ), + ], + ), + ), + ); + } +} + +void main() { + testWidgets('Barriers show when using PageRouteBuilder', (WidgetTester tester) async { + await tester.pumpWidget(TestPage()); + await tester.tap(find.byType(FloatingActionButton)); + await tester.pumpAndSettle(); + await expectLater( + find.byType(TestPage), + matchesGoldenFile('page_route_builder.barrier.png'), + ); + }); +}