From 8219ab61abfc85f996344253597f385bf4ab4a21 Mon Sep 17 00:00:00 2001 From: Adam Barth Date: Wed, 27 Jul 2016 13:49:30 -0700 Subject: [PATCH] Don't rebuild routes on the second animation frame (#5097) Previously we would rebuild every route on the second animation frame to rejigger the offstage bit and the animations. Now we build the page once and update the offstage bit in place and update the animations using ProxyAnimations. --- .../lib/src/material/bottom_sheet.dart | 8 +- packages/flutter/lib/src/widgets/routes.dart | 79 ++++++++++++------- packages/flutter/test/material/app_test.dart | 45 +++++++++++ 3 files changed, 100 insertions(+), 32 deletions(-) diff --git a/packages/flutter/lib/src/material/bottom_sheet.dart b/packages/flutter/lib/src/material/bottom_sheet.dart index 45743e7466..db63475976 100644 --- a/packages/flutter/lib/src/material/bottom_sheet.dart +++ b/packages/flutter/lib/src/material/bottom_sheet.dart @@ -186,7 +186,7 @@ class _ModalBottomSheetState extends State<_ModalBottomSheet> { child: new CustomSingleChildLayout( delegate: new _ModalBottomSheetLayout(config.route.animation.value), child: new BottomSheet( - animationController: config.route.animation, + animationController: config.route._animationController, onClosing: () => Navigator.pop(context), builder: config.route.builder ) @@ -215,9 +215,13 @@ class _ModalBottomSheetRoute extends PopupRoute { @override Color get barrierColor => Colors.black54; + AnimationController _animationController; + @override AnimationController createAnimationController() { - return BottomSheet.createAnimationController(); + assert(_animationController == null); + _animationController = BottomSheet.createAnimationController(); + return _animationController; } @override diff --git a/packages/flutter/lib/src/widgets/routes.dart b/packages/flutter/lib/src/widgets/routes.dart index c5bb5c2bab..3be41cf2ae 100644 --- a/packages/flutter/lib/src/widgets/routes.dart +++ b/packages/flutter/lib/src/widgets/routes.dart @@ -207,10 +207,10 @@ abstract class TransitionRoute extends OverlayRoute { TrainHoppingAnimation newAnimation; newAnimation = new TrainHoppingAnimation( current.currentTrain, - nextRoute.animation, + nextRoute._animation, onSwitchedTrain: () { assert(_forwardAnimation.parent == newAnimation); - assert(newAnimation.currentTrain == nextRoute.animation); + assert(newAnimation.currentTrain == nextRoute._animation); _forwardAnimation.parent = newAnimation.currentTrain; newAnimation.dispose(); } @@ -218,10 +218,10 @@ abstract class TransitionRoute extends OverlayRoute { _forwardAnimation.parent = newAnimation; current.dispose(); } else { - _forwardAnimation.parent = new TrainHoppingAnimation(current, nextRoute.animation); + _forwardAnimation.parent = new TrainHoppingAnimation(current, nextRoute._animation); } } else { - _forwardAnimation.parent = nextRoute.animation; + _forwardAnimation.parent = nextRoute._animation; } } else { _forwardAnimation.parent = kAlwaysDismissedAnimation; @@ -367,10 +367,12 @@ class _ModalScopeStatus extends InheritedWidget { class _ModalScope extends StatefulWidget { _ModalScope({ Key key, - this.route + this.route, + this.page }) : super(key: key); final ModalRoute route; + final Widget page; @override _ModalScopeState createState() => new _ModalScopeState(); @@ -408,31 +410,30 @@ class _ModalScopeState extends State<_ModalScope> { @override Widget build(BuildContext context) { - Widget contents = new PageStorage( - key: config.route._subtreeKey, - bucket: config.route._storageBucket, - child: new _ModalScopeStatus( - route: config.route, - isCurrent: config.route.isCurrent, - child: config.route.buildPage(context, config.route.animation, config.route.forwardAnimation) - ) - ); - if (config.route.offstage) { - contents = new OffStage(child: contents); - } else { - contents = new IgnorePointer( - ignoring: config.route.animation?.status == AnimationStatus.reverse, - child: config.route.buildTransitions( - context, - config.route.animation, - config.route.forwardAnimation, - new RepaintBoundary(child: contents) - ) - ); - } return new Focus( key: config.route.focusKey, - child: contents + child: new OffStage( + offstage: config.route.offstage, + child: new IgnorePointer( + ignoring: config.route.animation?.status == AnimationStatus.reverse, + child: config.route.buildTransitions( + context, + config.route.animation, + config.route.forwardAnimation, + new RepaintBoundary( + child: new PageStorage( + key: config.route._subtreeKey, + bucket: config.route._storageBucket, + child: new _ModalScopeStatus( + route: config.route, + isCurrent: config.route.isCurrent, + child: config.page + ) + ) + ) + ) + ) + ) ); } } @@ -521,6 +522,13 @@ abstract class ModalRoute extends TransitionRoute with LocalHistoryRoute new GlobalObjectKey(this); + @override + void install(OverlayEntry insertionPoint) { + super.install(insertionPoint); + _animationProxy = new ProxyAnimation(super.animation); + _forwardAnimationProxy = new ProxyAnimation(super.forwardAnimation); + } + @override void didPush() { if (!settings.isInitialRoute) { @@ -573,11 +581,21 @@ abstract class ModalRoute extends TransitionRoute with LocalHistoryRoute _subtreeKey.currentContext; + @override + Animation get animation => _animationProxy; + ProxyAnimation _animationProxy; + + @override + Animation get forwardAnimation => _forwardAnimationProxy; + ProxyAnimation _forwardAnimationProxy; + // Internals @@ -615,8 +633,9 @@ abstract class ModalRoute extends TransitionRoute with LocalHistoryRoute{ + '/next': (BuildContext context) { + return new Builder( + builder: (BuildContext context) { + ++buildCounter; + return new Container(); + } + ); + } + } + ) + ); + + expect(buildCounter, 0); + await tester.tap(find.text('X')); + expect(buildCounter, 0); + await tester.pump(); + expect(buildCounter, 1); + await tester.pump(const Duration(milliseconds: 10)); + expect(buildCounter, 1); + await tester.pump(const Duration(milliseconds: 10)); + expect(buildCounter, 1); + await tester.pump(const Duration(milliseconds: 10)); + expect(buildCounter, 1); + await tester.pump(const Duration(milliseconds: 10)); + expect(buildCounter, 1); + await tester.pump(const Duration(seconds: 1)); + expect(buildCounter, 2); + }); + }