From 0b098ee2e563d353fae88b81f3c1c8a5af366080 Mon Sep 17 00:00:00 2001 From: Adam Barth Date: Tue, 19 Jan 2016 16:48:45 -0800 Subject: [PATCH 1/4] Move Scaffold over to using AnimationController Also, clean up the class hierarchy for AnimationController now that we've renamed progress to value. That means everything in the hierarchy now has a value, include Watchable. This patch renames Watchable to Animated, which lets us use that type almost everywhere. I've added some ducktape to modal bottom sheets to avoid having to refactor all of Navigator to use AnimationController. I'll remove the ducktape in the next patch. --- .../lib/demo/page_selector_demo.dart | 6 +- .../lib/demo/progress_indicator_demo.dart | 14 +-- examples/widgets/smooth_resize.dart | 10 +- packages/flutter/lib/src/animation/tween.dart | 80 ++++++++-------- .../lib/src/material/bottom_sheet.dart | 58 ++++++++---- packages/flutter/lib/src/material/dialog.dart | 2 +- .../flutter/lib/src/material/dropdown.dart | 4 +- .../flutter/lib/src/material/popup_menu.dart | 2 +- .../lib/src/material/progress_indicator.dart | 53 +++++------ .../flutter/lib/src/material/scaffold.dart | 93 +++++++++++-------- .../flutter/lib/src/material/snack_bar.dart | 71 +++++++------- packages/flutter/lib/src/material/tabs.dart | 36 +++---- .../flutter/lib/src/material/tooltip.dart | 4 +- .../src/widgets/enter_exit_transition.dart | 4 +- .../flutter/lib/src/widgets/transitions.dart | 77 ++++++++------- 15 files changed, 282 insertions(+), 232 deletions(-) diff --git a/examples/material_gallery/lib/demo/page_selector_demo.dart b/examples/material_gallery/lib/demo/page_selector_demo.dart index c9a50ee3f9..c6611b64c2 100644 --- a/examples/material_gallery/lib/demo/page_selector_demo.dart +++ b/examples/material_gallery/lib/demo/page_selector_demo.dart @@ -12,9 +12,9 @@ class PageSelectorDemo extends StatelessComponent { final ColorTween _previousColor = new ColorTween(begin: color, end: Colors.transparent); final TabBarSelectionState selection = TabBarSelection.of(context); - Animation animation = new CurvedAnimation(parent: selection.animation, curve: Curves.ease); - return new AnimationWatchingBuilder( - watchable: animation, + CurvedAnimation animation = new CurvedAnimation(parent: selection.animation, curve: Curves.ease); + return new AnimatedBuilder( + animation: animation, builder: (BuildContext context) { Color background = selection.value == iconName ? _selectedColor.end : _selectedColor.begin; if (selection.valueIsChanging) { diff --git a/examples/material_gallery/lib/demo/progress_indicator_demo.dart b/examples/material_gallery/lib/demo/progress_indicator_demo.dart index 6e95552aac..2ca0b76d19 100644 --- a/examples/material_gallery/lib/demo/progress_indicator_demo.dart +++ b/examples/material_gallery/lib/demo/progress_indicator_demo.dart @@ -27,7 +27,7 @@ class _ProgressIndicatorDemoState extends State { }); } - Animation animation; + Animated animation; AnimationController controller; void handleTap() { @@ -55,19 +55,19 @@ class _ProgressIndicatorDemoState extends State { ), new LinearProgressIndicator(), new LinearProgressIndicator(), - new LinearProgressIndicator(value: animation.progress), + new LinearProgressIndicator(value: animation.value), new CircularProgressIndicator(), new SizedBox( width: 20.0, height: 20.0, - child: new CircularProgressIndicator(value: animation.progress) + child: new CircularProgressIndicator(value: animation.value) ), new SizedBox( width: 50.0, height: 30.0, - child: new CircularProgressIndicator(value: animation.progress) + child: new CircularProgressIndicator(value: animation.value) ), - new Text("${(animation.progress * 100.0).toStringAsFixed(1)}%" + (controller.isAnimating ? '' : ' (paused)')) + new Text("${(animation.value * 100.0).toStringAsFixed(1)}%" + (controller.isAnimating ? '' : ' (paused)')) ]; return new Column( children: indicators @@ -87,8 +87,8 @@ class _ProgressIndicatorDemoState extends State { behavior: HitTestBehavior.opaque, child: new Container( padding: const EdgeDims.symmetric(vertical: 12.0, horizontal: 8.0), - child: new AnimationWatchingBuilder( - watchable: animation, + child: new AnimatedBuilder( + animation: animation, builder: buildIndicators ) ) diff --git a/examples/widgets/smooth_resize.dart b/examples/widgets/smooth_resize.dart index 2d786860ce..48a8033b99 100644 --- a/examples/widgets/smooth_resize.dart +++ b/examples/widgets/smooth_resize.dart @@ -33,14 +33,14 @@ class CardTransition extends StatelessComponent { }); final Widget child; - final Animation animation; + final Animated animation; final Evaluatable x; final Evaluatable opacity; final Evaluatable scale; Widget build(BuildContext context) { - return new AnimationWatchingBuilder( - watchable: animation, + return new AnimatedBuilder( + animation: animation, builder: (BuildContext context) { double currentScale = scale.evaluate(animation); Matrix4 transform = new Matrix4.identity() @@ -62,7 +62,7 @@ class SmoothBlockState extends State { double _height = 100.0; - Widget _handleEnter(Animation animation, Widget child) { + Widget _handleEnter(Animated animation, Widget child) { return new CardTransition( x: new Tween(begin: -200.0, end: 0.0), opacity: new Tween(begin: 0.0, end: 1.0), @@ -72,7 +72,7 @@ class SmoothBlockState extends State { ); } - Widget _handleExit(Animation animation, Widget child) { + Widget _handleExit(Animated animation, Widget child) { return new CardTransition( x: new Tween(begin: 0.0, end: 200.0), opacity: new Tween(begin: 1.0, end: 0.0), diff --git a/packages/flutter/lib/src/animation/tween.dart b/packages/flutter/lib/src/animation/tween.dart index 046a2d36f2..3b620884b7 100644 --- a/packages/flutter/lib/src/animation/tween.dart +++ b/packages/flutter/lib/src/animation/tween.dart @@ -13,16 +13,16 @@ import 'forces.dart'; import 'listener_helpers.dart'; import 'simulation_stepper.dart'; -abstract class Watchable { - const Watchable(); +abstract class Animated { + const Animated(); - /// Calls the listener every time the progress of the performance changes. + /// Calls the listener every time the value of the animation changes. void addListener(VoidCallback listener); - /// Stop calling the listener every time the progress of the performance changes. + /// Stop calling the listener every time the value of the animation changes. void removeListener(VoidCallback listener); - /// Calls listener every time the status of the performance changes. + /// Calls listener every time the status of the animation changes. void addStatusListener(PerformanceStatusListener listener); - /// Stops calling the listener every time the status of the performance changes. + /// Stops calling the listener every time the status of the animation changes. void removeStatusListener(PerformanceStatusListener listener); /// The current status of this animation. @@ -31,6 +31,9 @@ abstract class Watchable { /// The current direction of the animation. AnimationDirection get direction; + /// The current value of the animation. + T get value; + /// Whether this animation is stopped at the beginning. bool get isDismissed => status == PerformanceStatus.dismissed; @@ -77,8 +80,8 @@ abstract class Watchable { } } -abstract class ProxyWatchableMixin implements Watchable { - Watchable get parent; +abstract class ProxyAnimatedMixin { + Animated get parent; void addListener(VoidCallback listener) => parent.addListener(listener); void removeListener(VoidCallback listener) => parent.removeListener(listener); @@ -92,37 +95,30 @@ abstract class ProxyWatchableMixin implements Watchable { abstract class Evaluatable { const Evaluatable(); - T evaluate(Animation animation); + T evaluate(Animated animation); - WatchableValue watch(Animation parent) { - return new WatchableValue(parent: parent, evaluatable: this); + Animated watch(Animated parent) { + return new _AnimatedEvaluation(parent, this); } } -class WatchableValue extends Watchable with ProxyWatchableMixin { - WatchableValue({ this.parent, this.evaluatable }); +class _AnimatedEvaluation extends Animated with ProxyAnimatedMixin { + _AnimatedEvaluation(this.parent, this._evaluatable); - final Animation parent; - final Evaluatable evaluatable; + /// The animation from which this value is derived. + final Animated parent; - T get value => evaluatable.evaluate(parent); + final Evaluatable _evaluatable; + + T get value => _evaluatable.evaluate(parent); } -abstract class Animation extends Watchable { - /// The current progress of this animation (a value from 0.0 to 1.0). - double get progress; - - String toStringDetails() { - return '${super.toStringDetails()} ${progress.toStringAsFixed(3)}'; - } -} - -class AnimationController extends Animation +class AnimationController extends Animated with EagerListenerMixin, LocalPerformanceListenersMixin, LocalPerformanceStatusListenersMixin { - AnimationController({ this.duration, double progress, this.debugLabel }) { + AnimationController({ this.duration, double value, this.debugLabel }) { _timeline = new SimulationStepper(_tick); - if (progress != null) - _timeline.value = progress.clamp(0.0, 1.0); + if (value != null) + _timeline.value = value.clamp(0.0, 1.0); } /// A label that is used in the [toString] output. Intended to aid with @@ -132,7 +128,7 @@ class AnimationController extends Animation /// Returns a [Animation] for this performance, /// so that a pointer to this object can be passed around without /// allowing users of that pointer to mutate the Performance state. - Animation get view => this; + Animated get view => this; /// The length of time this performance should last. Duration duration; @@ -144,8 +140,8 @@ class AnimationController extends Animation /// The progress of this performance along the timeline. /// /// Note: Setting this value stops the current animation. - double get progress => _timeline.value.clamp(0.0, 1.0); - void set progress(double t) { + double get value => _timeline.value.clamp(0.0, 1.0); + void set value(double t) { stop(); _timeline.value = t.clamp(0.0, 1.0); _checkStatusChanged(); @@ -155,9 +151,9 @@ class AnimationController extends Animation bool get isAnimating => _timeline.isAnimating; PerformanceStatus get status { - if (!isAnimating && progress == 1.0) + if (!isAnimating && value == 1.0) return PerformanceStatus.completed; - if (!isAnimating && progress == 0.0) + if (!isAnimating && value == 0.0) return PerformanceStatus.dismissed; return _direction == AnimationDirection.forward ? PerformanceStatus.forward : @@ -200,7 +196,7 @@ class AnimationController extends Animation Future fling({double velocity: 1.0, Force force}) { force ??= kDefaultSpringForce; _direction = velocity < 0.0 ? AnimationDirection.reverse : AnimationDirection.forward; - return _timeline.animateWith(force.release(progress, velocity)); + return _timeline.animateWith(force.release(value, velocity)); } /// Starts running this animation in the forward direction, and @@ -234,7 +230,7 @@ class AnimationController extends Animation String toStringDetails() { String paused = _timeline.isAnimating ? '' : '; paused'; String label = debugLabel == null ? '' : '; for $debugLabel'; - String more = super.toStringDetails(); + String more = '${super.toStringDetails()} ${value.toStringAsFixed(3)}'; return '$more$paused$label'; } } @@ -261,14 +257,14 @@ class _RepeatingSimulation extends Simulation { bool isDone(double timeInSeconds) => false; } -class CurvedAnimation extends Animation with ProxyWatchableMixin { +class CurvedAnimation extends Animated with ProxyAnimatedMixin { CurvedAnimation({ this.parent, this.curve, this.reverseCurve }) { assert(parent != null); assert(curve != null); parent.addStatusListener(_handleStatusChanged); } - final Animation parent; + final Animated parent; /// The curve to use in the forward direction. Curve curve; @@ -300,11 +296,11 @@ class CurvedAnimation extends Animation with ProxyWatchableMixin { } } - double get progress { + double get value { final bool useForwardCurve = reverseCurve == null || (_curveDirection ?? parent.direction) == AnimationDirection.forward; Curve activeCurve = useForwardCurve ? curve : reverseCurve; - double t = parent.progress; + double t = parent.value; if (activeCurve == null) return t; if (t == 0.0 || t == 1.0) { @@ -327,10 +323,10 @@ class Tween extends Evaluatable { /// Returns the value this variable has at the given animation clock value. T lerp(double t) => begin + (end - begin) * t; - T evaluate(Animation animation) { + T evaluate(Animated animation) { if (end == null) return begin; - double t = animation.progress; + double t = animation.value; if (t == 0.0) return begin; if (t == 1.0) diff --git a/packages/flutter/lib/src/material/bottom_sheet.dart b/packages/flutter/lib/src/material/bottom_sheet.dart index 590d2d6e18..ff5f42ca50 100644 --- a/packages/flutter/lib/src/material/bottom_sheet.dart +++ b/packages/flutter/lib/src/material/bottom_sheet.dart @@ -21,24 +21,24 @@ const Color _kBarrierColor = Colors.black54; class BottomSheet extends StatefulComponent { BottomSheet({ Key key, - this.performance, + this.animationController, this.onClosing, this.builder }) : super(key: key) { assert(onClosing != null); } - /// The performance that controls the bottom sheet's position. The BottomSheet - /// widget will manipulate the position of this performance, it is not just a + /// The animation that controls the bottom sheet's position. The BottomSheet + /// widget will manipulate the position of this animation, it is not just a /// passive observer. - final Performance performance; + final AnimationController animationController; final VoidCallback onClosing; final WidgetBuilder builder; _BottomSheetState createState() => new _BottomSheetState(); - static Performance createPerformanceController() { - return new Performance( + static AnimationController createAnimationController() { + return new AnimationController( duration: _kBottomSheetDuration, debugLabel: 'BottomSheet' ); @@ -54,12 +54,12 @@ class _BottomSheetState extends State { return renderBox.size.height; } - bool get _dismissUnderway => config.performance.direction == AnimationDirection.reverse; + bool get _dismissUnderway => config.animationController.direction == AnimationDirection.reverse; void _handleDragUpdate(double delta) { if (_dismissUnderway) return; - config.performance.progress -= delta / (_childHeight ?? delta); + config.animationController.value -= delta / (_childHeight ?? delta); } void _handleDragEnd(Offset velocity) { @@ -67,14 +67,14 @@ class _BottomSheetState extends State { return; if (velocity.dy > _kMinFlingVelocity) { double flingVelocity = -velocity.dy / _childHeight; - config.performance.fling(velocity: flingVelocity); + config.animationController.fling(velocity: flingVelocity); if (flingVelocity < 0.0) config.onClosing(); - } else if (config.performance.progress < _kCloseProgressThreshold) { - config.performance.fling(velocity: -1.0); + } else if (config.animationController.value < _kCloseProgressThreshold) { + config.animationController.fling(velocity: -1.0); config.onClosing(); } else { - config.performance.forward(); + config.animationController.forward(); } } @@ -129,17 +129,41 @@ class _ModalBottomSheet extends StatefulComponent { } class _ModalBottomSheetState extends State<_ModalBottomSheet> { + // TODO(abarth): Delete _controllerPerformanceAdaptor when navigator uses + // AnimationController and friends. + AnimationController _controllerPerformanceAdaptor; + + void initState() { + super.initState(); + _controllerPerformanceAdaptor = new AnimationController(); + _updateControllerPerformanceAdaptor(); + } + + void didUpdateConfig(_ModalBottomSheet oldConfig) { + if (config.route.performance != oldConfig.route.performance) + _updateControllerPerformanceAdaptor(); + } + + void _updateControllerPerformanceAdaptor() { + Performance performance = config.route.performance; + _controllerPerformanceAdaptor + ..duration = performance.duration + ..value = performance.progress; + if (performance.isAnimating) + _controllerPerformanceAdaptor.play(performance.direction); + } + Widget build(BuildContext context) { return new GestureDetector( onTap: () => Navigator.pop(context), - child: new BuilderTransition( - performance: config.route.performance, + child: new AnimatedBuilder( + animation: _controllerPerformanceAdaptor, builder: (BuildContext context) { return new ClipRect( child: new CustomOneChildLayout( delegate: new _ModalBottomSheetLayout(config.route.performance.progress), child: new BottomSheet( - performance: config.route.performance, + animationController: _controllerPerformanceAdaptor, onClosing: () => Navigator.pop(context), builder: config.route.builder ) @@ -163,8 +187,8 @@ class _ModalBottomSheetRoute extends PopupRoute { bool get barrierDismissable => true; Color get barrierColor => Colors.black54; - Performance createPerformanceController() { - return BottomSheet.createPerformanceController(); + AnimationController createAnimationController() { + return BottomSheet.createAnimationController(); } Widget buildPage(BuildContext context, PerformanceView performance, PerformanceView forwardPerformance) { diff --git a/packages/flutter/lib/src/material/dialog.dart b/packages/flutter/lib/src/material/dialog.dart index 11991973d7..4ad90222b9 100644 --- a/packages/flutter/lib/src/material/dialog.dart +++ b/packages/flutter/lib/src/material/dialog.dart @@ -133,7 +133,7 @@ class _DialogRoute extends PopupRoute { } Widget buildTransitions(BuildContext context, PerformanceView performance, PerformanceView forwardPerformance, Widget child) { - return new FadeTransition( + return new OldFadeTransition( performance: performance, opacity: new AnimatedValue(0.0, end: 1.0, curve: Curves.easeOut), child: child diff --git a/packages/flutter/lib/src/material/dropdown.dart b/packages/flutter/lib/src/material/dropdown.dart index 78db089171..6d8e606be3 100644 --- a/packages/flutter/lib/src/material/dropdown.dart +++ b/packages/flutter/lib/src/material/dropdown.dart @@ -87,7 +87,7 @@ class _DropDownMenu extends StatusTransitionComponent { final double end = (start + 1.5 * unit).clamp(0.0, 1.0); opacity = new AnimatedValue(0.0, end: 1.0, curve: new Interval(start, end), reverseCurve: const Interval(0.75, 1.0)); } - children.add(new FadeTransition( + children.add(new OldFadeTransition( performance: route.performance, opacity: opacity, child: new InkWell( @@ -120,7 +120,7 @@ class _DropDownMenu extends StatusTransitionComponent { reverseCurve: const Interval(0.0, 0.001) ); - return new FadeTransition( + return new OldFadeTransition( performance: route.performance, opacity: menuOpacity, child: new BuilderTransition( diff --git a/packages/flutter/lib/src/material/popup_menu.dart b/packages/flutter/lib/src/material/popup_menu.dart index 6ef058839a..2e72a1b898 100644 --- a/packages/flutter/lib/src/material/popup_menu.dart +++ b/packages/flutter/lib/src/material/popup_menu.dart @@ -61,7 +61,7 @@ class _PopupMenu extends StatelessComponent { for (int i = 0; i < route.items.length; ++i) { double start = (i + 1) * unit; double end = (start + 1.5 * unit).clamp(0.0, 1.0); - children.add(new FadeTransition( + children.add(new OldFadeTransition( performance: route.performance, opacity: new AnimatedValue(0.0, end: 1.0, curve: new Interval(start, end)), child: new InkWell( diff --git a/packages/flutter/lib/src/material/progress_indicator.dart b/packages/flutter/lib/src/material/progress_indicator.dart index fcf966e179..4fbc826b0f 100644 --- a/packages/flutter/lib/src/material/progress_indicator.dart +++ b/packages/flutter/lib/src/material/progress_indicator.dart @@ -27,7 +27,7 @@ abstract class ProgressIndicator extends StatefulComponent { Color _getBackgroundColor(BuildContext context) => Theme.of(context).primarySwatch[200]; Color _getValueColor(BuildContext context) => Theme.of(context).primaryColor; - Widget _buildIndicator(BuildContext context, double performanceValue); + Widget _buildIndicator(BuildContext context, double animationValue); _ProgressIndicatorState createState() => new _ProgressIndicatorState(); @@ -38,36 +38,33 @@ abstract class ProgressIndicator extends StatefulComponent { } class _ProgressIndicatorState extends State { - - ValuePerformance _performance; + Animated _animation; + AnimationController _controller; void initState() { super.initState(); - _performance = new ValuePerformance( - variable: new AnimatedValue(0.0, end: 1.0, curve: Curves.ease), + _controller = new AnimationController( duration: const Duration(milliseconds: 1500) - ); - _performance.addStatusListener((PerformanceStatus status) { + )..addStatusListener((PerformanceStatus status) { if (status == PerformanceStatus.completed) _restartAnimation(); - }); - _performance.play(); + })..forward(); + _animation = new CurvedAnimation(parent: _controller, curve: Curves.ease); } void _restartAnimation() { - _performance.progress = 0.0; - _performance.play(); + _controller.value = 0.0; + _controller.forward(); } Widget build(BuildContext context) { if (config.value != null) - return config._buildIndicator(context, _performance.value); + return config._buildIndicator(context, _animation.value); - return new BuilderTransition( - variables: >[_performance.variable], - performance: _performance.view, + return new AnimatedBuilder( + animation: _animation, builder: (BuildContext context) { - return config._buildIndicator(context, _performance.value); + return config._buildIndicator(context, _animation.value); } ); } @@ -78,13 +75,13 @@ class _LinearProgressIndicatorPainter extends CustomPainter { this.backgroundColor, this.valueColor, this.value, - this.performanceValue + this.animationValue }); final Color backgroundColor; final Color valueColor; final double value; - final double performanceValue; + final double animationValue; void paint(Canvas canvas, Size size) { Paint paint = new Paint() @@ -97,7 +94,7 @@ class _LinearProgressIndicatorPainter extends CustomPainter { double width = value.clamp(0.0, 1.0) * size.width; canvas.drawRect(Point.origin & new Size(width, size.height), paint); } else { - double startX = size.width * (1.5 * performanceValue - 0.5); + double startX = size.width * (1.5 * animationValue - 0.5); double endX = startX + 0.5 * size.width; double x = startX.clamp(0.0, size.width); double width = endX.clamp(0.0, size.width) - x; @@ -109,7 +106,7 @@ class _LinearProgressIndicatorPainter extends CustomPainter { return oldPainter.backgroundColor != backgroundColor || oldPainter.valueColor != valueColor || oldPainter.value != value - || oldPainter.performanceValue != performanceValue; + || oldPainter.animationValue != animationValue; } } @@ -119,7 +116,7 @@ class LinearProgressIndicator extends ProgressIndicator { double value }) : super(key: key, value: value); - Widget _buildIndicator(BuildContext context, double performanceValue) { + Widget _buildIndicator(BuildContext context, double animationValue) { return new Container( constraints: new BoxConstraints.tightFor( width: double.INFINITY, @@ -130,7 +127,7 @@ class LinearProgressIndicator extends ProgressIndicator { backgroundColor: _getBackgroundColor(context), valueColor: _getValueColor(context), value: value, - performanceValue: performanceValue + animationValue: animationValue ) ) ); @@ -147,12 +144,12 @@ class _CircularProgressIndicatorPainter extends CustomPainter { const _CircularProgressIndicatorPainter({ this.valueColor, this.value, - this.performanceValue + this.animationValue }); final Color valueColor; final double value; - final double performanceValue; + final double animationValue; void paint(Canvas canvas, Size size) { Paint paint = new Paint() @@ -166,7 +163,7 @@ class _CircularProgressIndicatorPainter extends CustomPainter { ..arcTo(Point.origin & size, _kStartAngle, angle, false); canvas.drawPath(path, paint); } else { - double startAngle = _kTwoPI * (1.75 * performanceValue - 0.75); + double startAngle = _kTwoPI * (1.75 * animationValue - 0.75); double endAngle = startAngle + _kTwoPI * 0.75; double arcAngle = startAngle.clamp(0.0, _kTwoPI); double arcSweep = endAngle.clamp(0.0, _kTwoPI) - arcAngle; @@ -179,7 +176,7 @@ class _CircularProgressIndicatorPainter extends CustomPainter { bool shouldRepaint(_CircularProgressIndicatorPainter oldPainter) { return oldPainter.valueColor != valueColor || oldPainter.value != value - || oldPainter.performanceValue != performanceValue; + || oldPainter.animationValue != animationValue; } } @@ -189,7 +186,7 @@ class CircularProgressIndicator extends ProgressIndicator { double value }) : super(key: key, value: value); - Widget _buildIndicator(BuildContext context, double performanceValue) { + Widget _buildIndicator(BuildContext context, double animationValue) { return new Container( constraints: new BoxConstraints( minWidth: _kMinCircularProgressIndicatorSize, @@ -199,7 +196,7 @@ class CircularProgressIndicator extends ProgressIndicator { painter: new _CircularProgressIndicatorPainter( valueColor: _getValueColor(context), value: value, - performanceValue: performanceValue + animationValue: animationValue ) ) ); diff --git a/packages/flutter/lib/src/material/scaffold.dart b/packages/flutter/lib/src/material/scaffold.dart index a35e952cc7..985a4591e5 100644 --- a/packages/flutter/lib/src/material/scaffold.dart +++ b/packages/flutter/lib/src/material/scaffold.dart @@ -111,18 +111,18 @@ class _FloatingActionButtonTransition extends StatefulComponent { } class _FloatingActionButtonTransitionState extends State<_FloatingActionButtonTransition> { - final Performance performance = new Performance(duration: _kFloatingActionButtonSegue); + final AnimationController controller = new AnimationController(duration: _kFloatingActionButtonSegue); Widget oldChild; void initState() { super.initState(); - performance.play().then((_) { + controller.forward().then((_) { oldChild = null; }); } void dispose() { - performance.stop(); + controller.stop(); super.dispose(); } @@ -130,9 +130,9 @@ class _FloatingActionButtonTransitionState extends State<_FloatingActionButtonTr if (Widget.canUpdate(oldConfig.child, config.child)) return; oldChild = oldConfig.child; - performance - ..progress = 0.0 - ..play().then((_) { + controller + ..value = 0.0 + ..forward().then((_) { oldChild = null; }); } @@ -141,15 +141,23 @@ class _FloatingActionButtonTransitionState extends State<_FloatingActionButtonTr final List children = new List(); if (oldChild != null) { children.add(new ScaleTransition( - scale: new AnimatedValue(1.0, end: 0.0, curve: new Interval(0.0, 0.5, curve: Curves.easeIn)), - performance: performance, + // TODO(abarth): We should use ReversedAnimation here. + scale: new Tween( + begin: 1.0, + end: 0.0 + ).watch(new CurvedAnimation( + parent: controller, + curve: const Interval(0.0, 0.5, curve: Curves.easeIn) + )), child: oldChild )); } children.add(new ScaleTransition( - scale: new AnimatedValue(0.0, end: 1.0, curve: new Interval(0.5, 1.0, curve: Curves.easeIn)), - performance: performance, + scale: new CurvedAnimation( + parent: controller, + curve: const Interval(0.5, 1.0, curve: Curves.easeIn) + ), child: config.child )); @@ -190,22 +198,22 @@ class ScaffoldState extends State { // SNACKBAR API Queue> _snackBars = new Queue>(); - Performance _snackBarPerformance; + AnimationController _snackBarController; Timer _snackBarTimer; ScaffoldFeatureController showSnackBar(SnackBar snackbar) { - _snackBarPerformance ??= SnackBar.createPerformanceController() + _snackBarController ??= SnackBar.createAnimationController() ..addStatusListener(_handleSnackBarStatusChange); if (_snackBars.isEmpty) { - assert(_snackBarPerformance.isDismissed); - _snackBarPerformance.forward(); + assert(_snackBarController.isDismissed); + _snackBarController.forward(); } ScaffoldFeatureController controller; controller = new ScaffoldFeatureController._( // We provide a fallback key so that if back-to-back snackbars happen to // match in structure, material ink splashes and highlights don't survive // from one to the next. - snackbar.withPerformance(_snackBarPerformance, fallbackKey: new UniqueKey()), + snackbar.withAnimation(_snackBarController, fallbackKey: new UniqueKey()), new Completer(), () { assert(_snackBars.first == controller); @@ -227,7 +235,7 @@ class ScaffoldState extends State { _snackBars.removeFirst(); }); if (_snackBars.isNotEmpty) - _snackBarPerformance.forward(); + _snackBarController.forward(); break; case PerformanceStatus.completed: setState(() { @@ -242,10 +250,10 @@ class ScaffoldState extends State { } void _hideSnackBar() { - assert(_snackBarPerformance.status == PerformanceStatus.forward || - _snackBarPerformance.status == PerformanceStatus.completed); + assert(_snackBarController.status == PerformanceStatus.forward || + _snackBarController.status == PerformanceStatus.completed); _snackBars.first._completer.complete(); - _snackBarPerformance.reverse(); + _snackBarController.reverse(); _snackBarTimer = null; } @@ -262,7 +270,7 @@ class ScaffoldState extends State { } Completer completer = new Completer(); GlobalKey<_PersistentBottomSheetState> bottomSheetKey = new GlobalKey<_PersistentBottomSheetState>(); - Performance performance = BottomSheet.createPerformanceController() + AnimationController controller = BottomSheet.createAnimationController() ..forward(); _PersistentBottomSheet bottomSheet; LocalHistoryEntry entry = new LocalHistoryEntry( @@ -278,7 +286,7 @@ class ScaffoldState extends State { ); bottomSheet = new _PersistentBottomSheet( key: bottomSheetKey, - performance: performance, + animationController: controller, onClosing: () { assert(_currentBottomSheet._widget == bottomSheet); entry.remove(); @@ -307,8 +315,8 @@ class ScaffoldState extends State { // INTERNALS void dispose() { - _snackBarPerformance?.stop(); - _snackBarPerformance = null; + _snackBarController?.stop(); + _snackBarController = null; _snackBarTimer?.cancel(); _snackBarTimer = null; super.dispose(); @@ -357,7 +365,7 @@ class ScaffoldState extends State { if (_snackBars.length > 0) { ModalRoute route = ModalRoute.of(context); if (route == null || route.isCurrent) { - if (_snackBarPerformance.isCompleted && _snackBarTimer == null) + if (_snackBarController.isCompleted && _snackBarTimer == null) _snackBarTimer = new Timer(_snackBars.first._widget.duration, _hideSnackBar); } else { _snackBarTimer?.cancel(); @@ -420,13 +428,13 @@ class ScaffoldFeatureController { class _PersistentBottomSheet extends StatefulComponent { _PersistentBottomSheet({ Key key, - this.performance, + this.animationController, this.onClosing, this.onDismissed, this.builder }) : super(key: key); - final Performance performance; + final AnimationController animationController; final VoidCallback onClosing; final VoidCallback onDismissed; final WidgetBuilder builder; @@ -441,22 +449,22 @@ class _PersistentBottomSheetState extends State<_PersistentBottomSheet> { void initState() { super.initState(); - assert(config.performance.status == PerformanceStatus.forward); - config.performance.addStatusListener(_handleStatusChange); + assert(config.animationController.status == PerformanceStatus.forward); + config.animationController.addStatusListener(_handleStatusChange); } void didUpdateConfig(_PersistentBottomSheet oldConfig) { super.didUpdateConfig(oldConfig); - assert(config.performance == oldConfig.performance); + assert(config.animationController == oldConfig.animationController); } void dispose() { - config.performance.stop(); + config.animationController.stop(); super.dispose(); } void close() { - config.performance.reverse(); + config.animationController.reverse(); } void _handleStatusChange(PerformanceStatus status) { @@ -465,15 +473,20 @@ class _PersistentBottomSheetState extends State<_PersistentBottomSheet> { } Widget build(BuildContext context) { - return new AlignTransition( - performance: config.performance, - alignment: new AnimatedValue(const FractionalOffset(0.0, 0.0)), - heightFactor: new AnimatedValue(0.0, end: 1.0), - child: new BottomSheet( - performance: config.performance, - onClosing: config.onClosing, - builder: config.builder - ) + Widget child = new BottomSheet( + animationController: config.animationController, + onClosing: config.onClosing, + builder: config.builder + ); + return new AnimatedBuilder( + animation: config.animationController, + builder: (BuildContext context) { + return new Align( + alignment: const FractionalOffset(0.0, 0.0), + heightFactor: config.animationController.value, + child: child + ); + } ); } diff --git a/packages/flutter/lib/src/material/snack_bar.dart b/packages/flutter/lib/src/material/snack_bar.dart index e088851a0d..1625dcd401 100644 --- a/packages/flutter/lib/src/material/snack_bar.dart +++ b/packages/flutter/lib/src/material/snack_bar.dart @@ -28,6 +28,7 @@ const Color _kSnackBackground = const Color(0xFF323232); const Duration _kSnackBarTransitionDuration = const Duration(milliseconds: 250); const Duration kSnackBarShortDisplayDuration = const Duration(milliseconds: 1500); const Duration kSnackBarMediumDisplayDuration = const Duration(milliseconds: 2750); +const Curve _snackBarHeightCurve = Curves.fastOutSlowIn; const Curve _snackBarFadeCurve = const Interval(0.72, 1.0, curve: Curves.fastOutSlowIn); class SnackBarAction extends StatelessComponent { @@ -56,7 +57,7 @@ class SnackBar extends StatelessComponent { this.content, this.actions, this.duration: kSnackBarShortDisplayDuration, - this.performance + this.animation }) : super(key: key) { assert(content != null); } @@ -64,10 +65,10 @@ class SnackBar extends StatelessComponent { final Widget content; final List actions; final Duration duration; - final PerformanceView performance; + final Animated animation; Widget build(BuildContext context) { - assert(performance != null); + assert(animation != null); List children = [ new Flexible( child: new Container( @@ -81,55 +82,61 @@ class SnackBar extends StatelessComponent { ]; if (actions != null) children.addAll(actions); + CurvedAnimation heightAnimation = new CurvedAnimation(parent: animation, curve: _snackBarHeightCurve); + CurvedAnimation fadeAnimation = new CurvedAnimation(parent: animation, curve: _snackBarFadeCurve); ThemeData theme = Theme.of(context); - return new ClipRect( - child: new AlignTransition( - performance: performance, - alignment: new AnimatedValue(const FractionalOffset(0.0, 0.0)), - heightFactor: new AnimatedValue(0.0, end: 1.0, curve: Curves.fastOutSlowIn), - child: new Material( - elevation: 6, - color: _kSnackBackground, - child: new Container( - margin: const EdgeDims.symmetric(horizontal: _kSideMargins), - child: new Theme( - data: new ThemeData( - brightness: ThemeBrightness.dark, - accentColor: theme.accentColor, - accentColorBrightness: theme.accentColorBrightness, - text: Typography.white - ), - child: new FadeTransition( - performance: performance, - opacity: new AnimatedValue(0.0, end: 1.0, curve: _snackBarFadeCurve), - child: new Row( - children: children, - alignItems: FlexAlignItems.center - ) - ) + Widget child = new Material( + elevation: 6, + color: _kSnackBackground, + child: new Container( + margin: const EdgeDims.symmetric(horizontal: _kSideMargins), + child: new Theme( + data: new ThemeData( + brightness: ThemeBrightness.dark, + accentColor: theme.accentColor, + accentColorBrightness: theme.accentColorBrightness, + text: Typography.white + ), + child: new FadeTransition( + opacity: fadeAnimation, + child: new Row( + children: children, + alignItems: FlexAlignItems.center ) ) ) ) ); + return new ClipRect( + child: new AnimatedBuilder( + animation: heightAnimation, + builder: (BuildContext context) { + return new Align( + alignment: const FractionalOffset(0.0, 0.0), + heightFactor: heightAnimation.value, + child: child + ); + } + ) + ); } // API for Scaffold.addSnackBar(): - static Performance createPerformanceController() { - return new Performance( + static AnimationController createAnimationController() { + return new AnimationController( duration: _kSnackBarTransitionDuration, debugLabel: 'SnackBar' ); } - SnackBar withPerformance(Performance newPerformance, { Key fallbackKey }) { + SnackBar withAnimation(Animated newAnimation, { Key fallbackKey }) { return new SnackBar( key: key ?? fallbackKey, content: content, actions: actions, duration: duration, - performance: newPerformance + animation: newAnimation ); } } diff --git a/packages/flutter/lib/src/material/tabs.dart b/packages/flutter/lib/src/material/tabs.dart index feb5d99855..1fb57f462e 100644 --- a/packages/flutter/lib/src/material/tabs.dart +++ b/packages/flutter/lib/src/material/tabs.dart @@ -415,10 +415,10 @@ class TabBarSelection extends StatefulComponent { class TabBarSelectionState extends State> { - Animation get animation => _controller.view; + Animated get animation => _controller.view; // Both the TabBar and TabBarView classes access _performance because they // alternately drive selection progress between tabs. - final AnimationController _controller = new AnimationController(duration: _kTabBarScroll, progress: 1.0); + final AnimationController _controller = new AnimationController(duration: _kTabBarScroll, value: 1.0); final Map _valueToIndex = new Map(); void _initValueToIndex() { @@ -480,22 +480,22 @@ class TabBarSelectionState extends State> { // one. Convert progress to reflect the fact that we're now moving between (just) // the previous and current selection index. - double progress; + double value; if (_controller.status == PerformanceStatus.completed) - progress = 0.0; + value = 0.0; else if (_previousValue == values.first) - progress = _controller.progress; + value = _controller.value; else if (_previousValue == values.last) - progress = 1.0 - _controller.progress; + value = 1.0 - _controller.value; else if (previousIndex < index) - progress = (_controller.progress - 0.5) * 2.0; + value = (_controller.value - 0.5) * 2.0; else - progress = 1.0 - _controller.progress * 2.0; + value = 1.0 - _controller.value * 2.0; _controller - ..progress = progress + ..value = value ..forward().then((_) { - if (_controller.progress == 1.0) { + if (_controller.value == 1.0) { if (config.onChanged != null) config.onChanged(_value); _valueIsChanging = false; @@ -612,7 +612,7 @@ class _TabBarState extends ScrollableState> implements TabBarSelect _valueIsChanging = true; } Rect oldRect = _indicatorRect; - double t = _selection.animation.progress; + double t = _selection.animation.value; if (_valueIsChanging) { // When _valueIsChanging is true, we're animating based on a ticker and // want to curve the animation. When _valueIsChanging is false, we're @@ -676,9 +676,9 @@ class _TabBarState extends ScrollableState> implements TabBarSelect labelColor = isSelectedTab ? selectedColor : color; if (_selection.valueIsChanging) { if (isSelectedTab) - labelColor = Color.lerp(color, selectedColor, _selection.animation.progress); + labelColor = Color.lerp(color, selectedColor, _selection.animation.value); else if (isPreviouslySelectedTab) - labelColor = Color.lerp(selectedColor, color, _selection.animation.progress); + labelColor = Color.lerp(selectedColor, color, _selection.animation.value); } } return new _Tab( @@ -876,7 +876,7 @@ class _TabBarViewState extends PageableListState implements TabBarSe return; // The TabBar is driving the TabBarSelection performance. - final Animation animation = _selection.animation; + final Animated animation = _selection.animation; if (animation.status == PerformanceStatus.completed) { _updateItemsAndScrollBehavior(); @@ -898,9 +898,9 @@ class _TabBarViewState extends PageableListState implements TabBarSe } if (_scrollDirection == AnimationDirection.forward) - scrollTo(animation.progress); + scrollTo(animation.value); else - scrollTo(1.0 - animation.progress); + scrollTo(1.0 - animation.value); } void dispatchOnScroll() { @@ -911,9 +911,9 @@ class _TabBarViewState extends PageableListState implements TabBarSe final AnimationController controller = _selection._controller; if (_selection.index == 0 || _selection.index == _tabCount - 1) - controller.progress = scrollOffset; + controller.value = scrollOffset; else - controller.progress = scrollOffset / 2.0; + controller.value = scrollOffset / 2.0; } Future fling(Offset scrollVelocity) { diff --git a/packages/flutter/lib/src/material/tooltip.dart b/packages/flutter/lib/src/material/tooltip.dart index 0a52c5b5ee..60f27d3d86 100644 --- a/packages/flutter/lib/src/material/tooltip.dart +++ b/packages/flutter/lib/src/material/tooltip.dart @@ -158,7 +158,7 @@ class _TooltipState extends State { assert(_entry != null); _timer?.cancel(); _timer = null; - _performance.reverse(); + _performance.reverse(); } void deactivate() { @@ -266,7 +266,7 @@ class _TooltipOverlay extends StatelessComponent { screenEdgeMargin: screenEdgeMargin, preferBelow: preferBelow ), - child: new FadeTransition( + child: new OldFadeTransition( performance: performance, opacity: new AnimatedValue(0.0, end: 1.0, curve: Curves.ease), child: new Opacity( diff --git a/packages/flutter/lib/src/widgets/enter_exit_transition.dart b/packages/flutter/lib/src/widgets/enter_exit_transition.dart index db851eb240..e7c4b0b232 100644 --- a/packages/flutter/lib/src/widgets/enter_exit_transition.dart +++ b/packages/flutter/lib/src/widgets/enter_exit_transition.dart @@ -91,9 +91,9 @@ class _Entry { } } -typedef Widget TransitionBuilderCallback(Animation animation, Widget child); +typedef Widget TransitionBuilderCallback(Animated animation, Widget child); -Widget _identityTransition(Animation animation, Widget child) => child; +Widget _identityTransition(Animated animation, Widget child) => child; class EnterExitTransition extends StatefulComponent { EnterExitTransition({ diff --git a/packages/flutter/lib/src/widgets/transitions.dart b/packages/flutter/lib/src/widgets/transitions.dart index 91b6e441d0..3d2c30502d 100644 --- a/packages/flutter/lib/src/widgets/transitions.dart +++ b/packages/flutter/lib/src/widgets/transitions.dart @@ -63,47 +63,47 @@ class _TransitionState extends State { } } -abstract class AnimationWatchingComponent extends StatefulComponent { - AnimationWatchingComponent({ +abstract class AnimatedComponent extends StatefulComponent { + AnimatedComponent({ Key key, - this.watchable + this.animation }) : super(key: key) { - assert(watchable != null); + assert(animation != null); } - final Watchable watchable; + final Animated animation; Widget build(BuildContext context); - _AnimationWatchingComponentState createState() => new _AnimationWatchingComponentState(); + _AnimatedComponentState createState() => new _AnimatedComponentState(); void debugFillDescription(List description) { super.debugFillDescription(description); - description.add('watchable: $watchable'); + description.add('animation: $animation'); } } -class _AnimationWatchingComponentState extends State { +class _AnimatedComponentState extends State { void initState() { super.initState(); - config.watchable.addListener(_handleTick); + config.animation.addListener(_handleTick); } - void didUpdateConfig(AnimationWatchingComponent oldConfig) { - if (config.watchable != oldConfig.watchable) { - oldConfig.watchable.removeListener(_handleTick); - config.watchable.addListener(_handleTick); + void didUpdateConfig(AnimatedComponent oldConfig) { + if (config.animation != oldConfig.animation) { + oldConfig.animation.removeListener(_handleTick); + config.animation.addListener(_handleTick); } } void dispose() { - config.watchable.removeListener(_handleTick); + config.animation.removeListener(_handleTick); super.dispose(); } void _handleTick() { setState(() { - // The watchable's state is our build state, and it changed already. + // The animation's state is our build state, and it changed already. }); } @@ -146,24 +146,22 @@ class SlideTransition extends TransitionWithChild { } } -class ScaleTransition extends TransitionWithChild { +class ScaleTransition extends AnimatedComponent { ScaleTransition({ Key key, - this.scale, + Animated scale, this.alignment: const FractionalOffset(0.5, 0.5), - PerformanceView performance, - Widget child - }) : super(key: key, - performance: performance, - child: child); + this.child + }) : scale = scale, super(key: key, animation: scale); - final AnimatedValue scale; + final Animated scale; final FractionalOffset alignment; + final Widget child; - Widget buildWithChild(BuildContext context, Widget child) { - performance.updateVariable(scale); + Widget build(BuildContext context) { + double scaleValue = scale.value; Matrix4 transform = new Matrix4.identity() - ..scale(scale.value, scale.value); + ..scale(scaleValue, scaleValue); return new Transform( transform: transform, alignment: alignment, @@ -195,8 +193,8 @@ class RotationTransition extends TransitionWithChild { } } -class FadeTransition extends TransitionWithChild { - FadeTransition({ +class OldFadeTransition extends TransitionWithChild { + OldFadeTransition({ Key key, this.opacity, PerformanceView performance, @@ -213,6 +211,21 @@ class FadeTransition extends TransitionWithChild { } } +class FadeTransition extends AnimatedComponent { + FadeTransition({ + Key key, + Animated opacity, + this.child + }) : opacity = opacity, super(key: key, animation: opacity); + + final Animated opacity; + final Widget child; + + Widget build(BuildContext context) { + return new Opacity(opacity: opacity.value, child: child); + } +} + class ColorTransition extends TransitionWithChild { ColorTransition({ Key key, @@ -352,12 +365,12 @@ class BuilderTransition extends TransitionComponent { } } -class AnimationWatchingBuilder extends AnimationWatchingComponent { - AnimationWatchingBuilder({ +class AnimatedBuilder extends AnimatedComponent { + AnimatedBuilder({ Key key, - Watchable watchable, + Animated animation, this.builder - }) : super(key: key, watchable: watchable); + }) : super(key: key, animation: animation); final WidgetBuilder builder; From 6cea5dc80d0585e8839e0bfe0a9d19ac22a4ce64 Mon Sep 17 00:00:00 2001 From: Adam Barth Date: Tue, 19 Jan 2016 22:58:59 -0800 Subject: [PATCH 2/4] Switch Navigator over to using AnimationController This patch moves Navigator and related code over to using AnimationController. --- packages/flutter/lib/animation.dart | 1 + .../flutter/lib/src/animation/animations.dart | 248 ++++++++++++++++++ packages/flutter/lib/src/animation/tween.dart | 23 +- .../lib/src/material/bottom_sheet.dart | 32 +-- packages/flutter/lib/src/material/dialog.dart | 12 +- .../flutter/lib/src/material/dropdown.dart | 58 ++-- .../src/material/floating_action_button.dart | 22 +- packages/flutter/lib/src/material/page.dart | 37 +-- .../flutter/lib/src/material/popup_menu.dart | 68 ++--- .../flutter/lib/src/material/scaffold.dart | 2 +- packages/flutter/lib/src/widgets/heroes.dart | 89 +++---- .../lib/src/widgets/modal_barrier.dart | 29 +- packages/flutter/lib/src/widgets/pages.dart | 8 +- packages/flutter/lib/src/widgets/routes.dart | 106 ++++---- .../lib/src/widgets/status_transitions.dart | 16 +- .../flutter/lib/src/widgets/transitions.dart | 43 ++- .../widget/page_forward_transitions_test.dart | 17 +- .../flutter/test/widget/positioned_test.dart | 14 +- 18 files changed, 536 insertions(+), 289 deletions(-) create mode 100644 packages/flutter/lib/src/animation/animations.dart diff --git a/packages/flutter/lib/animation.dart b/packages/flutter/lib/animation.dart index e97a134e00..64fdc8287f 100644 --- a/packages/flutter/lib/animation.dart +++ b/packages/flutter/lib/animation.dart @@ -8,6 +8,7 @@ library animation; export 'src/animation/animated_value.dart'; +export 'src/animation/animations.dart'; export 'src/animation/clamped_simulation.dart'; export 'src/animation/curves.dart'; export 'src/animation/forces.dart'; diff --git a/packages/flutter/lib/src/animation/animations.dart b/packages/flutter/lib/src/animation/animations.dart new file mode 100644 index 0000000000..fcf42956bc --- /dev/null +++ b/packages/flutter/lib/src/animation/animations.dart @@ -0,0 +1,248 @@ +// Copyright 2016 The Chromium 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 'dart:ui' show VoidCallback; + +import 'animated_value.dart'; +import 'listener_helpers.dart'; +import 'tween.dart'; + +class AlwaysCompleteAnimation extends Animated { + const AlwaysCompleteAnimation(); + + // this performance never changes state + void addListener(VoidCallback listener) { } + void removeListener(VoidCallback listener) { } + void addStatusListener(PerformanceStatusListener listener) { } + void removeStatusListener(PerformanceStatusListener listener) { } + PerformanceStatus get status => PerformanceStatus.completed; + AnimationDirection get direction => AnimationDirection.forward; + double get value => 1.0; +} + +const AlwaysCompleteAnimation kAlwaysCompleteAnimation = const AlwaysCompleteAnimation(); + +class AlwaysDismissedAnimation extends Animated { + const AlwaysDismissedAnimation(); + + // this performance never changes state + void addListener(VoidCallback listener) { } + void removeListener(VoidCallback listener) { } + void addStatusListener(PerformanceStatusListener listener) { } + void removeStatusListener(PerformanceStatusListener listener) { } + PerformanceStatus get status => PerformanceStatus.dismissed; + AnimationDirection get direction => AnimationDirection.forward; + double get value => 0.0; +} + +const AlwaysDismissedAnimation kAlwaysDismissedAnimation = const AlwaysDismissedAnimation(); + +class ProxyAnimation extends Animated + with LazyListenerMixin, LocalPerformanceListenersMixin, LocalPerformanceStatusListenersMixin { + ProxyAnimation([Animated animation]) { + _masterAnimation = animation; + if (_masterAnimation == null) { + _status = PerformanceStatus.dismissed; + _direction = AnimationDirection.forward; + _value = 0.0; + } + } + + PerformanceStatus _status; + AnimationDirection _direction; + double _value; + + Animated get masterAnimation => _masterAnimation; + Animated _masterAnimation; + void set masterAnimation(Animated value) { + if (value == _masterAnimation) + return; + if (_masterAnimation != null) { + _status = _masterAnimation.status; + _direction = _masterAnimation.direction; + _value = _masterAnimation.value; + if (isListening) + didStopListening(); + } + _masterAnimation = value; + if (_masterAnimation != null) { + if (isListening) + didStartListening(); + if (_value != _masterAnimation.value) + notifyListeners(); + if (_status != _masterAnimation.status) + notifyStatusListeners(_masterAnimation.status); + _status = null; + _direction = null; + _value = null; + } + } + + void didStartListening() { + if (_masterAnimation != null) { + _masterAnimation.addListener(notifyListeners); + _masterAnimation.addStatusListener(notifyStatusListeners); + } + } + + void didStopListening() { + if (_masterAnimation != null) { + _masterAnimation.removeListener(notifyListeners); + _masterAnimation.removeStatusListener(notifyStatusListeners); + } + } + + PerformanceStatus get status => _masterAnimation != null ? _masterAnimation.status : _status; + AnimationDirection get direction => _masterAnimation != null ? _masterAnimation.direction : _direction; + double get value => _masterAnimation != null ? _masterAnimation.value : _value; +} + +class ReverseAnimation extends Animated + with LazyListenerMixin, LocalPerformanceStatusListenersMixin { + ReverseAnimation(this.masterAnimation); + + final Animated masterAnimation; + + void addListener(VoidCallback listener) { + didRegisterListener(); + masterAnimation.addListener(listener); + } + void removeListener(VoidCallback listener) { + masterAnimation.removeListener(listener); + didUnregisterListener(); + } + + void didStartListening() { + masterAnimation.addStatusListener(_statusChangeHandler); + } + + void didStopListening() { + masterAnimation.removeStatusListener(_statusChangeHandler); + } + + void _statusChangeHandler(PerformanceStatus status) { + notifyStatusListeners(_reverseStatus(status)); + } + + PerformanceStatus get status => _reverseStatus(masterAnimation.status); + AnimationDirection get direction => _reverseDirection(masterAnimation.direction); + double get value => 1.0 - masterAnimation.value; + + PerformanceStatus _reverseStatus(PerformanceStatus status) { + switch (status) { + case PerformanceStatus.forward: return PerformanceStatus.reverse; + case PerformanceStatus.reverse: return PerformanceStatus.forward; + case PerformanceStatus.completed: return PerformanceStatus.dismissed; + case PerformanceStatus.dismissed: return PerformanceStatus.completed; + } + } + + AnimationDirection _reverseDirection(AnimationDirection direction) { + switch (direction) { + case AnimationDirection.forward: return AnimationDirection.reverse; + case AnimationDirection.reverse: return AnimationDirection.forward; + } + } +} + +enum _TrainHoppingMode { minimize, maximize } + +/// This animation starts by proxying one animation, but can be given a +/// second animation. When their times cross (either because the second is +/// going in the opposite direction, or because the one overtakes the other), +/// the animation hops over to proxying the second animation, and the second +/// animation becomes the new "first" performance. +/// +/// Since this object must track the two animations even when it has no +/// listeners of its own, instead of shutting down when all its listeners are +/// removed, it exposes a [dispose()] method. Call this method to shut this +/// object down. +class TrainHoppingAnimation extends Animated + with EagerListenerMixin, LocalPerformanceListenersMixin, LocalPerformanceStatusListenersMixin { + TrainHoppingAnimation(this._currentTrain, this._nextTrain, { this.onSwitchedTrain }) { + assert(_currentTrain != null); + if (_nextTrain != null) { + if (_currentTrain.value > _nextTrain.value) { + _mode = _TrainHoppingMode.maximize; + } else { + _mode = _TrainHoppingMode.minimize; + if (_currentTrain.value == _nextTrain.value) { + _currentTrain = _nextTrain; + _nextTrain = null; + } + } + } + _currentTrain.addStatusListener(_statusChangeHandler); + _currentTrain.addListener(_valueChangeHandler); + if (_nextTrain != null) + _nextTrain.addListener(_valueChangeHandler); + assert(_mode != null); + } + + Animated get currentTrain => _currentTrain; + Animated _currentTrain; + Animated _nextTrain; + _TrainHoppingMode _mode; + + VoidCallback onSwitchedTrain; + + PerformanceStatus _lastStatus; + void _statusChangeHandler(PerformanceStatus status) { + assert(_currentTrain != null); + if (status != _lastStatus) { + notifyListeners(); + _lastStatus = status; + } + assert(_lastStatus != null); + } + + PerformanceStatus get status => _currentTrain.status; + AnimationDirection get direction => _currentTrain.direction; + + double _lastValue; + void _valueChangeHandler() { + assert(_currentTrain != null); + bool hop = false; + if (_nextTrain != null) { + switch (_mode) { + case _TrainHoppingMode.minimize: + hop = _nextTrain.value <= _currentTrain.value; + break; + case _TrainHoppingMode.maximize: + hop = _nextTrain.value >= _currentTrain.value; + break; + } + if (hop) { + _currentTrain.removeStatusListener(_statusChangeHandler); + _currentTrain.removeListener(_valueChangeHandler); + _currentTrain = _nextTrain; + _nextTrain.addListener(_valueChangeHandler); + _statusChangeHandler(_nextTrain.status); + } + } + double newValue = value; + if (newValue != _lastValue) { + notifyListeners(); + _lastValue = newValue; + } + assert(_lastValue != null); + if (hop && onSwitchedTrain != null) + onSwitchedTrain(); + } + + double get value => _currentTrain.value; + + /// Frees all the resources used by this performance. + /// After this is called, this object is no longer usable. + void dispose() { + assert(_currentTrain != null); + _currentTrain.removeStatusListener(_statusChangeHandler); + _currentTrain.removeListener(_valueChangeHandler); + _currentTrain = null; + if (_nextTrain != null) { + _nextTrain.removeListener(_valueChangeHandler); + _nextTrain = null; + } + } +} diff --git a/packages/flutter/lib/src/animation/tween.dart b/packages/flutter/lib/src/animation/tween.dart index 3b620884b7..8bddcb1612 100644 --- a/packages/flutter/lib/src/animation/tween.dart +++ b/packages/flutter/lib/src/animation/tween.dart @@ -97,7 +97,7 @@ abstract class Evaluatable { T evaluate(Animated animation); - Animated watch(Animated parent) { + Animated animate(Animated parent) { return new _AnimatedEvaluation(parent, this); } } @@ -258,7 +258,11 @@ class _RepeatingSimulation extends Simulation { } class CurvedAnimation extends Animated with ProxyAnimatedMixin { - CurvedAnimation({ this.parent, this.curve, this.reverseCurve }) { + CurvedAnimation({ + this.parent, + this.curve: Curves.linear, + this.reverseCurve + }) { assert(parent != null); assert(curve != null); parent.addStatusListener(_handleStatusChanged); @@ -373,3 +377,18 @@ class IntTween extends Tween { // the begin and end types by a double, and int * double returns a double. int lerp(double t) => (begin + (end - begin) * t).round(); } + +class CurveTween extends Evaluatable { + CurveTween({ this.curve }); + + Curve curve; + + double evaluate(Animated animation) { + double t = animation.value; + if (t == 0.0 || t == 1.0) { + assert(curve.transform(t).round() == t); + return t; + } + return curve.transform(t); + } +} diff --git a/packages/flutter/lib/src/material/bottom_sheet.dart b/packages/flutter/lib/src/material/bottom_sheet.dart index ff5f42ca50..0274e54b11 100644 --- a/packages/flutter/lib/src/material/bottom_sheet.dart +++ b/packages/flutter/lib/src/material/bottom_sheet.dart @@ -129,41 +129,17 @@ class _ModalBottomSheet extends StatefulComponent { } class _ModalBottomSheetState extends State<_ModalBottomSheet> { - // TODO(abarth): Delete _controllerPerformanceAdaptor when navigator uses - // AnimationController and friends. - AnimationController _controllerPerformanceAdaptor; - - void initState() { - super.initState(); - _controllerPerformanceAdaptor = new AnimationController(); - _updateControllerPerformanceAdaptor(); - } - - void didUpdateConfig(_ModalBottomSheet oldConfig) { - if (config.route.performance != oldConfig.route.performance) - _updateControllerPerformanceAdaptor(); - } - - void _updateControllerPerformanceAdaptor() { - Performance performance = config.route.performance; - _controllerPerformanceAdaptor - ..duration = performance.duration - ..value = performance.progress; - if (performance.isAnimating) - _controllerPerformanceAdaptor.play(performance.direction); - } - Widget build(BuildContext context) { return new GestureDetector( onTap: () => Navigator.pop(context), child: new AnimatedBuilder( - animation: _controllerPerformanceAdaptor, + animation: config.route.animation, builder: (BuildContext context) { return new ClipRect( child: new CustomOneChildLayout( - delegate: new _ModalBottomSheetLayout(config.route.performance.progress), + delegate: new _ModalBottomSheetLayout(config.route.animation.value), child: new BottomSheet( - animationController: _controllerPerformanceAdaptor, + animationController: config.route.animation, onClosing: () => Navigator.pop(context), builder: config.route.builder ) @@ -191,7 +167,7 @@ class _ModalBottomSheetRoute extends PopupRoute { return BottomSheet.createAnimationController(); } - Widget buildPage(BuildContext context, PerformanceView performance, PerformanceView forwardPerformance) { + Widget buildPage(BuildContext context, Animated animation, Animated forwardAnimation) { return new _ModalBottomSheet(route: this); } } diff --git a/packages/flutter/lib/src/material/dialog.dart b/packages/flutter/lib/src/material/dialog.dart index 4ad90222b9..02ac1c4f95 100644 --- a/packages/flutter/lib/src/material/dialog.dart +++ b/packages/flutter/lib/src/material/dialog.dart @@ -128,14 +128,16 @@ class _DialogRoute extends PopupRoute { bool get barrierDismissable => true; Color get barrierColor => Colors.black54; - Widget buildPage(BuildContext context, PerformanceView performance, PerformanceView forwardPerformance) { + Widget buildPage(BuildContext context, Animated animation, Animated forwardAnimation) { return child; } - Widget buildTransitions(BuildContext context, PerformanceView performance, PerformanceView forwardPerformance, Widget child) { - return new OldFadeTransition( - performance: performance, - opacity: new AnimatedValue(0.0, end: 1.0, curve: Curves.easeOut), + Widget buildTransitions(BuildContext context, Animated animation, Animated forwardAnimation, Widget child) { + return new FadeTransition( + opacity: new CurvedAnimation( + parent: animation, + curve: Curves.easeOut + ), child: child ); } diff --git a/packages/flutter/lib/src/material/dropdown.dart b/packages/flutter/lib/src/material/dropdown.dart index 6d8e606be3..46e7aca108 100644 --- a/packages/flutter/lib/src/material/dropdown.dart +++ b/packages/flutter/lib/src/material/dropdown.dart @@ -62,7 +62,7 @@ class _DropDownMenu extends StatusTransitionComponent { _DropDownMenu({ Key key, _DropDownRoute route - }) : route = route, super(key: key, performance: route.performance); + }) : route = route, super(key: key, animation: route.animation); final _DropDownRoute route; @@ -79,16 +79,15 @@ class _DropDownMenu extends StatusTransitionComponent { final double unit = 0.5 / (route.items.length + 1.5); final List children = []; for (int itemIndex = 0; itemIndex < route.items.length; ++itemIndex) { - AnimatedValue opacity; + CurvedAnimation opacity; if (itemIndex == route.selectedIndex) { - opacity = new AnimatedValue(0.0, end: 1.0, curve: const Interval(0.0, 0.001), reverseCurve: const Interval(0.75, 1.0)); + opacity = new CurvedAnimation(parent: route.animation, curve: const Interval(0.0, 0.001), reverseCurve: const Interval(0.75, 1.0)); } else { final double start = (0.5 + (itemIndex + 1) * unit).clamp(0.0, 1.0); final double end = (start + 1.5 * unit).clamp(0.0, 1.0); - opacity = new AnimatedValue(0.0, end: 1.0, curve: new Interval(start, end), reverseCurve: const Interval(0.75, 1.0)); + opacity = new CurvedAnimation(parent: route.animation, curve: new Interval(start, end), reverseCurve: const Interval(0.75, 1.0)); } - children.add(new OldFadeTransition( - performance: route.performance, + children.add(new FadeTransition( opacity: opacity, child: new InkWell( child: new Container( @@ -103,42 +102,45 @@ class _DropDownMenu extends StatusTransitionComponent { )); } - final AnimatedValue menuOpacity = new AnimatedValue(0.0, - end: 1.0, + final CurvedAnimation opacity = new CurvedAnimation( + parent: route.animation, curve: const Interval(0.0, 0.25), reverseCurve: const Interval(0.75, 1.0) ); - final AnimatedValue menuTop = new AnimatedValue(route.rect.top, - end: route.rect.top - route.selectedIndex * route.rect.height, - curve: const Interval(0.25, 0.5), - reverseCurve: const Interval(0.0, 0.001) - ); - final AnimatedValue menuBottom = new AnimatedValue(route.rect.bottom, - end: menuTop.end + route.items.length * route.rect.height, + final CurvedAnimation resize = new CurvedAnimation( + parent: route.animation, curve: const Interval(0.25, 0.5), reverseCurve: const Interval(0.0, 0.001) ); - return new OldFadeTransition( - performance: route.performance, - opacity: menuOpacity, - child: new BuilderTransition( - performance: route.performance, - variables: >[menuTop, menuBottom], + final Tween menuTop = new Tween( + begin: route.rect.top, + end: route.rect.top - route.selectedIndex * route.rect.height + ); + final Tween menuBottom = new Tween( + begin: route.rect.bottom, + end: menuTop.end + route.items.length * route.rect.height + ); + + Widget child = new Material( + type: MaterialType.transparency, + child: new Block(children) + ); + return new FadeTransition( + opacity: opacity, + child: new AnimatedBuilder( + animation: resize, builder: (BuildContext context) { return new CustomPaint( painter: new _DropDownMenuPainter( color: Theme.of(context).canvasColor, elevation: route.elevation, - menuTop: menuTop.value, - menuBottom: menuBottom.value, + menuTop: menuTop.evaluate(resize), + menuBottom: menuBottom.evaluate(resize), renderBox: context.findRenderObject() ), - child: new Material( - type: MaterialType.transparency, - child: new Block(children) - ) + child: child ); } ) @@ -190,7 +192,7 @@ class _DropDownRoute extends PopupRoute<_DropDownRouteResult> { ); } - Widget buildPage(BuildContext context, PerformanceView performance, PerformanceView forwardPerformance) { + Widget buildPage(BuildContext context, Animated animation, Animated forwardAnimation) { return new _DropDownMenu(route: this); } } diff --git a/packages/flutter/lib/src/material/floating_action_button.dart b/packages/flutter/lib/src/material/floating_action_button.dart index 3e2e057bec..931a58b86a 100644 --- a/packages/flutter/lib/src/material/floating_action_button.dart +++ b/packages/flutter/lib/src/material/floating_action_button.dart @@ -40,20 +40,29 @@ class FloatingActionButton extends StatefulComponent { } class _FloatingActionButtonState extends State { - final Performance _childSegue = new Performance(duration: _kChildSegue); + Animated _childSegue; + AnimationController _childSegueController; void initState() { super.initState(); - _childSegue.play(); + _childSegueController = new AnimationController(duration: _kChildSegue) + ..forward(); + _childSegue = new Tween( + begin: -0.125, + end: 0.0 + ).animate(new CurvedAnimation( + parent: _childSegueController, + curve: _kChildSegueInterval + )); } void didUpdateConfig(FloatingActionButton oldConfig) { super.didUpdateConfig(oldConfig); if (Widget.canUpdate(oldConfig.child, config.child) && config.backgroundColor == oldConfig.backgroundColor) return; - _childSegue - ..progress = 0.0 - ..play(); + _childSegueController + ..value = 0.0 + ..forward(); } bool _highlight = false; @@ -87,8 +96,7 @@ class _FloatingActionButtonState extends State { child: new IconTheme( data: new IconThemeData(color: iconThemeColor), child: new RotationTransition( - performance: _childSegue, - turns: new AnimatedValue(-0.125, end: 0.0, curve: _kChildSegueInterval), + turns: _childSegue, child: config.child ) ) diff --git a/packages/flutter/lib/src/material/page.dart b/packages/flutter/lib/src/material/page.dart index 3c551a3792..ee73eb90c0 100644 --- a/packages/flutter/lib/src/material/page.dart +++ b/packages/flutter/lib/src/material/page.dart @@ -7,31 +7,32 @@ import 'dart:async'; import 'package:flutter/animation.dart'; import 'package:flutter/widgets.dart'; -class _MaterialPageTransition extends TransitionWithChild { +class _MaterialPageTransition extends AnimatedComponent { _MaterialPageTransition({ Key key, - PerformanceView performance, - Widget child - }) : super(key: key, - performance: performance, - child: child); + Animated animation, + this.child + }) : super( + key: key, + animation: new CurvedAnimation(parent: animation, curve: Curves.easeOut) + ); - final AnimatedValue _position = - new AnimatedValue(const Point(0.0, 75.0), end: Point.origin, curve: Curves.easeOut); + final Widget child; - final AnimatedValue _opacity = - new AnimatedValue(0.0, end: 1.0, curve: Curves.easeOut); + final Tween _position = new Tween( + begin: const Point(0.0, 75.0), + end: Point.origin + ); - Widget buildWithChild(BuildContext context, Widget child) { - performance.updateVariable(_position); - performance.updateVariable(_opacity); + Widget build(BuildContext context) { + Point position = _position.evaluate(animation); Matrix4 transform = new Matrix4.identity() - ..translate(_position.value.x, _position.value.y); + ..translate(position.x, position.y); return new Transform( transform: transform, // TODO(ianh): tell the transform to be un-transformed for hit testing child: new Opacity( - opacity: _opacity.value, + opacity: animation.value, child: child ) ); @@ -56,7 +57,7 @@ class MaterialPageRoute extends PageRoute { Color get barrierColor => null; bool canTransitionFrom(TransitionRoute nextRoute) => false; - Widget buildPage(BuildContext context, PerformanceView performance, PerformanceView forwardPerformance) { + Widget buildPage(BuildContext context, Animated animation, Animated forwardAnimation) { Widget result = builder(context); assert(() { if (result == null) @@ -67,9 +68,9 @@ class MaterialPageRoute extends PageRoute { return result; } - Widget buildTransitions(BuildContext context, PerformanceView performance, PerformanceView forwardPerformance, Widget child) { + Widget buildTransitions(BuildContext context, Animated animation, Animated forwardAnimation, Widget child) { return new _MaterialPageTransition( - performance: performance, + animation: animation, child: child ); } diff --git a/packages/flutter/lib/src/material/popup_menu.dart b/packages/flutter/lib/src/material/popup_menu.dart index 2e72a1b898..c3e53fadbc 100644 --- a/packages/flutter/lib/src/material/popup_menu.dart +++ b/packages/flutter/lib/src/material/popup_menu.dart @@ -61,9 +61,12 @@ class _PopupMenu extends StatelessComponent { for (int i = 0; i < route.items.length; ++i) { double start = (i + 1) * unit; double end = (start + 1.5 * unit).clamp(0.0, 1.0); - children.add(new OldFadeTransition( - performance: route.performance, - opacity: new AnimatedValue(0.0, end: 1.0, curve: new Interval(start, end)), + CurvedAnimation opacity = new CurvedAnimation( + parent: route.animation, + curve: new Interval(start, end) + ); + children.add(new FadeTransition( + opacity: opacity, child: new InkWell( onTap: () => Navigator.pop(context, route.items[i].value), child: route.items[i] @@ -71,38 +74,39 @@ class _PopupMenu extends StatelessComponent { ); } - final AnimatedValue opacity = new AnimatedValue(0.0, end: 1.0, curve: new Interval(0.0, 1.0 / 3.0)); - final AnimatedValue width = new AnimatedValue(0.0, end: 1.0, curve: new Interval(0.0, unit)); - final AnimatedValue height = new AnimatedValue(0.0, end: 1.0, curve: new Interval(0.0, unit * route.items.length)); + final CurveTween opacity = new CurveTween(curve: new Interval(0.0, 1.0 / 3.0)); + final CurveTween width = new CurveTween(curve: new Interval(0.0, unit)); + final CurveTween height = new CurveTween(curve: new Interval(0.0, unit * route.items.length)); - return new BuilderTransition( - performance: route.performance, - variables: >[opacity, width, height], + Widget child = new ConstrainedBox( + constraints: new BoxConstraints( + minWidth: _kMenuMinWidth, + maxWidth: _kMenuMaxWidth + ), + child: new IntrinsicWidth( + stepWidth: _kMenuWidthStep, + child: new Block( + children, + padding: const EdgeDims.symmetric( + vertical: _kMenuVerticalPadding + ) + ) + ) + ); + + return new AnimatedBuilder( + animation: route.animation, builder: (BuildContext context) { return new Opacity( - opacity: opacity.value, + opacity: opacity.evaluate(route.animation), child: new Material( type: MaterialType.card, elevation: route.elevation, child: new Align( alignment: const FractionalOffset(1.0, 0.0), - widthFactor: width.value, - heightFactor: height.value, - child: new ConstrainedBox( - constraints: new BoxConstraints( - minWidth: _kMenuMinWidth, - maxWidth: _kMenuMaxWidth - ), - child: new IntrinsicWidth( - stepWidth: _kMenuWidthStep, - child: new Block( - children, - padding: const EdgeDims.symmetric( - vertical: _kMenuVerticalPadding - ) - ) - ) - ) + widthFactor: width.evaluate(route.animation), + heightFactor: height.evaluate(route.animation), + child: child ) ) ); @@ -127,18 +131,18 @@ class _PopupMenuRoute extends PopupRoute { return position; } - PerformanceView createPerformance() { - return new CurvedPerformance( - super.createPerformance(), + Animated createAnimation() { + return new CurvedAnimation( + parent: super.createAnimation(), reverseCurve: new Interval(0.0, _kMenuCloseIntervalEnd) - ); + ); } Duration get transitionDuration => _kMenuDuration; bool get barrierDismissable => true; Color get barrierColor => null; - Widget buildPage(BuildContext context, PerformanceView performance, PerformanceView forwardPerformance) { + Widget buildPage(BuildContext context, Animated animation, Animated forwardAnimation) { return new _PopupMenu(route: this); } } diff --git a/packages/flutter/lib/src/material/scaffold.dart b/packages/flutter/lib/src/material/scaffold.dart index 985a4591e5..4b269c0772 100644 --- a/packages/flutter/lib/src/material/scaffold.dart +++ b/packages/flutter/lib/src/material/scaffold.dart @@ -145,7 +145,7 @@ class _FloatingActionButtonTransitionState extends State<_FloatingActionButtonTr scale: new Tween( begin: 1.0, end: 0.0 - ).watch(new CurvedAnimation( + ).animate(new CurvedAnimation( parent: controller, curve: const Interval(0.0, 0.5, curve: Curves.easeIn) )), diff --git a/packages/flutter/lib/src/widgets/heroes.dart b/packages/flutter/lib/src/widgets/heroes.dart index fdc1e41a9d..d035ed5ad2 100644 --- a/packages/flutter/lib/src/widgets/heroes.dart +++ b/packages/flutter/lib/src/widgets/heroes.dart @@ -73,7 +73,7 @@ class _HeroManifest { abstract class HeroHandle { bool get alwaysAnimate; - _HeroManifest _takeChild(Rect animationArea); + _HeroManifest _takeChild(Rect animationArea, Animated currentAnimation); } class Hero extends StatefulComponent { @@ -167,7 +167,7 @@ class HeroState extends State implements HeroHandle { bool get alwaysAnimate => config.alwaysAnimate; - _HeroManifest _takeChild(Rect animationArea) { + _HeroManifest _takeChild(Rect animationArea, Animated currentAnimation) { assert(_mode == _HeroMode.measured || _mode == _HeroMode.taken); final RenderBox renderObject = context.findRenderObject(); final Point heroTopLeft = renderObject.localToGlobal(Point.origin); @@ -256,14 +256,14 @@ class _HeroQuestState implements HeroHandle { final RelativeRect targetRect; final int targetTurns; final HeroState targetState; - final AnimatedRelativeRectValue currentRect; - final AnimatedValue currentTurns; + final RelativeRectTween currentRect; + final Tween currentTurns; bool get alwaysAnimate => true; bool get taken => _taken; bool _taken = false; - _HeroManifest _takeChild(Rect animationArea) { + _HeroManifest _takeChild(Rect animationArea, Animated currentAnimation) { assert(!taken); _taken = true; Set states = sourceStates; @@ -273,18 +273,16 @@ class _HeroQuestState implements HeroHandle { key: key, config: child, sourceStates: states, - currentRect: currentRect.value, - currentTurns: currentTurns.value + currentRect: currentRect.evaluate(currentAnimation), + currentTurns: currentTurns.evaluate(currentAnimation) ); } - Widget build(BuildContext context, PerformanceView performance) { + Widget build(BuildContext context, Animated animation) { return new PositionedTransition( - rect: currentRect, - performance: performance, + rect: currentRect.animate(animation), child: new RotationTransition( - turns: currentTurns, - performance: performance, + turns: currentTurns.animate(animation), child: new KeyedSubtree( key: key, child: child @@ -317,16 +315,16 @@ class HeroParty { return result; } - AnimatedRelativeRectValue createAnimatedRelativeRect(RelativeRect begin, RelativeRect end, Curve curve) { - return new AnimatedRelativeRectValue(begin, end: end, curve: curve); + RelativeRectTween createRectTween(RelativeRect begin, RelativeRect end) { + return new RelativeRectTween(begin: begin, end: end); } - AnimatedValue createAnimatedTurns(double begin, double end, Curve curve) { + Tween createTurnsTween(double begin, double end) { assert(end.floor() == end); - return new AnimatedValue(begin, end: end, curve: curve); + return new Tween(begin: begin, end: end); } - void animate(Map heroesFrom, Map heroesTo, Rect animationArea, Curve curve) { + void animate(Map heroesFrom, Map heroesTo, Rect animationArea) { assert(!heroesFrom.containsKey(null)); assert(!heroesTo.containsKey(null)); @@ -346,9 +344,9 @@ class HeroParty { if ((heroPair.from == null && !heroPair.to.alwaysAnimate) || (heroPair.to == null && !heroPair.from.alwaysAnimate)) continue; - _HeroManifest from = heroPair.from?._takeChild(animationArea); + _HeroManifest from = heroPair.from?._takeChild(animationArea, _currentAnimation); assert(heroPair.to == null || heroPair.to is HeroState); - _HeroManifest to = heroPair.to?._takeChild(animationArea); + _HeroManifest to = heroPair.to?._takeChild(animationArea, _currentAnimation); assert(from != null || to != null); assert(to == null || to.sourceStates.length == 1); assert(to == null || to.currentTurns.floor() == to.currentTurns); @@ -369,8 +367,8 @@ class HeroParty { targetRect: targetRect, targetTurns: targetTurns.floor(), targetState: targetState, - currentRect: createAnimatedRelativeRect(sourceRect, targetRect, curve), - currentTurns: createAnimatedTurns(sourceTurns, targetTurns, curve) + currentRect: createRectTween(sourceRect, targetRect), + currentTurns: createTurnsTween(sourceTurns, targetTurns) )); } @@ -378,19 +376,19 @@ class HeroParty { _heroes = _newHeroes; } - PerformanceView _currentPerformance; + Animated _currentAnimation; void _clearCurrentPerformance() { - _currentPerformance?.removeStatusListener(_handleUpdate); - _currentPerformance = null; + _currentAnimation?.removeStatusListener(_handleUpdate); + _currentAnimation = null; } - void setPerformance(PerformanceView performance) { - assert(performance != null || _heroes.length == 0); - if (performance != _currentPerformance) { + void setAnimation(Animated animation) { + assert(animation != null || _heroes.length == 0); + if (animation != _currentAnimation) { _clearCurrentPerformance(); - _currentPerformance = performance; - _currentPerformance?.addStatusListener(_handleUpdate); + _currentAnimation = animation; + _currentAnimation?.addStatusListener(_handleUpdate); } } @@ -419,7 +417,7 @@ class HeroController extends NavigatorObserver { } HeroParty _party; - PerformanceView _performance; + Animated _animation; PageRoute _from; PageRoute _to; @@ -429,11 +427,11 @@ class HeroController extends NavigatorObserver { assert(navigator != null); assert(route != null); if (route is PageRoute) { - assert(route.performance != null); + assert(route.animation != null); if (previousRoute is PageRoute) // could be null _from = previousRoute; _to = route; - _performance = route.performance; + _animation = route.animation; _checkForHeroQuest(); } } @@ -442,11 +440,11 @@ class HeroController extends NavigatorObserver { assert(navigator != null); assert(route != null); if (route is PageRoute) { - assert(route.performance != null); + assert(route.animation != null); if (previousRoute is PageRoute) { _to = previousRoute; _from = route; - _performance = route.performance; + _animation = route.animation; _checkForHeroQuest(); } } @@ -454,7 +452,7 @@ class HeroController extends NavigatorObserver { void _checkForHeroQuest() { if (_from != null && _to != null && _from != _to) { - _to.offstage = _to.performance.status != PerformanceStatus.completed; + _to.offstage = _to.animation.status != PerformanceStatus.completed; Scheduler.instance.addPostFrameCallback(_updateQuest); } } @@ -463,7 +461,7 @@ class HeroController extends NavigatorObserver { _removeHeroesFromOverlay(); _from = null; _to = null; - _performance = null; + _animation = null; } Rect _getAnimationArea(BuildContext context) { @@ -481,7 +479,7 @@ class HeroController extends NavigatorObserver { void _addHeroToOverlay(Widget hero, Object tag, OverlayState overlay) { OverlayEntry entry = new OverlayEntry(builder: (_) => hero); - if (_performance.direction == AnimationDirection.forward) + if (_animation.direction == AnimationDirection.forward) _to.insertHeroOverlayEntry(entry, tag, overlay); else _from.insertHeroOverlayEntry(entry, tag, overlay); @@ -507,18 +505,21 @@ class HeroController extends NavigatorObserver { Map heroesTo = Hero.of(_to.subtreeContext, mostValuableKeys); _to.offstage = false; - PerformanceView performance = _performance; + Animated animation = _animation; Curve curve = Curves.ease; - if (performance.status == PerformanceStatus.reverse) { - performance = new ReversePerformance(performance); - curve = new Interval(performance.progress, 1.0, curve: curve); + if (animation.status == PerformanceStatus.reverse) { + animation = new ReverseAnimation(animation); + curve = new Interval(animation.value, 1.0, curve: curve); } - _party.animate(heroesFrom, heroesTo, _getAnimationArea(navigator.context), curve); + _party.animate(heroesFrom, heroesTo, _getAnimationArea(navigator.context)); _removeHeroesFromOverlay(); - _party.setPerformance(performance); + _party.setAnimation(new CurvedAnimation( + parent: animation, + curve: curve + )); for (_HeroQuestState hero in _party._heroes) { - Widget widget = hero.build(navigator.context, performance); + Widget widget = hero.build(navigator.context, animation); _addHeroToOverlay(widget, hero.tag, navigator.overlay); } } diff --git a/packages/flutter/lib/src/widgets/modal_barrier.dart b/packages/flutter/lib/src/widgets/modal_barrier.dart index d1b9d7c0ca..aa39a3d3dc 100644 --- a/packages/flutter/lib/src/widgets/modal_barrier.dart +++ b/packages/flutter/lib/src/widgets/modal_barrier.dart @@ -43,38 +43,23 @@ class ModalBarrier extends StatelessComponent { } /// Prevents the user from interacting with widgets behind itself. -class AnimatedModalBarrier extends StatelessComponent { +class AnimatedModalBarrier extends AnimatedComponent { AnimatedModalBarrier({ Key key, - this.color, - this.performance, + Animated color, this.dismissable: true - }) : super(key: key); + }) : color = color, super(key: key, animation: color); /// If non-null, fill the barrier with this color. - /// - /// The barrier will animate this color according to the given [performance]. - final AnimatedColorValue color; - - /// The performance to use when animating the given [color]. - final PerformanceView performance; + final Animated color; /// Whether touching the barrier will pop the current route off the [Navigator]. final bool dismissable; Widget build(BuildContext context) { - return new BuilderTransition( - performance: performance, - variables: [color], - builder: (BuildContext context) { - return new IgnorePointer( - ignoring: performance.status == PerformanceStatus.reverse, - child: new ModalBarrier( - color: color.value, - dismissable: dismissable - ) - ); - } + return new ModalBarrier( + color: color.value, + dismissable: dismissable ); } } diff --git a/packages/flutter/lib/src/widgets/pages.dart b/packages/flutter/lib/src/widgets/pages.dart index 322a6ac6f7..92b78cbd63 100644 --- a/packages/flutter/lib/src/widgets/pages.dart +++ b/packages/flutter/lib/src/widgets/pages.dart @@ -21,11 +21,11 @@ abstract class PageRoute extends ModalRoute { bool canTransitionTo(TransitionRoute nextRoute) => nextRoute is PageRoute; bool canTransitionFrom(TransitionRoute nextRoute) => nextRoute is PageRoute; - Performance createPerformanceController() { - Performance performance = super.createPerformanceController(); + AnimationController createAnimationController() { + AnimationController controller = super.createAnimationController(); if (settings.isInitialRoute) - performance.progress = 1.0; - return performance; + controller.value = 1.0; + return controller; } /// Subclasses can override this method to customize how heroes are inserted. diff --git a/packages/flutter/lib/src/widgets/routes.dart b/packages/flutter/lib/src/widgets/routes.dart index 396e433d43..48b86b34e9 100644 --- a/packages/flutter/lib/src/widgets/routes.dart +++ b/packages/flutter/lib/src/widgets/routes.dart @@ -92,25 +92,25 @@ abstract class TransitionRoute extends OverlayRoute { Duration get transitionDuration; bool get opaque; - PerformanceView get performance => _performance; - Performance _performanceController; - PerformanceView _performance; + Animated get animation => _animation; + Animated _animation; + AnimationController _controller; /// Called to create the Performance object that will drive the transitions to /// this route from the previous one, and back to the previous route from this /// one. - Performance createPerformanceController() { + AnimationController createAnimationController() { Duration duration = transitionDuration; assert(duration != null && duration >= Duration.ZERO); - return new Performance(duration: duration, debugLabel: debugLabel); + return new AnimationController(duration: duration, debugLabel: debugLabel); } /// Called to create the PerformanceView that exposes the current progress of /// the transition controlled by the Performance object created by - /// [createPerformanceController()]. - PerformanceView createPerformance() { - assert(_performanceController != null); - return _performanceController.view; + /// [createAnimationController()]. + Animated createAnimation() { + assert(_controller != null); + return _controller.view; } T _result; @@ -134,33 +134,33 @@ abstract class TransitionRoute extends OverlayRoute { } } - PerformanceView get forwardPerformance => _forwardPerformance; - final ProxyPerformance _forwardPerformance = new ProxyPerformance(alwaysDismissedPerformance); + Animated get forwardAnimation => _forwardAnimation; + final ProxyAnimation _forwardAnimation = new ProxyAnimation(kAlwaysDismissedAnimation); void install(OverlayEntry insertionPoint) { - _performanceController = createPerformanceController(); - assert(_performanceController != null); - _performance = createPerformance(); - assert(_performance != null); + _controller = createAnimationController(); + assert(_controller != null); + _animation = createAnimation(); + assert(_animation != null); super.install(insertionPoint); } void didPush() { - _performance.addStatusListener(handleStatusChanged); - _performanceController.forward(); + _animation.addStatusListener(handleStatusChanged); + _controller.forward(); super.didPush(); } void didReplace(Route oldRoute) { if (oldRoute is TransitionRoute) - _performanceController.progress = oldRoute._performanceController.progress; - _performance.addStatusListener(handleStatusChanged); + _controller.value = oldRoute._controller.value; + _animation.addStatusListener(handleStatusChanged); super.didReplace(oldRoute); } bool didPop(T result) { _result = result; - _performanceController.reverse(); + _controller.reverse(); _popCompleter?.complete(_result); return true; } @@ -177,30 +177,30 @@ abstract class TransitionRoute extends OverlayRoute { void _updateForwardPerformance(Route nextRoute) { if (nextRoute is TransitionRoute && canTransitionTo(nextRoute) && nextRoute.canTransitionFrom(this)) { - PerformanceView current = _forwardPerformance.masterPerformance; + Animated current = _forwardAnimation.masterAnimation; if (current != null) { - if (current is TrainHoppingPerformance) { - TrainHoppingPerformance newPerformance; - newPerformance = new TrainHoppingPerformance( + if (current is TrainHoppingAnimation) { + TrainHoppingAnimation newPerformance; + newPerformance = new TrainHoppingAnimation( current.currentTrain, - nextRoute.performance, + nextRoute.animation, onSwitchedTrain: () { - assert(_forwardPerformance.masterPerformance == newPerformance); - assert(newPerformance.currentTrain == nextRoute.performance); - _forwardPerformance.masterPerformance = newPerformance.currentTrain; + assert(_forwardAnimation.masterAnimation == newPerformance); + assert(newPerformance.currentTrain == nextRoute.animation); + _forwardAnimation.masterAnimation = newPerformance.currentTrain; newPerformance.dispose(); } ); - _forwardPerformance.masterPerformance = newPerformance; + _forwardAnimation.masterAnimation = newPerformance; current.dispose(); } else { - _forwardPerformance.masterPerformance = new TrainHoppingPerformance(current, nextRoute.performance); + _forwardAnimation.masterAnimation = new TrainHoppingAnimation(current, nextRoute.animation); } } else { - _forwardPerformance.masterPerformance = nextRoute.performance; + _forwardAnimation.masterAnimation = nextRoute.animation; } } else { - _forwardPerformance.masterPerformance = alwaysDismissedPerformance; + _forwardAnimation.masterAnimation = kAlwaysDismissedAnimation; } } @@ -213,12 +213,12 @@ abstract class TransitionRoute extends OverlayRoute { } void dispose() { - _performanceController.stop(); + _controller.stop(); super.dispose(); } String get debugLabel => '$runtimeType'; - String toString() => '$runtimeType(performance: $_performanceController)'; + String toString() => '$runtimeType(animation: $_controller)'; } class LocalHistoryEntry { @@ -306,8 +306,8 @@ class _ModalScope extends StatefulComponent { class _ModalScopeState extends State<_ModalScope> { void initState() { super.initState(); - config.route.performance?.addStatusListener(_performanceStatusChanged); - config.route.forwardPerformance?.addStatusListener(_performanceStatusChanged); + config.route.animation?.addStatusListener(_animationStatusChanged); + config.route.forwardAnimation?.addStatusListener(_animationStatusChanged); } void didUpdateConfig(_ModalScope oldConfig) { @@ -315,12 +315,12 @@ class _ModalScopeState extends State<_ModalScope> { } void dispose() { - config.route.performance?.removeStatusListener(_performanceStatusChanged); - config.route.forwardPerformance?.removeStatusListener(_performanceStatusChanged); + config.route.animation?.removeStatusListener(_animationStatusChanged); + config.route.forwardAnimation?.removeStatusListener(_animationStatusChanged); super.dispose(); } - void _performanceStatusChanged(PerformanceStatus status) { + void _animationStatusChanged(PerformanceStatus status) { setState(() { // The performances' states are our build state, and they changed already. }); @@ -333,7 +333,7 @@ class _ModalScopeState extends State<_ModalScope> { child: new _ModalScopeStatus( route: config.route, isCurrent: config.route.isCurrent, - child: config.route.buildPage(context, config.route.performance, config.route.forwardPerformance) + child: config.route.buildPage(context, config.route.animation, config.route.forwardAnimation) ) ); if (config.route.offstage) { @@ -342,11 +342,11 @@ class _ModalScopeState extends State<_ModalScope> { contents = new Focus( key: new GlobalObjectKey(config.route), child: new IgnorePointer( - ignoring: config.route.performance?.status == PerformanceStatus.reverse, + ignoring: config.route.animation?.status == PerformanceStatus.reverse, child: config.route.buildTransitions( context, - config.route.performance, - config.route.forwardPerformance, + config.route.animation, + config.route.forwardAnimation, contents ) ) @@ -396,8 +396,8 @@ abstract class ModalRoute extends TransitionRoute with LocalHistoryRoute null; - Widget buildPage(BuildContext context, PerformanceView performance, PerformanceView forwardPerformance); - Widget buildTransitions(BuildContext context, PerformanceView performance, PerformanceView forwardPerformance, Widget child) { + Widget buildPage(BuildContext context, Animated animation, Animated forwardAnimation); + Widget buildTransitions(BuildContext context, Animated animation, Animated forwardAnimation, Widget child) { return child; } @@ -442,17 +442,23 @@ abstract class ModalRoute extends TransitionRoute with LocalHistoryRoute color = new ColorTween( + begin: _kTransparent, + end: barrierColor + ).animate(new CurvedAnimation( + parent: animation, + curve: Curves.ease + )); barrier = new AnimatedModalBarrier( - color: new AnimatedColorValue(_kTransparent, end: barrierColor, curve: Curves.ease), - performance: performance, + color: color, dismissable: barrierDismissable ); } else { barrier = new ModalBarrier(dismissable: barrierDismissable); } - assert(performance.status != PerformanceStatus.dismissed); + assert(animation.status != PerformanceStatus.dismissed); return new IgnorePointer( - ignoring: performance.status == PerformanceStatus.reverse, + ignoring: animation.status == PerformanceStatus.reverse, child: barrier ); } @@ -470,7 +476,7 @@ abstract class ModalRoute extends TransitionRoute with LocalHistoryRoute '$runtimeType($settings, performance: $_performance)'; + String toString() => '$runtimeType($settings, animation: $_animation)'; } /// A modal route that overlays a widget over the current route. diff --git a/packages/flutter/lib/src/widgets/status_transitions.dart b/packages/flutter/lib/src/widgets/status_transitions.dart index 8b3fe3ae1c..e1d873bdf5 100644 --- a/packages/flutter/lib/src/widgets/status_transitions.dart +++ b/packages/flutter/lib/src/widgets/status_transitions.dart @@ -9,12 +9,12 @@ import 'framework.dart'; abstract class StatusTransitionComponent extends StatefulComponent { StatusTransitionComponent({ Key key, - this.performance + this.animation }) : super(key: key) { - assert(performance != null); + assert(animation != null); } - final PerformanceView performance; + final Animated animation; Widget build(BuildContext context); @@ -24,18 +24,18 @@ abstract class StatusTransitionComponent extends StatefulComponent { class _StatusTransitionState extends State { void initState() { super.initState(); - config.performance.addStatusListener(_performanceStatusChanged); + config.animation.addStatusListener(_performanceStatusChanged); } void didUpdateConfig(StatusTransitionComponent oldConfig) { - if (config.performance != oldConfig.performance) { - oldConfig.performance.removeStatusListener(_performanceStatusChanged); - config.performance.addStatusListener(_performanceStatusChanged); + if (config.animation != oldConfig.animation) { + oldConfig.animation.removeStatusListener(_performanceStatusChanged); + config.animation.addStatusListener(_performanceStatusChanged); } } void dispose() { - config.performance.removeStatusListener(_performanceStatusChanged); + config.animation.removeStatusListener(_performanceStatusChanged); super.dispose(); } diff --git a/packages/flutter/lib/src/widgets/transitions.dart b/packages/flutter/lib/src/widgets/transitions.dart index 3d2c30502d..7b9ac7243f 100644 --- a/packages/flutter/lib/src/widgets/transitions.dart +++ b/packages/flutter/lib/src/widgets/transitions.dart @@ -170,21 +170,19 @@ class ScaleTransition extends AnimatedComponent { } } -class RotationTransition extends TransitionWithChild { +class RotationTransition extends AnimatedComponent { RotationTransition({ Key key, - this.turns, - PerformanceView performance, - Widget child - }) : super(key: key, - performance: performance, - child: child); + Animated turns, + this.child + }) : turns = turns, super(key: key, animation: turns); - final AnimatedValue turns; + final Animated turns; + final Widget child; - Widget buildWithChild(BuildContext context, Widget child) { - performance.updateVariable(turns); - Matrix4 transform = new Matrix4.rotationZ(turns.value * math.PI * 2.0); + Widget build(BuildContext context) { + double turnsValue = turns.value; + Matrix4 transform = new Matrix4.rotationZ(turnsValue * math.PI * 2.0); return new Transform( transform: transform, alignment: const FractionalOffset(0.5, 0.5), @@ -307,9 +305,9 @@ class AlignTransition extends TransitionWithChild { /// This class specializes the interpolation of AnimatedValue to /// be appropriate for rectangles that are described in terms of offsets from /// other rectangles. -class AnimatedRelativeRectValue extends AnimatedValue { - AnimatedRelativeRectValue(RelativeRect begin, { RelativeRect end, Curve curve, Curve reverseCurve }) - : super(begin, end: end, curve: curve, reverseCurve: reverseCurve); +class RelativeRectTween extends Tween { + RelativeRectTween({ RelativeRect begin, RelativeRect end }) + : super(begin: begin, end: end); RelativeRect lerp(double t) => RelativeRect.lerp(begin, end, t); } @@ -320,22 +318,19 @@ class AnimatedRelativeRectValue extends AnimatedValue { /// of the performance. /// /// Only works if it's the child of a [Stack]. -class PositionedTransition extends TransitionWithChild { +class PositionedTransition extends AnimatedComponent { PositionedTransition({ Key key, - this.rect, - PerformanceView performance, - Widget child - }) : super(key: key, - performance: performance, - child: child) { + Animated rect, + this.child + }) : rect = rect, super(key: key, animation: rect) { assert(rect != null); } - final AnimatedRelativeRectValue rect; + final Animated rect; + final Widget child; - Widget buildWithChild(BuildContext context, Widget child) { - performance.updateVariable(rect); + Widget build(BuildContext context) { return new Positioned( top: rect.value.top, right: rect.value.right, diff --git a/packages/flutter/test/widget/page_forward_transitions_test.dart b/packages/flutter/test/widget/page_forward_transitions_test.dart index 0002646443..337ba45d86 100644 --- a/packages/flutter/test/widget/page_forward_transitions_test.dart +++ b/packages/flutter/test/widget/page_forward_transitions_test.dart @@ -7,21 +7,22 @@ import 'package:flutter/animation.dart'; import 'package:flutter/material.dart'; import 'package:test/test.dart' hide TypeMatcher; -class TestTransition extends TransitionComponent { +class TestTransition extends AnimatedComponent { TestTransition({ Key key, this.childFirstHalf, this.childSecondHalf, - PerformanceView performance - }) : super(key: key, performance: performance) { - assert(performance != null); + Animated animation + }) : super(key: key, animation: animation) { + assert(animation != null); } final Widget childFirstHalf; final Widget childSecondHalf; Widget build(BuildContext context) { - if (performance.progress >= 0.5) + final Animated animation = this.animation; + if (animation.value >= 0.5) return childSecondHalf; return childFirstHalf; } @@ -32,7 +33,7 @@ class TestRoute extends PageRoute { final Widget child; Duration get transitionDuration => kMaterialPageRouteTransitionDuration; Color get barrierColor => null; - Widget buildPage(BuildContext context, PerformanceView performance, PerformanceView forwardPerformance) { + Widget buildPage(BuildContext context, Animated animation, Animated forwardAnimation) { return child; } } @@ -81,12 +82,12 @@ void main() { new TestTransition( childFirstHalf: new Text('A'), childSecondHalf: new Text('B'), - performance: route.performance + animation: route.animation ), new TestTransition( childFirstHalf: new Text('C'), childSecondHalf: new Text('D'), - performance: route.forwardPerformance + animation: route.forwardAnimation ), ] ); diff --git a/packages/flutter/test/widget/positioned_test.dart b/packages/flutter/test/widget/positioned_test.dart index 9ad1848512..28f2c71baa 100644 --- a/packages/flutter/test/widget/positioned_test.dart +++ b/packages/flutter/test/widget/positioned_test.dart @@ -13,18 +13,17 @@ void main() { test('Can animate position data', () { testWidgets((WidgetTester tester) { - final AnimatedRelativeRectValue rect = new AnimatedRelativeRectValue( - new RelativeRect.fromRect( + final RelativeRectTween rect = new RelativeRectTween( + begin: new RelativeRect.fromRect( new Rect.fromLTRB(10.0, 20.0, 20.0, 30.0), new Rect.fromLTRB(0.0, 10.0, 100.0, 110.0) ), end: new RelativeRect.fromRect( new Rect.fromLTRB(80.0, 90.0, 90.0, 100.0), new Rect.fromLTRB(0.0, 10.0, 100.0, 110.0) - ), - curve: Curves.linear + ) ); - final Performance performance = new Performance( + final AnimationController controller = new AnimationController( duration: const Duration(seconds: 10) ); final List sizes = []; @@ -46,8 +45,7 @@ void main() { child: new Stack( children: [ new PositionedTransition( - rect: rect, - performance: performance, + rect: rect.animate(controller), child: new Container( key: key ) @@ -58,7 +56,7 @@ void main() { ) ); // t=0 recordMetrics(); - performance.play(); + controller.forward(); tester.pump(); // t=0 again recordMetrics(); tester.pump(const Duration(seconds: 1)); // t=1 From 715af6ebe82871bdb01501bfebcba982940b754a Mon Sep 17 00:00:00 2001 From: Adam Barth Date: Tue, 19 Jan 2016 23:03:33 -0800 Subject: [PATCH 3/4] Port Tooltip to AnimationController --- .../flutter/lib/src/material/tooltip.dart | 28 ++++++++++--------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/packages/flutter/lib/src/material/tooltip.dart b/packages/flutter/lib/src/material/tooltip.dart index 60f27d3d86..a59a259dcd 100644 --- a/packages/flutter/lib/src/material/tooltip.dart +++ b/packages/flutter/lib/src/material/tooltip.dart @@ -71,13 +71,13 @@ class Tooltip extends StatefulComponent { class _TooltipState extends State { - Performance _performance; + AnimationController _controller; OverlayEntry _entry; Timer _timer; void initState() { super.initState(); - _performance = new Performance(duration: config.fadeDuration) + _controller = new AnimationController(duration: config.fadeDuration) ..addStatusListener((PerformanceStatus status) { switch (status) { case PerformanceStatus.completed: @@ -100,7 +100,7 @@ class _TooltipState extends State { void didUpdateConfig(Tooltip oldConfig) { super.didUpdateConfig(oldConfig); if (config.fadeDuration != oldConfig.fadeDuration) - _performance.duration = config.fadeDuration; + _controller.duration = config.fadeDuration; if (_entry != null && (config.message != oldConfig.message || config.backgroundColor != oldConfig.backgroundColor || @@ -117,7 +117,7 @@ class _TooltipState extends State { } void resetShowTimer() { - assert(_performance.status == PerformanceStatus.completed); + assert(_controller.status == PerformanceStatus.completed); assert(_entry != null); _timer = new Timer(config.showDuration, hideTooltip); } @@ -136,7 +136,10 @@ class _TooltipState extends State { height: config.height, padding: config.padding, opacity: config.opacity, - performance: _performance, + animation: new CurvedAnimation( + parent: _controller, + curve: Curves.ease + ), target: target, verticalOffset: config.verticalOffset, screenEdgeMargin: config.screenEdgeMargin, @@ -146,9 +149,9 @@ class _TooltipState extends State { Overlay.of(context).insert(_entry); } _timer?.cancel(); - if (_performance.status != PerformanceStatus.completed) { + if (_controller.status != PerformanceStatus.completed) { _timer = null; - _performance.forward(); + _controller.forward(); } else { resetShowTimer(); } @@ -158,7 +161,7 @@ class _TooltipState extends State { assert(_entry != null); _timer?.cancel(); _timer = null; - _performance.reverse(); + _controller.reverse(); } void deactivate() { @@ -232,7 +235,7 @@ class _TooltipOverlay extends StatelessComponent { this.height, this.padding, this.opacity, - this.performance, + this.animation, this.target, this.verticalOffset, this.screenEdgeMargin, @@ -246,7 +249,7 @@ class _TooltipOverlay extends StatelessComponent { final double borderRadius; final double height; final EdgeDims padding; - final PerformanceView performance; + final Animated animation; final Point target; final double verticalOffset; final EdgeDims screenEdgeMargin; @@ -266,9 +269,8 @@ class _TooltipOverlay extends StatelessComponent { screenEdgeMargin: screenEdgeMargin, preferBelow: preferBelow ), - child: new OldFadeTransition( - performance: performance, - opacity: new AnimatedValue(0.0, end: 1.0, curve: Curves.ease), + child: new FadeTransition( + opacity: animation, child: new Opacity( opacity: opacity, child: new Container( From d25951c517d94b79ccfa88179829633ddc66bad5 Mon Sep 17 00:00:00 2001 From: Adam Barth Date: Wed, 20 Jan 2016 09:10:34 -0800 Subject: [PATCH 4/4] AnimatedBuilder should have a child Providing a pre-built child is more efficient because we don't need to rebuild the child every tick of the animation. --- .../lib/demo/page_selector_demo.dart | 2 +- .../lib/demo/progress_indicator_demo.dart | 2 +- examples/widgets/smooth_resize.dart | 5 +- .../lib/src/material/bottom_sheet.dart | 2 +- .../flutter/lib/src/material/dropdown.dart | 5 +- .../flutter/lib/src/material/popup_menu.dart | 5 +- .../lib/src/material/progress_indicator.dart | 2 +- .../flutter/lib/src/material/scaffold.dart | 14 +++--- .../flutter/lib/src/material/snack_bar.dart | 48 +++++++++---------- .../flutter/lib/src/widgets/transitions.dart | 10 ++-- 10 files changed, 51 insertions(+), 44 deletions(-) diff --git a/examples/material_gallery/lib/demo/page_selector_demo.dart b/examples/material_gallery/lib/demo/page_selector_demo.dart index c6611b64c2..594724bcb0 100644 --- a/examples/material_gallery/lib/demo/page_selector_demo.dart +++ b/examples/material_gallery/lib/demo/page_selector_demo.dart @@ -15,7 +15,7 @@ class PageSelectorDemo extends StatelessComponent { CurvedAnimation animation = new CurvedAnimation(parent: selection.animation, curve: Curves.ease); return new AnimatedBuilder( animation: animation, - builder: (BuildContext context) { + builder: (BuildContext context, Widget child) { Color background = selection.value == iconName ? _selectedColor.end : _selectedColor.begin; if (selection.valueIsChanging) { // Then the selection's performance is animating from previousValue to value. diff --git a/examples/material_gallery/lib/demo/progress_indicator_demo.dart b/examples/material_gallery/lib/demo/progress_indicator_demo.dart index 2ca0b76d19..a3c6a1d772 100644 --- a/examples/material_gallery/lib/demo/progress_indicator_demo.dart +++ b/examples/material_gallery/lib/demo/progress_indicator_demo.dart @@ -47,7 +47,7 @@ class _ProgressIndicatorDemoState extends State { controller.play(direction); } - Widget buildIndicators(BuildContext context) { + Widget buildIndicators(BuildContext context, Widget child) { List indicators = [ new SizedBox( width: 200.0, diff --git a/examples/widgets/smooth_resize.dart b/examples/widgets/smooth_resize.dart index 48a8033b99..8b09c972c1 100644 --- a/examples/widgets/smooth_resize.dart +++ b/examples/widgets/smooth_resize.dart @@ -41,7 +41,7 @@ class CardTransition extends StatelessComponent { Widget build(BuildContext context) { return new AnimatedBuilder( animation: animation, - builder: (BuildContext context) { + builder: (BuildContext context, Widget child) { double currentScale = scale.evaluate(animation); Matrix4 transform = new Matrix4.identity() ..translate(x.evaluate(animation)) @@ -53,7 +53,8 @@ class CardTransition extends StatelessComponent { child: child ) ); - } + }, + child: child ); } } diff --git a/packages/flutter/lib/src/material/bottom_sheet.dart b/packages/flutter/lib/src/material/bottom_sheet.dart index 0274e54b11..965d04e275 100644 --- a/packages/flutter/lib/src/material/bottom_sheet.dart +++ b/packages/flutter/lib/src/material/bottom_sheet.dart @@ -134,7 +134,7 @@ class _ModalBottomSheetState extends State<_ModalBottomSheet> { onTap: () => Navigator.pop(context), child: new AnimatedBuilder( animation: config.route.animation, - builder: (BuildContext context) { + builder: (BuildContext context, Widget child) { return new ClipRect( child: new CustomOneChildLayout( delegate: new _ModalBottomSheetLayout(config.route.animation.value), diff --git a/packages/flutter/lib/src/material/dropdown.dart b/packages/flutter/lib/src/material/dropdown.dart index 46e7aca108..39560cb846 100644 --- a/packages/flutter/lib/src/material/dropdown.dart +++ b/packages/flutter/lib/src/material/dropdown.dart @@ -131,7 +131,7 @@ class _DropDownMenu extends StatusTransitionComponent { opacity: opacity, child: new AnimatedBuilder( animation: resize, - builder: (BuildContext context) { + builder: (BuildContext context, Widget child) { return new CustomPaint( painter: new _DropDownMenuPainter( color: Theme.of(context).canvasColor, @@ -142,7 +142,8 @@ class _DropDownMenu extends StatusTransitionComponent { ), child: child ); - } + }, + child: child ) ); } diff --git a/packages/flutter/lib/src/material/popup_menu.dart b/packages/flutter/lib/src/material/popup_menu.dart index c3e53fadbc..29fef63899 100644 --- a/packages/flutter/lib/src/material/popup_menu.dart +++ b/packages/flutter/lib/src/material/popup_menu.dart @@ -96,7 +96,7 @@ class _PopupMenu extends StatelessComponent { return new AnimatedBuilder( animation: route.animation, - builder: (BuildContext context) { + builder: (BuildContext context, Widget child) { return new Opacity( opacity: opacity.evaluate(route.animation), child: new Material( @@ -110,7 +110,8 @@ class _PopupMenu extends StatelessComponent { ) ) ); - } + }, + child: child ); } } diff --git a/packages/flutter/lib/src/material/progress_indicator.dart b/packages/flutter/lib/src/material/progress_indicator.dart index 4fbc826b0f..026b0c1099 100644 --- a/packages/flutter/lib/src/material/progress_indicator.dart +++ b/packages/flutter/lib/src/material/progress_indicator.dart @@ -63,7 +63,7 @@ class _ProgressIndicatorState extends State { return new AnimatedBuilder( animation: _animation, - builder: (BuildContext context) { + builder: (BuildContext context, Widget child) { return config._buildIndicator(context, _animation.value); } ); diff --git a/packages/flutter/lib/src/material/scaffold.dart b/packages/flutter/lib/src/material/scaffold.dart index 4b269c0772..1a8e5bbfc1 100644 --- a/packages/flutter/lib/src/material/scaffold.dart +++ b/packages/flutter/lib/src/material/scaffold.dart @@ -473,20 +473,20 @@ class _PersistentBottomSheetState extends State<_PersistentBottomSheet> { } Widget build(BuildContext context) { - Widget child = new BottomSheet( - animationController: config.animationController, - onClosing: config.onClosing, - builder: config.builder - ); return new AnimatedBuilder( animation: config.animationController, - builder: (BuildContext context) { + builder: (BuildContext context, Widget child) { return new Align( alignment: const FractionalOffset(0.0, 0.0), heightFactor: config.animationController.value, child: child ); - } + }, + child: new BottomSheet( + animationController: config.animationController, + onClosing: config.onClosing, + builder: config.builder + ) ); } diff --git a/packages/flutter/lib/src/material/snack_bar.dart b/packages/flutter/lib/src/material/snack_bar.dart index 1625dcd401..35ca86e1ca 100644 --- a/packages/flutter/lib/src/material/snack_bar.dart +++ b/packages/flutter/lib/src/material/snack_bar.dart @@ -85,38 +85,38 @@ class SnackBar extends StatelessComponent { CurvedAnimation heightAnimation = new CurvedAnimation(parent: animation, curve: _snackBarHeightCurve); CurvedAnimation fadeAnimation = new CurvedAnimation(parent: animation, curve: _snackBarFadeCurve); ThemeData theme = Theme.of(context); - Widget child = new Material( - elevation: 6, - color: _kSnackBackground, - child: new Container( - margin: const EdgeDims.symmetric(horizontal: _kSideMargins), - child: new Theme( - data: new ThemeData( - brightness: ThemeBrightness.dark, - accentColor: theme.accentColor, - accentColorBrightness: theme.accentColorBrightness, - text: Typography.white - ), - child: new FadeTransition( - opacity: fadeAnimation, - child: new Row( - children: children, - alignItems: FlexAlignItems.center - ) - ) - ) - ) - ); return new ClipRect( child: new AnimatedBuilder( animation: heightAnimation, - builder: (BuildContext context) { + builder: (BuildContext context, Widget child) { return new Align( alignment: const FractionalOffset(0.0, 0.0), heightFactor: heightAnimation.value, child: child ); - } + }, + child: new Material( + elevation: 6, + color: _kSnackBackground, + child: new Container( + margin: const EdgeDims.symmetric(horizontal: _kSideMargins), + child: new Theme( + data: new ThemeData( + brightness: ThemeBrightness.dark, + accentColor: theme.accentColor, + accentColorBrightness: theme.accentColorBrightness, + text: Typography.white + ), + child: new FadeTransition( + opacity: fadeAnimation, + child: new Row( + children: children, + alignItems: FlexAlignItems.center + ) + ) + ) + ) + ) ) ); } diff --git a/packages/flutter/lib/src/widgets/transitions.dart b/packages/flutter/lib/src/widgets/transitions.dart index 7b9ac7243f..9f04c00ff5 100644 --- a/packages/flutter/lib/src/widgets/transitions.dart +++ b/packages/flutter/lib/src/widgets/transitions.dart @@ -360,16 +360,20 @@ class BuilderTransition extends TransitionComponent { } } +typedef Widget TransitionBuilder(BuildContext context, Widget child); + class AnimatedBuilder extends AnimatedComponent { AnimatedBuilder({ Key key, Animated animation, - this.builder + this.builder, + this.child }) : super(key: key, animation: animation); - final WidgetBuilder builder; + final TransitionBuilder builder; + final Widget child; Widget build(BuildContext context) { - return builder(context); + return builder(context, child); } }