diff --git a/examples/material_gallery/lib/demo/progress_indicator_demo.dart b/examples/material_gallery/lib/demo/progress_indicator_demo.dart index a3c6a1d772..9babb5f58e 100644 --- a/examples/material_gallery/lib/demo/progress_indicator_demo.dart +++ b/examples/material_gallery/lib/demo/progress_indicator_demo.dart @@ -21,13 +21,13 @@ class _ProgressIndicatorDemoState extends State { parent: controller, curve: new Interval(0.0, 0.9, curve: Curves.ease), reverseCurve: Curves.ease - )..addStatusListener((PerformanceStatus status) { - if (status == PerformanceStatus.dismissed || status == PerformanceStatus.completed) + )..addStatusListener((AnimationStatus status) { + if (status == AnimationStatus.dismissed || status == AnimationStatus.completed) reverseValueAnimationDirection(); }); } - Animated animation; + Animation animation; AnimationController controller; void handleTap() { diff --git a/examples/widgets/smooth_resize.dart b/examples/widgets/smooth_resize.dart index 8b09c972c1..9d4486c86c 100644 --- a/examples/widgets/smooth_resize.dart +++ b/examples/widgets/smooth_resize.dart @@ -33,10 +33,10 @@ class CardTransition extends StatelessComponent { }); final Widget child; - final Animated animation; - final Evaluatable x; - final Evaluatable opacity; - final Evaluatable scale; + final Animation animation; + final Animatable x; + final Animatable opacity; + final Animatable scale; Widget build(BuildContext context) { return new AnimatedBuilder( @@ -63,7 +63,7 @@ class SmoothBlockState extends State { double _height = 100.0; - Widget _handleEnter(Animated animation, Widget child) { + Widget _handleEnter(Animation animation, Widget child) { return new CardTransition( x: new Tween(begin: -200.0, end: 0.0), opacity: new Tween(begin: 0.0, end: 1.0), @@ -73,7 +73,7 @@ class SmoothBlockState extends State { ); } - Widget _handleExit(Animated animation, Widget child) { + Widget _handleExit(Animation 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/animation.dart b/packages/flutter/lib/animation.dart index 64fdc8287f..fd1afc53a2 100644 --- a/packages/flutter/lib/animation.dart +++ b/packages/flutter/lib/animation.dart @@ -7,13 +7,13 @@ /// This library depends only on core Dart libraries and the `newton` package. library animation; -export 'src/animation/animated_value.dart'; +export 'src/animation/animation.dart'; +export 'src/animation/animation_controller.dart'; export 'src/animation/animations.dart'; export 'src/animation/clamped_simulation.dart'; export 'src/animation/curves.dart'; export 'src/animation/forces.dart'; export 'src/animation/listener_helpers.dart'; -export 'src/animation/performance.dart'; export 'src/animation/scroll_behavior.dart'; export 'src/animation/simulation_stepper.dart'; export 'src/animation/ticker.dart'; diff --git a/packages/flutter/lib/src/animation/animated_value.dart b/packages/flutter/lib/src/animation/animated_value.dart deleted file mode 100644 index ad49d46c8e..0000000000 --- a/packages/flutter/lib/src/animation/animated_value.dart +++ /dev/null @@ -1,129 +0,0 @@ -// Copyright 2015 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 Color, Size, Rect; - -import 'curves.dart'; - -/// The direction in which an animation is running. -enum AnimationDirection { - /// The animation is running from beginning to end. - forward, - - /// The animation is running backwards, from end to beginning. - reverse -} - -/// An interface describing a variable that changes as an animation progresses. -/// -/// Animatable objects, by convention, must be cheap to create. This allows them -/// to be used in build functions in Widgets. -abstract class Animatable { - // TODO(ianh): replace mentions of this class with just mentioning AnimatedValue directly - /// Update the variable to a given time in an animation that is running in the given direction - void setProgress(double t, AnimationDirection direction); -} - -/// An animated variable with a concrete type. -class AnimatedValue implements Animatable { - AnimatedValue(this.begin, { this.end, this.curve, this.reverseCurve }) { - value = begin; - } - - /// The current value of this variable. - T value; - - /// The value this variable has at the beginning of the animation. - T begin; - - /// The value this variable has at the end of the animation. - T end; - - /// Returns the value this variable has at the given animation clock value. - T lerp(double t) => begin + (end - begin) * t; - - /// The curve to use in the forward direction. - Curve curve; - - /// The curve to use in the reverse direction. - /// - /// If this field is null, uses [curve] in both directions. - Curve reverseCurve; - - Curve _getActiveCurve(AnimationDirection direction) { - if (direction == AnimationDirection.forward || reverseCurve == null) - return curve; - return reverseCurve; - } - - double transform(double t, AnimationDirection direction) { - Curve activeCurve = _getActiveCurve(direction); - if (activeCurve == null) - return t; - if (t == 0.0 || t == 1.0) { - assert(activeCurve.transform(t).round() == t); - return t; - } - return activeCurve.transform(t); - } - - /// Updates the value of this variable according to the given animation clock - /// value and direction. - void setProgress(double t, AnimationDirection direction) { - if (end != null) { - t = transform(t, direction); - if (t == 0.0) - value = begin; - else if (t == 1.0) - value = end; - else - value = lerp(t); - } - } - - String toString() => 'AnimatedValue(begin=$begin, end=$end, value=$value)'; -} - -/// An animated variable containing a color. -/// -/// This class specializes the interpolation of AnimatedValue to be -/// appropriate for colors. -class AnimatedColorValue extends AnimatedValue { - AnimatedColorValue(Color begin, { Color end, Curve curve, Curve reverseCurve }) - : super(begin, end: end, curve: curve, reverseCurve: reverseCurve); - - Color lerp(double t) => Color.lerp(begin, end, t); -} - -/// An animated variable containing a size. -/// -/// This class specializes the interpolation of AnimatedValue to be -/// appropriate for rectangles. -class AnimatedSizeValue extends AnimatedValue { - AnimatedSizeValue(Size begin, { Size end, Curve curve, Curve reverseCurve }) - : super(begin, end: end, curve: curve, reverseCurve: reverseCurve); - - Size lerp(double t) => Size.lerp(begin, end, t); -} - -/// An animated variable containing a rectangle. -/// -/// This class specializes the interpolation of AnimatedValue to be -/// appropriate for rectangles. -class AnimatedRectValue extends AnimatedValue { - AnimatedRectValue(Rect begin, { Rect end, Curve curve, Curve reverseCurve }) - : super(begin, end: end, curve: curve, reverseCurve: reverseCurve); - - Rect lerp(double t) => Rect.lerp(begin, end, t); -} - -/// An animated variable containing a int. -class AnimatedIntValue extends AnimatedValue { - AnimatedIntValue(int begin, { int end, Curve curve, Curve reverseCurve }) - : super(begin, end: end, curve: curve, reverseCurve: reverseCurve); - - // The inherited lerp() function doesn't work with ints because it multiplies - // the begin and end types by a double, and int * double returns a double. - int lerp(double t) => (begin + (end - begin) * t).round(); -} diff --git a/packages/flutter/lib/src/animation/animation.dart b/packages/flutter/lib/src/animation/animation.dart new file mode 100644 index 0000000000..f45035b0a9 --- /dev/null +++ b/packages/flutter/lib/src/animation/animation.dart @@ -0,0 +1,98 @@ +// 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 Color, Size, Rect, VoidCallback, lerpDouble; + +/// The direction in which an animation is running. +enum AnimationDirection { + /// The animation is running from beginning to end. + forward, + + /// The animation is running backwards, from end to beginning. + reverse +} + +/// The status of an animation +enum AnimationStatus { + /// The animation is stopped at the beginning + dismissed, + + /// The animation is running from beginning to end + forward, + + /// The animation is running backwards, from end to beginning + reverse, + + /// The animation is stopped at the end + completed, +} + +typedef void AnimationStatusListener(AnimationStatus status); + +abstract class Animation { + const Animation(); + + /// Calls the listener every time the value of the animation changes. + void addListener(VoidCallback listener); + /// Stop calling the listener every time the value of the animation changes. + void removeListener(VoidCallback listener); + /// Calls listener every time the status of the animation changes. + void addStatusListener(AnimationStatusListener listener); + /// Stops calling the listener every time the status of the animation changes. + void removeStatusListener(AnimationStatusListener listener); + + /// The current status of this animation. + AnimationStatus get status; + + /// 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 == AnimationStatus.dismissed; + + /// Whether this animation is stopped at the end. + bool get isCompleted => status == AnimationStatus.completed; + + String toString() { + return '$runtimeType(${toStringDetails()})'; + } + String toStringDetails() { + assert(status != null); + assert(direction != null); + String icon; + switch (status) { + case AnimationStatus.forward: + icon = '\u25B6'; // > + break; + case AnimationStatus.reverse: + icon = '\u25C0'; // < + break; + case AnimationStatus.completed: + switch (direction) { + case AnimationDirection.forward: + icon = '\u23ED'; // >>| + break; + case AnimationDirection.reverse: + icon = '\u29CF'; // <| + break; + } + break; + case AnimationStatus.dismissed: + switch (direction) { + case AnimationDirection.forward: + icon = '\u29D0'; // |> + break; + case AnimationDirection.reverse: + icon = '\u23EE'; // |<< + break; + } + break; + } + assert(icon != null); + return '$icon'; + } +} diff --git a/packages/flutter/lib/src/animation/animation_controller.dart b/packages/flutter/lib/src/animation/animation_controller.dart new file mode 100644 index 0000000000..bdf1b26379 --- /dev/null +++ b/packages/flutter/lib/src/animation/animation_controller.dart @@ -0,0 +1,157 @@ +// 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:async'; +import 'dart:ui' show Color, Size, Rect, VoidCallback, lerpDouble; + +import 'package:newton/newton.dart'; + +import 'animation.dart'; +import 'forces.dart'; +import 'listener_helpers.dart'; +import 'simulation_stepper.dart'; + +class AnimationController extends Animation + with EagerListenerMixin, LocalPerformanceListenersMixin, LocalPerformanceStatusListenersMixin { + AnimationController({ this.duration, double value, this.debugLabel }) { + _timeline = new SimulationStepper(_tick); + if (value != null) + _timeline.value = value.clamp(0.0, 1.0); + } + + /// A label that is used in the [toString] output. Intended to aid with + /// identifying animation controller instances in debug output. + final String debugLabel; + + /// Returns a [Animated] for this animation controller, + /// so that a pointer to this object can be passed around without + /// allowing users of that pointer to mutate the AnimationController state. + Animation get view => this; + + /// The length of time this animation should last. + Duration duration; + + SimulationStepper _timeline; + AnimationDirection get direction => _direction; + AnimationDirection _direction = AnimationDirection.forward; + + /// The progress of this animation along the timeline. + /// + /// Note: Setting this value stops the current animation. + 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(); + } + + /// Whether this animation is currently animating in either the forward or reverse direction. + bool get isAnimating => _timeline.isAnimating; + + AnimationStatus get status { + if (!isAnimating && value == 1.0) + return AnimationStatus.completed; + if (!isAnimating && value == 0.0) + return AnimationStatus.dismissed; + return _direction == AnimationDirection.forward ? + AnimationStatus.forward : + AnimationStatus.reverse; + } + + /// Starts running this animation forwards (towards the end). + Future forward() => play(AnimationDirection.forward); + + /// Starts running this animation in reverse (towards the beginning). + Future reverse() => play(AnimationDirection.reverse); + + /// Starts running this animation in the given direction. + Future play(AnimationDirection direction) { + _direction = direction; + return resume(); + } + + /// Resumes this animation in the most recent direction. + Future resume() { + return _animateTo(_direction == AnimationDirection.forward ? 1.0 : 0.0); + } + + /// Stops running this animation. + void stop() { + _timeline.stop(); + } + + /// Releases any resources used by this object. + /// + /// Same as stop(). + void dispose() { + stop(); + } + + /// + /// Flings the timeline with an optional force (defaults to a critically + /// damped spring) and initial velocity. If velocity is positive, the + /// animation will complete, otherwise it will dismiss. + Future fling({double velocity: 1.0, Force force}) { + force ??= kDefaultSpringForce; + _direction = velocity < 0.0 ? AnimationDirection.reverse : AnimationDirection.forward; + return _timeline.animateWith(force.release(value, velocity)); + } + + /// Starts running this animation in the forward direction, and + /// restarts the animation when it completes. + Future repeat({ double min: 0.0, double max: 1.0, Duration period }) { + period ??= duration; + return _timeline.animateWith(new _RepeatingSimulation(min, max, period)); + } + + AnimationStatus _lastStatus = AnimationStatus.dismissed; + void _checkStatusChanged() { + AnimationStatus currentStatus = status; + if (currentStatus != _lastStatus) + notifyStatusListeners(status); + _lastStatus = currentStatus; + } + + Future _animateTo(double target) { + Duration remainingDuration = duration * (target - _timeline.value).abs(); + _timeline.stop(); + if (remainingDuration == Duration.ZERO) + return new Future.value(); + return _timeline.animateTo(target, duration: remainingDuration); + } + + void _tick(double t) { + notifyListeners(); + _checkStatusChanged(); + } + + String toStringDetails() { + String paused = _timeline.isAnimating ? '' : '; paused'; + String label = debugLabel == null ? '' : '; for $debugLabel'; + String more = '${super.toStringDetails()} ${value.toStringAsFixed(3)}'; + return '$more$paused$label'; + } +} + +class _RepeatingSimulation extends Simulation { + _RepeatingSimulation(this.min, this.max, Duration period) + : _periodInSeconds = period.inMicroseconds / Duration.MICROSECONDS_PER_SECOND { + assert(_periodInSeconds > 0.0); + } + + final double min; + final double max; + + final double _periodInSeconds; + + double x(double timeInSeconds) { + assert(timeInSeconds >= 0.0); + final double t = (timeInSeconds / _periodInSeconds) % 1.0; + return lerpDouble(min, max, t); + } + + double dx(double timeInSeconds) => 1.0; + + bool isDone(double timeInSeconds) => false; +} diff --git a/packages/flutter/lib/src/animation/animations.dart b/packages/flutter/lib/src/animation/animations.dart index a969d6db24..b5585dc44e 100644 --- a/packages/flutter/lib/src/animation/animations.dart +++ b/packages/flutter/lib/src/animation/animations.dart @@ -4,56 +4,81 @@ import 'dart:ui' show VoidCallback; -import 'animated_value.dart'; +import 'animation.dart'; +import 'curves.dart'; import 'listener_helpers.dart'; -import 'tween.dart'; -class AlwaysCompleteAnimation extends Animated { +class AlwaysCompleteAnimation extends Animation { const AlwaysCompleteAnimation(); void addListener(VoidCallback listener) { } void removeListener(VoidCallback listener) { } - void addStatusListener(PerformanceStatusListener listener) { } - void removeStatusListener(PerformanceStatusListener listener) { } - PerformanceStatus get status => PerformanceStatus.completed; + void addStatusListener(AnimationStatusListener listener) { } + void removeStatusListener(AnimationStatusListener listener) { } + AnimationStatus get status => AnimationStatus.completed; AnimationDirection get direction => AnimationDirection.forward; double get value => 1.0; } const AlwaysCompleteAnimation kAlwaysCompleteAnimation = const AlwaysCompleteAnimation(); -class AlwaysDismissedAnimation extends Animated { +class AlwaysDismissedAnimation extends Animation { const AlwaysDismissedAnimation(); void addListener(VoidCallback listener) { } void removeListener(VoidCallback listener) { } - void addStatusListener(PerformanceStatusListener listener) { } - void removeStatusListener(PerformanceStatusListener listener) { } - PerformanceStatus get status => PerformanceStatus.dismissed; + void addStatusListener(AnimationStatusListener listener) { } + void removeStatusListener(AnimationStatusListener listener) { } + AnimationStatus get status => AnimationStatus.dismissed; AnimationDirection get direction => AnimationDirection.forward; double get value => 0.0; } const AlwaysDismissedAnimation kAlwaysDismissedAnimation = const AlwaysDismissedAnimation(); -class ProxyAnimation extends Animated +class AlwaysStoppedAnimation extends Animation { + const AlwaysStoppedAnimation(this.value); + + final double value; + + void addListener(VoidCallback listener) { } + void removeListener(VoidCallback listener) { } + void addStatusListener(AnimationStatusListener listener) { } + void removeStatusListener(AnimationStatusListener listener) { } + AnimationStatus get status => AnimationStatus.forward; + AnimationDirection get direction => AnimationDirection.forward; +} + +abstract class ProxyAnimatedMixin { + Animation get parent; + + void addListener(VoidCallback listener) => parent.addListener(listener); + void removeListener(VoidCallback listener) => parent.removeListener(listener); + void addStatusListener(AnimationStatusListener listener) => parent.addStatusListener(listener); + void removeStatusListener(AnimationStatusListener listener) => parent.removeStatusListener(listener); + + AnimationStatus get status => parent.status; + AnimationDirection get direction => parent.direction; +} + +class ProxyAnimation extends Animation with LazyListenerMixin, LocalPerformanceListenersMixin, LocalPerformanceStatusListenersMixin { - ProxyAnimation([Animated animation]) { + ProxyAnimation([Animation animation]) { _masterAnimation = animation; if (_masterAnimation == null) { - _status = PerformanceStatus.dismissed; + _status = AnimationStatus.dismissed; _direction = AnimationDirection.forward; _value = 0.0; } } - PerformanceStatus _status; + AnimationStatus _status; AnimationDirection _direction; double _value; - Animated get masterAnimation => _masterAnimation; - Animated _masterAnimation; - void set masterAnimation(Animated value) { + Animation get masterAnimation => _masterAnimation; + Animation _masterAnimation; + void set masterAnimation(Animation value) { if (value == _masterAnimation) return; if (_masterAnimation != null) { @@ -91,16 +116,16 @@ class ProxyAnimation extends Animated } } - PerformanceStatus get status => _masterAnimation != null ? _masterAnimation.status : _status; + AnimationStatus 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 +class ReverseAnimation extends Animation with LazyListenerMixin, LocalPerformanceStatusListenersMixin { ReverseAnimation(this.masterAnimation); - final Animated masterAnimation; + final Animation masterAnimation; void addListener(VoidCallback listener) { didRegisterListener(); @@ -119,20 +144,20 @@ class ReverseAnimation extends Animated masterAnimation.removeStatusListener(_statusChangeHandler); } - void _statusChangeHandler(PerformanceStatus status) { + void _statusChangeHandler(AnimationStatus status) { notifyStatusListeners(_reverseStatus(status)); } - PerformanceStatus get status => _reverseStatus(masterAnimation.status); + AnimationStatus get status => _reverseStatus(masterAnimation.status); AnimationDirection get direction => _reverseDirection(masterAnimation.direction); double get value => 1.0 - masterAnimation.value; - PerformanceStatus _reverseStatus(PerformanceStatus status) { + AnimationStatus _reverseStatus(AnimationStatus 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; + case AnimationStatus.forward: return AnimationStatus.reverse; + case AnimationStatus.reverse: return AnimationStatus.forward; + case AnimationStatus.completed: return AnimationStatus.dismissed; + case AnimationStatus.dismissed: return AnimationStatus.completed; } } @@ -144,6 +169,64 @@ class ReverseAnimation extends Animated } } +class CurvedAnimation extends Animation with ProxyAnimatedMixin { + CurvedAnimation({ + this.parent, + this.curve: Curves.linear, + this.reverseCurve + }) { + assert(parent != null); + assert(curve != null); + parent.addStatusListener(_handleStatusChanged); + } + + final Animation parent; + + /// The curve to use in the forward direction. + Curve curve; + + /// The curve to use in the reverse direction. + /// + /// If this field is null, uses [curve] in both directions. + Curve reverseCurve; + + /// The direction used to select the current curve. + /// + /// The curve direction is only reset when we hit the beginning or the end of + /// the timeline to avoid discontinuities in the value of any variables this + /// a animation is used to animate. + AnimationDirection _curveDirection; + + void _handleStatusChanged(AnimationStatus status) { + switch (status) { + case AnimationStatus.dismissed: + case AnimationStatus.completed: + _curveDirection = null; + break; + case AnimationStatus.forward: + _curveDirection ??= AnimationDirection.forward; + break; + case AnimationStatus.reverse: + _curveDirection ??= AnimationDirection.reverse; + break; + } + } + + double get value { + final bool useForwardCurve = reverseCurve == null || (_curveDirection ?? parent.direction) == AnimationDirection.forward; + Curve activeCurve = useForwardCurve ? curve : reverseCurve; + + double t = parent.value; + if (activeCurve == null) + return t; + if (t == 0.0 || t == 1.0) { + assert(activeCurve.transform(t).round() == t); + return t; + } + return activeCurve.transform(t); + } +} + enum _TrainHoppingMode { minimize, maximize } /// This animation starts by proxying one animation, but can be given a @@ -156,7 +239,7 @@ enum _TrainHoppingMode { minimize, maximize } /// 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 +class TrainHoppingAnimation extends Animation with EagerListenerMixin, LocalPerformanceListenersMixin, LocalPerformanceStatusListenersMixin { TrainHoppingAnimation(this._currentTrain, this._nextTrain, { this.onSwitchedTrain }) { assert(_currentTrain != null); @@ -178,15 +261,15 @@ class TrainHoppingAnimation extends Animated assert(_mode != null); } - Animated get currentTrain => _currentTrain; - Animated _currentTrain; - Animated _nextTrain; + Animation get currentTrain => _currentTrain; + Animation _currentTrain; + Animation _nextTrain; _TrainHoppingMode _mode; VoidCallback onSwitchedTrain; - PerformanceStatus _lastStatus; - void _statusChangeHandler(PerformanceStatus status) { + AnimationStatus _lastStatus; + void _statusChangeHandler(AnimationStatus status) { assert(_currentTrain != null); if (status != _lastStatus) { notifyListeners(); @@ -195,7 +278,7 @@ class TrainHoppingAnimation extends Animated assert(_lastStatus != null); } - PerformanceStatus get status => _currentTrain.status; + AnimationStatus get status => _currentTrain.status; AnimationDirection get direction => _currentTrain.direction; double _lastValue; diff --git a/packages/flutter/lib/src/animation/listener_helpers.dart b/packages/flutter/lib/src/animation/listener_helpers.dart index 9fb9640b47..174f2e1a6e 100644 --- a/packages/flutter/lib/src/animation/listener_helpers.dart +++ b/packages/flutter/lib/src/animation/listener_helpers.dart @@ -4,22 +4,7 @@ import 'dart:ui' show VoidCallback; -/// The status of an animation -enum PerformanceStatus { - /// The animation is stopped at the beginning - dismissed, - - /// The animation is running from beginning to end - forward, - - /// The animation is running backwards, from end to beginning - reverse, - - /// The animation is stopped at the end - completed, -} - -typedef void PerformanceStatusListener(PerformanceStatus status); +import 'animation.dart'; abstract class _ListenerMixin { void didRegisterListener(); @@ -71,18 +56,18 @@ abstract class LocalPerformanceListenersMixin extends _ListenerMixin { } abstract class LocalPerformanceStatusListenersMixin extends _ListenerMixin { - final List _statusListeners = []; - void addStatusListener(PerformanceStatusListener listener) { + final List _statusListeners = []; + void addStatusListener(AnimationStatusListener listener) { didRegisterListener(); _statusListeners.add(listener); } - void removeStatusListener(PerformanceStatusListener listener) { + void removeStatusListener(AnimationStatusListener listener) { _statusListeners.remove(listener); didUnregisterListener(); } - void notifyStatusListeners(PerformanceStatus status) { - List localListeners = new List.from(_statusListeners); - for (PerformanceStatusListener listener in localListeners) + void notifyStatusListeners(AnimationStatus status) { + List localListeners = new List.from(_statusListeners); + for (AnimationStatusListener listener in localListeners) listener(status); } } diff --git a/packages/flutter/lib/src/animation/performance.dart b/packages/flutter/lib/src/animation/performance.dart deleted file mode 100644 index e083e2f906..0000000000 --- a/packages/flutter/lib/src/animation/performance.dart +++ /dev/null @@ -1,732 +0,0 @@ -// Copyright 2015 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:async'; -import 'dart:ui' show VoidCallback, lerpDouble; - -import 'package:newton/newton.dart'; - -import 'animated_value.dart'; -import 'curves.dart'; -import 'forces.dart'; -import 'listener_helpers.dart'; -import 'simulation_stepper.dart'; - -/// A read-only view of a [Performance]. -/// -/// This interface is implemented by [Performance]. -/// -/// Read-only access to [Performance] is used by classes that -/// want to watch a performance but should not be able to change the -/// performance's state. -abstract class PerformanceView { - const PerformanceView(); - - /// Update the given variable according to the current progress of the performance. - void updateVariable(Animatable variable); - /// Calls the listener every time the progress of the performance changes. - void addListener(VoidCallback listener); - /// Stop calling the listener every time the progress of the performance changes. - void removeListener(VoidCallback listener); - /// Calls listener every time the status of the performance changes. - void addStatusListener(PerformanceStatusListener listener); - /// Stops calling the listener every time the status of the performance changes. - void removeStatusListener(PerformanceStatusListener listener); - - /// The current status of this animation. - PerformanceStatus get status; - - /// The current direction of the animation. - AnimationDirection get direction; - - /// The direction used to select the current curve. - /// - /// The curve direction is only reset when we hit the beginning or the end of - /// the timeline to avoid discontinuities in the value of any variables this - /// performance is used to animate. - AnimationDirection get curveDirection; - - /// The current progress of this animation (a value from 0.0 to 1.0). - /// - /// This is the value that is used to update any variables when using - /// [updateVariable]. - double get progress; - - /// Whether this animation is stopped at the beginning. - bool get isDismissed => status == PerformanceStatus.dismissed; - - /// Whether this animation is stopped at the end. - bool get isCompleted => status == PerformanceStatus.completed; - - String toString() { - return '$runtimeType(${toStringDetails()})'; - } - String toStringDetails() { - assert(status != null); - assert(direction != null); - String icon; - switch (status) { - case PerformanceStatus.forward: - icon = '\u25B6'; // > - break; - case PerformanceStatus.reverse: - icon = '\u25C0'; // < - break; - case PerformanceStatus.completed: - switch (direction) { - case AnimationDirection.forward: - icon = '\u23ED'; // >>| - break; - case AnimationDirection.reverse: - icon = '\u29CF'; // <| - break; - } - break; - case PerformanceStatus.dismissed: - switch (direction) { - case AnimationDirection.forward: - icon = '\u29D0'; // |> - break; - case AnimationDirection.reverse: - icon = '\u23EE'; // |<< - break; - } - break; - } - assert(icon != null); - return '$icon ${progress.toStringAsFixed(3)}'; - } -} - -class AlwaysCompletePerformance extends PerformanceView { - const AlwaysCompletePerformance(); - - void updateVariable(Animatable variable) { - variable.setProgress(1.0, AnimationDirection.forward); - } - - // 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; - AnimationDirection get curveDirection => AnimationDirection.forward; - double get progress => 1.0; -} -const AlwaysCompletePerformance alwaysCompletePerformance = const AlwaysCompletePerformance(); - -class AlwaysDismissedPerformance extends PerformanceView { - const AlwaysDismissedPerformance(); - - void updateVariable(Animatable variable) { - variable.setProgress(0.0, AnimationDirection.forward); - } - - // 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; - AnimationDirection get curveDirection => AnimationDirection.forward; - double get progress => 0.0; -} -const AlwaysDismissedPerformance alwaysDismissedPerformance = const AlwaysDismissedPerformance(); - -class ReversePerformance extends PerformanceView - with LazyListenerMixin, LocalPerformanceStatusListenersMixin { - ReversePerformance(this.masterPerformance); - - final PerformanceView masterPerformance; - - void updateVariable(Animatable variable) { - variable.setProgress(progress, curveDirection); - } - - void addListener(VoidCallback listener) { - didRegisterListener(); - masterPerformance.addListener(listener); - } - void removeListener(VoidCallback listener) { - masterPerformance.removeListener(listener); - didUnregisterListener(); - } - - void didStartListening() { - masterPerformance.addStatusListener(_statusChangeHandler); - } - - void didStopListening() { - masterPerformance.removeStatusListener(_statusChangeHandler); - } - - void _statusChangeHandler(PerformanceStatus status) { - notifyStatusListeners(_reverseStatus(status)); - } - - PerformanceStatus get status => _reverseStatus(masterPerformance.status); - AnimationDirection get direction => _reverseDirection(masterPerformance.direction); - AnimationDirection get curveDirection => _reverseDirection(masterPerformance.curveDirection); - double get progress => 1.0 - masterPerformance.progress; - - 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; - } - } -} - -class MeanPerformance extends PerformanceView - with LazyListenerMixin, LocalPerformanceListenersMixin, LocalPerformanceStatusListenersMixin { - MeanPerformance(this._performances) { - assert(_performances != null); - } - - // This list is intended to be immutable. Behavior is undefined if you mutate it. - final List _performances; - - void didStartListening() { - for (PerformanceView performance in _performances) { - performance.addListener(notifyListeners); - performance.addStatusListener(notifyStatusListeners); - } - } - - void didStopListening() { - for (PerformanceView performance in _performances) { - performance.removeListener(notifyListeners); - performance.removeStatusListener(notifyStatusListeners); - } - } - - void updateVariable(Animatable variable) { - variable.setProgress(progress, curveDirection); - } - - PerformanceStatus get status { - bool dismissed = true; - bool completed = true; - int direction = 0; - for (PerformanceView performance in _performances) { - switch (performance.status) { - case PerformanceStatus.dismissed: - completed = false; - break; - case PerformanceStatus.completed: - dismissed = false; - break; - case PerformanceStatus.forward: - dismissed = false; - completed = false; - direction += 1; - break; - case PerformanceStatus.reverse: - dismissed = false; - completed = false; - direction -= 1; - break; - } - } - if (direction > 1) - return PerformanceStatus.forward; - if (direction < 1) - return PerformanceStatus.reverse; - if (dismissed) - return PerformanceStatus.dismissed; // all performances were dismissed, or we had none - if (completed) - return PerformanceStatus.completed; // all performances were completed - // Performances were conflicted. - // Either we had an equal non-zero number of forwards and reverse - // transitions, or we had both completed and dismissed transitions. - // We default to whatever our first performance was. - assert(_performances.isNotEmpty); - return _performances[0].status; - } - - AnimationDirection get direction { - if (_performances.isEmpty) - return AnimationDirection.forward; - int direction = 0; - for (PerformanceView performance in _performances) { - switch (performance.direction) { - case AnimationDirection.forward: - direction += 1; - break; - case AnimationDirection.reverse: - direction -= 1; - break; - } - } - if (direction > 1) - return AnimationDirection.forward; - if (direction < 1) - return AnimationDirection.reverse; - // We had an equal (non-zero) number of forwards and reverse transitions. - // Default to the first one. - return _performances[0].direction; - } - - AnimationDirection get curveDirection { - if (_performances.isEmpty) - return AnimationDirection.forward; - int curveDirection = 0; - for (PerformanceView performance in _performances) { - switch (performance.curveDirection) { - case AnimationDirection.forward: - curveDirection += 1; - break; - case AnimationDirection.reverse: - curveDirection -= 1; - break; - } - } - if (curveDirection > 1) - return AnimationDirection.forward; - if (curveDirection < 1) - return AnimationDirection.reverse; - // We had an equal (non-zero) number of forwards and reverse transitions. - // Default to the first one. - return _performances[0].curveDirection; - } - - double get progress { - if (_performances.isEmpty) - return 0.0; - double result = 0.0; - for (PerformanceView performance in _performances) - result += performance.progress; - return result / _performances.length; - } -} - -enum _TrainHoppingMode { minimize, maximize } - -/// This performance starts by proxying one performance, but can be given a -/// second performance. When their times cross (either because the second is -/// going in the opposite direction, or because the one overtakes the other), -/// the performance hops over to proxying the second performance, and the second -/// performance becomes the new "first" performance. -/// -/// Since this object must track the two performances 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 TrainHoppingPerformance extends PerformanceView - with EagerListenerMixin, LocalPerformanceListenersMixin, LocalPerformanceStatusListenersMixin { - TrainHoppingPerformance(this._currentTrain, this._nextTrain, { this.onSwitchedTrain }) { - assert(_currentTrain != null); - if (_nextTrain != null) { - if (_currentTrain.progress > _nextTrain.progress) { - _mode = _TrainHoppingMode.maximize; - } else { - _mode = _TrainHoppingMode.minimize; - if (_currentTrain.progress == _nextTrain.progress) { - _currentTrain = _nextTrain; - _nextTrain = null; - } - } - } - _currentTrain.addStatusListener(_statusChangeHandler); - _currentTrain.addListener(_valueChangeHandler); - if (_nextTrain != null) - _nextTrain.addListener(_valueChangeHandler); - assert(_mode != null); - } - - PerformanceView get currentTrain => _currentTrain; - PerformanceView _currentTrain; - PerformanceView _nextTrain; - _TrainHoppingMode _mode; - - VoidCallback onSwitchedTrain; - - void updateVariable(Animatable variable) { - assert(_currentTrain != null); - variable.setProgress(progress, curveDirection); - } - - 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; - AnimationDirection get curveDirection => _currentTrain.curveDirection; - - double _lastProgress; - void _valueChangeHandler() { - assert(_currentTrain != null); - bool hop = false; - if (_nextTrain != null) { - switch (_mode) { - case _TrainHoppingMode.minimize: - hop = _nextTrain.progress <= _currentTrain.progress; - break; - case _TrainHoppingMode.maximize: - hop = _nextTrain.progress >= _currentTrain.progress; - break; - } - if (hop) { - _currentTrain.removeStatusListener(_statusChangeHandler); - _currentTrain.removeListener(_valueChangeHandler); - _currentTrain = _nextTrain; - _nextTrain.addListener(_valueChangeHandler); - _statusChangeHandler(_nextTrain.status); - } - } - double newProgress = progress; - if (newProgress != _lastProgress) { - notifyListeners(); - _lastProgress = newProgress; - } - assert(_lastProgress != null); - if (hop && onSwitchedTrain != null) - onSwitchedTrain(); - } - - double get progress => _currentTrain.progress; - - /// 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; - } - } -} - -class ProxyPerformance extends PerformanceView - with LazyListenerMixin, LocalPerformanceListenersMixin, LocalPerformanceStatusListenersMixin { - ProxyPerformance([PerformanceView performance]) { - _masterPerformance = performance; - if (_masterPerformance == null) { - _status = PerformanceStatus.dismissed; - _direction = AnimationDirection.forward; - _curveDirection = AnimationDirection.forward; - _progress = 0.0; - } - } - - PerformanceStatus _status; - AnimationDirection _direction; - AnimationDirection _curveDirection; - double _progress; - - PerformanceView get masterPerformance => _masterPerformance; - PerformanceView _masterPerformance; - void set masterPerformance(PerformanceView value) { - if (value == _masterPerformance) - return; - if (_masterPerformance != null) { - _status = _masterPerformance.status; - _direction = _masterPerformance.direction; - _curveDirection = _masterPerformance.curveDirection; - _progress = _masterPerformance.progress; - if (isListening) - didStopListening(); - } - _masterPerformance = value; - if (_masterPerformance != null) { - if (isListening) - didStartListening(); - if (_progress != _masterPerformance.progress) - notifyListeners(); - if (_status != _masterPerformance.status) - notifyStatusListeners(_masterPerformance.status); - _status = null; - _direction = null; - _curveDirection = null; - _progress = null; - } - } - - void didStartListening() { - if (_masterPerformance != null) { - _masterPerformance.addListener(notifyListeners); - _masterPerformance.addStatusListener(notifyStatusListeners); - } - } - - void didStopListening() { - if (_masterPerformance != null) { - _masterPerformance.removeListener(notifyListeners); - _masterPerformance.removeStatusListener(notifyStatusListeners); - } - } - - void updateVariable(Animatable variable) { - variable.setProgress(progress, curveDirection); - } - - PerformanceStatus get status => _masterPerformance != null ? _masterPerformance.status : _status; - AnimationDirection get direction => _masterPerformance != null ? _masterPerformance.direction : _direction; - AnimationDirection get curveDirection => _masterPerformance != null ? _masterPerformance.curveDirection : _curveDirection; - double get progress => _masterPerformance != null ? _masterPerformance.progress : _progress; -} - -class CurvedPerformance extends PerformanceView { - CurvedPerformance(this._performance, { this.curve, this.reverseCurve }); - - final PerformanceView _performance; - - /// The curve to use in the forward direction - Curve curve; - - /// The curve to use in the reverse direction - /// - /// If this field is null, use [curve] in both directions. - Curve reverseCurve; - - void addListener(VoidCallback listener) { - _performance.addListener(listener); - } - void removeListener(VoidCallback listener) { - _performance.removeListener(listener); - } - - void addStatusListener(PerformanceStatusListener listener) { - _performance.addStatusListener(listener); - } - void removeStatusListener(PerformanceStatusListener listener) { - _performance.removeStatusListener(listener); - } - - void updateVariable(Animatable variable) { - variable.setProgress(progress, curveDirection); - } - - PerformanceStatus get status => _performance.status; - AnimationDirection get direction => _performance.direction; - AnimationDirection get curveDirection => _performance.curveDirection; - double get progress { - Curve activeCurve; - if (curveDirection == AnimationDirection.forward || reverseCurve == null) - activeCurve = curve; - else - activeCurve = reverseCurve; - if (activeCurve == null) - return _performance.progress; - if (_performance.status == PerformanceStatus.dismissed) { - assert(_performance.progress == 0.0); - assert(activeCurve.transform(0.0).roundToDouble() == 0.0); - return 0.0; - } - if (_performance.status == PerformanceStatus.completed) { - assert(_performance.progress == 1.0); - assert(activeCurve.transform(1.0).roundToDouble() == 1.0); - return 1.0; - } - return activeCurve.transform(_performance.progress); - } -} - -/// A timeline that can be reversed and used to update [Animatable]s. -/// -/// For example, a performance may handle an animation of a menu opening by -/// sliding and fading in (changing Y value and opacity) over .5 seconds. The -/// performance can move forwards (present) or backwards (dismiss). A consumer -/// may also take direct control of the timeline by manipulating [progress], or -/// [fling] the timeline causing a physics-based simulation to take over the -/// progression. -class Performance extends PerformanceView - with EagerListenerMixin, LocalPerformanceListenersMixin, LocalPerformanceStatusListenersMixin { - Performance({ this.duration, double progress, this.debugLabel }) { - _timeline = new SimulationStepper(_tick); - if (progress != null) - _timeline.value = progress.clamp(0.0, 1.0); - } - - /// A label that is used in the [toString] output. Intended to aid with - /// identifying performance instances in debug output. - final String debugLabel; - - /// Returns a [PerformanceView] 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. - PerformanceView get view => this; - - /// The length of time this performance should last. - Duration duration; - - SimulationStepper _timeline; - AnimationDirection get direction => _direction; - AnimationDirection _direction = AnimationDirection.forward; - AnimationDirection get curveDirection => _curveDirection; - AnimationDirection _curveDirection = AnimationDirection.forward; - - /// 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) { - stop(); - _timeline.value = t.clamp(0.0, 1.0); - _checkStatusChanged(); - } - - /// Whether this animation is currently animating in either the forward or reverse direction. - bool get isAnimating => _timeline.isAnimating; - - PerformanceStatus get status { - if (!isAnimating && progress == 1.0) - return PerformanceStatus.completed; - if (!isAnimating && progress == 0.0) - return PerformanceStatus.dismissed; - return _direction == AnimationDirection.forward ? - PerformanceStatus.forward : - PerformanceStatus.reverse; - } - - /// Updates the given variable according to the current progress of this performance. - void updateVariable(Animatable variable) { - variable.setProgress(progress, _curveDirection); - } - - /// Starts running this animation forwards (towards the end). - Future forward() => play(AnimationDirection.forward); - - /// Starts running this animation in reverse (towards the beginning). - Future reverse() => play(AnimationDirection.reverse); - - /// Starts running this animation in the given direction. - Future play([AnimationDirection direction = AnimationDirection.forward]) { - _direction = direction; - return resume(); - } - - /// Resumes this animation in the most recent direction. - Future resume() { - return _animateTo(_direction == AnimationDirection.forward ? 1.0 : 0.0); - } - - /// Stops running this animation. - void stop() { - _timeline.stop(); - } - - /// Releases any resources used by this object. - /// - /// Same as stop(). - void dispose() { - stop(); - } - - /// - /// Flings the timeline with an optional force (defaults to a critically - /// damped spring) and initial velocity. If velocity is positive, the - /// animation will complete, otherwise it will dismiss. - 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)); - } - - /// Starts running this animation in the forward direction, and - /// restarts the animation when it completes. - Future repeat({ double min: 0.0, double max: 1.0, Duration period }) { - period ??= duration; - return _timeline.animateWith(new _RepeatingSimulation(min, max, period)); - } - - PerformanceStatus _lastStatus = PerformanceStatus.dismissed; - void _checkStatusChanged() { - PerformanceStatus currentStatus = status; - if (currentStatus != _lastStatus) - notifyStatusListeners(status); - _lastStatus = currentStatus; - } - - void _updateCurveDirection() { - if (status != _lastStatus) { - if (_lastStatus == PerformanceStatus.dismissed || _lastStatus == PerformanceStatus.completed) - _curveDirection = _direction; - } - } - - Future _animateTo(double target) { - Duration remainingDuration = duration * (target - _timeline.value).abs(); - _timeline.stop(); - if (remainingDuration == Duration.ZERO) - return new Future.value(); - return _timeline.animateTo(target, duration: remainingDuration); - } - - void _tick(double t) { - _updateCurveDirection(); - didTick(t); - } - - void didTick(double t) { - notifyListeners(); - _checkStatusChanged(); - } - - String toStringDetails() { - String paused = _timeline.isAnimating ? '' : '; paused'; - String label = debugLabel == null ? '' : '; for $debugLabel'; - String more = super.toStringDetails(); - return '$more$paused$label'; - } -} - -/// An animation performance with an animated variable with a concrete type. -class ValuePerformance extends Performance { - ValuePerformance({ this.variable, Duration duration, double progress }) : - super(duration: duration, progress: progress); - - AnimatedValue variable; - T get value => variable.value; - - void didTick(double t) { - if (variable != null) - variable.setProgress(progress, _curveDirection); - super.didTick(t); - } -} - -class _RepeatingSimulation extends Simulation { - _RepeatingSimulation(this.min, this.max, Duration period) - : _periodInSeconds = period.inMicroseconds / Duration.MICROSECONDS_PER_SECOND { - assert(_periodInSeconds > 0.0); - } - - final double min; - final double max; - - final double _periodInSeconds; - - double x(double timeInSeconds) { - assert(timeInSeconds >= 0.0); - final double t = (timeInSeconds / _periodInSeconds) % 1.0; - return lerpDouble(min, max, t); - } - - double dx(double timeInSeconds) => 1.0; - - bool isDone(double timeInSeconds) => false; -} diff --git a/packages/flutter/lib/src/animation/tween.dart b/packages/flutter/lib/src/animation/tween.dart index b512ee8463..06273c2d97 100644 --- a/packages/flutter/lib/src/animation/tween.dart +++ b/packages/flutter/lib/src/animation/tween.dart @@ -2,349 +2,50 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -import 'dart:async'; import 'dart:ui' show Color, Size, Rect, VoidCallback, lerpDouble; -import 'package:newton/newton.dart'; - -import 'animated_value.dart'; +import 'animation.dart'; +import 'animations.dart'; import 'curves.dart'; -import 'forces.dart'; -import 'listener_helpers.dart'; -import 'simulation_stepper.dart'; -abstract class Animated { - const Animated(); +abstract class Animatable { + const Animatable(); - /// Calls the listener every time the value of the animation changes. - void addListener(VoidCallback listener); - /// Stop calling the listener every time the value of the animation changes. - void removeListener(VoidCallback listener); - /// Calls listener every time the status of the animation changes. - void addStatusListener(PerformanceStatusListener listener); - /// Stops calling the listener every time the status of the animation changes. - void removeStatusListener(PerformanceStatusListener listener); + T evaluate(Animation animation); - /// The current status of this animation. - PerformanceStatus get status; - - /// 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; - - /// Whether this animation is stopped at the end. - bool get isCompleted => status == PerformanceStatus.completed; - - String toString() { - return '$runtimeType(${toStringDetails()})'; - } - String toStringDetails() { - assert(status != null); - assert(direction != null); - String icon; - switch (status) { - case PerformanceStatus.forward: - icon = '\u25B6'; // > - break; - case PerformanceStatus.reverse: - icon = '\u25C0'; // < - break; - case PerformanceStatus.completed: - switch (direction) { - case AnimationDirection.forward: - icon = '\u23ED'; // >>| - break; - case AnimationDirection.reverse: - icon = '\u29CF'; // <| - break; - } - break; - case PerformanceStatus.dismissed: - switch (direction) { - case AnimationDirection.forward: - icon = '\u29D0'; // |> - break; - case AnimationDirection.reverse: - icon = '\u23EE'; // |<< - break; - } - break; - } - assert(icon != null); - return '$icon'; - } -} - -abstract class ProxyAnimatedMixin { - Animated get parent; - - void addListener(VoidCallback listener) => parent.addListener(listener); - void removeListener(VoidCallback listener) => parent.removeListener(listener); - void addStatusListener(PerformanceStatusListener listener) => parent.addStatusListener(listener); - void removeStatusListener(PerformanceStatusListener listener) => parent.removeStatusListener(listener); - - PerformanceStatus get status => parent.status; - AnimationDirection get direction => parent.direction; -} - -abstract class Evaluatable { - const Evaluatable(); - - T evaluate(Animated animation); - - Animated animate(Animated parent) { + Animation animate(Animation parent) { return new _AnimatedEvaluation(parent, this); } - Evaluatable chain(Evaluatable parent) { + Animatable chain(Animatable parent) { return new _ChainedEvaluation(parent, this); } } -class _AnimatedEvaluation extends Animated with ProxyAnimatedMixin { +class _AnimatedEvaluation extends Animation with ProxyAnimatedMixin { _AnimatedEvaluation(this.parent, this._evaluatable); /// The animation from which this value is derived. - final Animated parent; + final Animation parent; - final Evaluatable _evaluatable; + final Animatable _evaluatable; T get value => _evaluatable.evaluate(parent); } -class AlwaysStoppedAnimation extends Animated { - const AlwaysStoppedAnimation(this.value); - - final double value; - - void addListener(VoidCallback listener) { } - void removeListener(VoidCallback listener) { } - void addStatusListener(PerformanceStatusListener listener) { } - void removeStatusListener(PerformanceStatusListener listener) { } - PerformanceStatus get status => PerformanceStatus.forward; - AnimationDirection get direction => AnimationDirection.forward; -} - -class _ChainedEvaluation extends Evaluatable { +class _ChainedEvaluation extends Animatable { _ChainedEvaluation(this._parent, this._evaluatable); - final Evaluatable _parent; - final Evaluatable _evaluatable; + final Animatable _parent; + final Animatable _evaluatable; - T evaluate(Animated animation) { + T evaluate(Animation animation) { double value = _parent.evaluate(animation); return _evaluatable.evaluate(new AlwaysStoppedAnimation(value)); } } -class AnimationController extends Animated - with EagerListenerMixin, LocalPerformanceListenersMixin, LocalPerformanceStatusListenersMixin { - AnimationController({ this.duration, double value, this.debugLabel }) { - _timeline = new SimulationStepper(_tick); - if (value != null) - _timeline.value = value.clamp(0.0, 1.0); - } - - /// A label that is used in the [toString] output. Intended to aid with - /// identifying animation controller instances in debug output. - final String debugLabel; - - /// Returns a [Animated] for this animation controller, - /// so that a pointer to this object can be passed around without - /// allowing users of that pointer to mutate the AnimationController state. - Animated get view => this; - - /// The length of time this animation should last. - Duration duration; - - SimulationStepper _timeline; - AnimationDirection get direction => _direction; - AnimationDirection _direction = AnimationDirection.forward; - - /// The progress of this animation along the timeline. - /// - /// Note: Setting this value stops the current animation. - 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(); - } - - /// Whether this animation is currently animating in either the forward or reverse direction. - bool get isAnimating => _timeline.isAnimating; - - PerformanceStatus get status { - if (!isAnimating && value == 1.0) - return PerformanceStatus.completed; - if (!isAnimating && value == 0.0) - return PerformanceStatus.dismissed; - return _direction == AnimationDirection.forward ? - PerformanceStatus.forward : - PerformanceStatus.reverse; - } - - /// Starts running this animation forwards (towards the end). - Future forward() => play(AnimationDirection.forward); - - /// Starts running this animation in reverse (towards the beginning). - Future reverse() => play(AnimationDirection.reverse); - - /// Starts running this animation in the given direction. - Future play(AnimationDirection direction) { - _direction = direction; - return resume(); - } - - /// Resumes this animation in the most recent direction. - Future resume() { - return _animateTo(_direction == AnimationDirection.forward ? 1.0 : 0.0); - } - - /// Stops running this animation. - void stop() { - _timeline.stop(); - } - - /// Releases any resources used by this object. - /// - /// Same as stop(). - void dispose() { - stop(); - } - - /// - /// Flings the timeline with an optional force (defaults to a critically - /// damped spring) and initial velocity. If velocity is positive, the - /// animation will complete, otherwise it will dismiss. - Future fling({double velocity: 1.0, Force force}) { - force ??= kDefaultSpringForce; - _direction = velocity < 0.0 ? AnimationDirection.reverse : AnimationDirection.forward; - return _timeline.animateWith(force.release(value, velocity)); - } - - /// Starts running this animation in the forward direction, and - /// restarts the animation when it completes. - Future repeat({ double min: 0.0, double max: 1.0, Duration period }) { - period ??= duration; - return _timeline.animateWith(new _RepeatingSimulation(min, max, period)); - } - - PerformanceStatus _lastStatus = PerformanceStatus.dismissed; - void _checkStatusChanged() { - PerformanceStatus currentStatus = status; - if (currentStatus != _lastStatus) - notifyStatusListeners(status); - _lastStatus = currentStatus; - } - - Future _animateTo(double target) { - Duration remainingDuration = duration * (target - _timeline.value).abs(); - _timeline.stop(); - if (remainingDuration == Duration.ZERO) - return new Future.value(); - return _timeline.animateTo(target, duration: remainingDuration); - } - - void _tick(double t) { - notifyListeners(); - _checkStatusChanged(); - } - - String toStringDetails() { - String paused = _timeline.isAnimating ? '' : '; paused'; - String label = debugLabel == null ? '' : '; for $debugLabel'; - String more = '${super.toStringDetails()} ${value.toStringAsFixed(3)}'; - return '$more$paused$label'; - } -} - -class _RepeatingSimulation extends Simulation { - _RepeatingSimulation(this.min, this.max, Duration period) - : _periodInSeconds = period.inMicroseconds / Duration.MICROSECONDS_PER_SECOND { - assert(_periodInSeconds > 0.0); - } - - final double min; - final double max; - - final double _periodInSeconds; - - double x(double timeInSeconds) { - assert(timeInSeconds >= 0.0); - final double t = (timeInSeconds / _periodInSeconds) % 1.0; - return lerpDouble(min, max, t); - } - - double dx(double timeInSeconds) => 1.0; - - bool isDone(double timeInSeconds) => false; -} - -class CurvedAnimation extends Animated with ProxyAnimatedMixin { - CurvedAnimation({ - this.parent, - this.curve: Curves.linear, - this.reverseCurve - }) { - assert(parent != null); - assert(curve != null); - parent.addStatusListener(_handleStatusChanged); - } - - final Animated parent; - - /// The curve to use in the forward direction. - Curve curve; - - /// The curve to use in the reverse direction. - /// - /// If this field is null, uses [curve] in both directions. - Curve reverseCurve; - - /// The direction used to select the current curve. - /// - /// The curve direction is only reset when we hit the beginning or the end of - /// the timeline to avoid discontinuities in the value of any variables this - /// a animation is used to animate. - AnimationDirection _curveDirection; - - void _handleStatusChanged(PerformanceStatus status) { - switch (status) { - case PerformanceStatus.dismissed: - case PerformanceStatus.completed: - _curveDirection = null; - break; - case PerformanceStatus.forward: - _curveDirection ??= AnimationDirection.forward; - break; - case PerformanceStatus.reverse: - _curveDirection ??= AnimationDirection.reverse; - break; - } - } - - double get value { - final bool useForwardCurve = reverseCurve == null || (_curveDirection ?? parent.direction) == AnimationDirection.forward; - Curve activeCurve = useForwardCurve ? curve : reverseCurve; - - double t = parent.value; - if (activeCurve == null) - return t; - if (t == 0.0 || t == 1.0) { - assert(activeCurve.transform(t).round() == t); - return t; - } - return activeCurve.transform(t); - } -} - -class Tween extends Evaluatable { +class Tween extends Animatable { Tween({ this.begin, this.end }); /// The value this variable has at the beginning of the animation. @@ -356,7 +57,7 @@ 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(Animated animation) { + T evaluate(Animation animation) { if (end == null) return begin; double t = animation.value; @@ -407,12 +108,12 @@ class IntTween extends Tween { int lerp(double t) => (begin + (end - begin) * t).round(); } -class CurveTween extends Evaluatable { +class CurveTween extends Animatable { CurveTween({ this.curve }); Curve curve; - double evaluate(Animated animation) { + double evaluate(Animation animation) { double t = animation.value; if (t == 0.0 || t == 1.0) { assert(curve.transform(t).round() == t); diff --git a/packages/flutter/lib/src/material/bottom_sheet.dart b/packages/flutter/lib/src/material/bottom_sheet.dart index 965d04e275..2d4f0a0738 100644 --- a/packages/flutter/lib/src/material/bottom_sheet.dart +++ b/packages/flutter/lib/src/material/bottom_sheet.dart @@ -167,7 +167,7 @@ class _ModalBottomSheetRoute extends PopupRoute { return BottomSheet.createAnimationController(); } - Widget buildPage(BuildContext context, Animated animation, Animated forwardAnimation) { + Widget buildPage(BuildContext context, Animation animation, Animation 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 02ac1c4f95..77498ca811 100644 --- a/packages/flutter/lib/src/material/dialog.dart +++ b/packages/flutter/lib/src/material/dialog.dart @@ -128,11 +128,11 @@ class _DialogRoute extends PopupRoute { bool get barrierDismissable => true; Color get barrierColor => Colors.black54; - Widget buildPage(BuildContext context, Animated animation, Animated forwardAnimation) { + Widget buildPage(BuildContext context, Animation animation, Animation forwardAnimation) { return child; } - Widget buildTransitions(BuildContext context, Animated animation, Animated forwardAnimation, Widget child) { + Widget buildTransitions(BuildContext context, Animation animation, Animation forwardAnimation, Widget child) { return new FadeTransition( opacity: new CurvedAnimation( parent: animation, diff --git a/packages/flutter/lib/src/material/drawer.dart b/packages/flutter/lib/src/material/drawer.dart index 7ff312ad58..b5d0703264 100644 --- a/packages/flutter/lib/src/material/drawer.dart +++ b/packages/flutter/lib/src/material/drawer.dart @@ -92,18 +92,18 @@ class DrawerControllerState extends State { } } - void _animationStatusChanged(PerformanceStatus status) { + void _animationStatusChanged(AnimationStatus status) { switch (status) { - case PerformanceStatus.forward: + case AnimationStatus.forward: _ensureHistoryEntry(); break; - case PerformanceStatus.reverse: + case AnimationStatus.reverse: _historyEntry?.remove(); _historyEntry = null; break; - case PerformanceStatus.dismissed: + case AnimationStatus.dismissed: break; - case PerformanceStatus.completed: + case AnimationStatus.completed: break; } } @@ -155,7 +155,7 @@ class DrawerControllerState extends State { final GlobalKey _gestureDetectorKey = new GlobalKey(); Widget build(BuildContext context) { - if (_controller.status == PerformanceStatus.dismissed) { + if (_controller.status == AnimationStatus.dismissed) { return new Align( alignment: const FractionalOffset(0.0, 0.5), child: new GestureDetector( diff --git a/packages/flutter/lib/src/material/dropdown.dart b/packages/flutter/lib/src/material/dropdown.dart index 39560cb846..b27aa8a2a5 100644 --- a/packages/flutter/lib/src/material/dropdown.dart +++ b/packages/flutter/lib/src/material/dropdown.dart @@ -193,7 +193,7 @@ class _DropDownRoute extends PopupRoute<_DropDownRouteResult> { ); } - Widget buildPage(BuildContext context, Animated animation, Animated forwardAnimation) { + Widget buildPage(BuildContext context, Animation animation, Animation 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 931a58b86a..5519d735ec 100644 --- a/packages/flutter/lib/src/material/floating_action_button.dart +++ b/packages/flutter/lib/src/material/floating_action_button.dart @@ -40,7 +40,7 @@ class FloatingActionButton extends StatefulComponent { } class _FloatingActionButtonState extends State { - Animated _childSegue; + Animation _childSegue; AnimationController _childSegueController; void initState() { diff --git a/packages/flutter/lib/src/material/material.dart b/packages/flutter/lib/src/material/material.dart index 90cf0094f2..eb7de2c85d 100644 --- a/packages/flutter/lib/src/material/material.dart +++ b/packages/flutter/lib/src/material/material.dart @@ -367,10 +367,10 @@ class _InkSplash extends InkFeature implements InkSplash { final bool clipToReferenceBox; final bool repositionToReferenceBox; - Animated _radius; + Animation _radius; AnimationController _radiusController; - Animated _alpha; + Animation _alpha; AnimationController _alphaController; void confirm() { @@ -385,8 +385,8 @@ class _InkSplash extends InkFeature implements InkSplash { _alphaController.forward(); } - void _handleAlphaStatusChanged(PerformanceStatus status) { - if (status == PerformanceStatus.completed) + void _handleAlphaStatusChanged(AnimationStatus status) { + if (status == AnimationStatus.completed) dispose(); } @@ -456,7 +456,7 @@ class _InkHighlight extends InkFeature implements InkHighlight { bool get active => _active; bool _active = true; - Animated _alpha; + Animation _alpha; AnimationController _alphaController; void activate() { @@ -469,8 +469,8 @@ class _InkHighlight extends InkFeature implements InkHighlight { _alphaController.reverse(); } - void _handleAlphaStatusChanged(PerformanceStatus status) { - if (status == PerformanceStatus.dismissed && !_active) + void _handleAlphaStatusChanged(AnimationStatus status) { + if (status == AnimationStatus.dismissed && !_active) dispose(); } diff --git a/packages/flutter/lib/src/material/page.dart b/packages/flutter/lib/src/material/page.dart index ee73eb90c0..2ad1658f9c 100644 --- a/packages/flutter/lib/src/material/page.dart +++ b/packages/flutter/lib/src/material/page.dart @@ -10,7 +10,7 @@ import 'package:flutter/widgets.dart'; class _MaterialPageTransition extends AnimatedComponent { _MaterialPageTransition({ Key key, - Animated animation, + Animation animation, this.child }) : super( key: key, @@ -57,7 +57,7 @@ class MaterialPageRoute extends PageRoute { Color get barrierColor => null; bool canTransitionFrom(TransitionRoute nextRoute) => false; - Widget buildPage(BuildContext context, Animated animation, Animated forwardAnimation) { + Widget buildPage(BuildContext context, Animation animation, Animation forwardAnimation) { Widget result = builder(context); assert(() { if (result == null) @@ -68,7 +68,7 @@ class MaterialPageRoute extends PageRoute { return result; } - Widget buildTransitions(BuildContext context, Animated animation, Animated forwardAnimation, Widget child) { + Widget buildTransitions(BuildContext context, Animation animation, Animation forwardAnimation, Widget child) { return new _MaterialPageTransition( 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 29fef63899..975388ec80 100644 --- a/packages/flutter/lib/src/material/popup_menu.dart +++ b/packages/flutter/lib/src/material/popup_menu.dart @@ -132,7 +132,7 @@ class _PopupMenuRoute extends PopupRoute { return position; } - Animated createAnimation() { + Animation createAnimation() { return new CurvedAnimation( parent: super.createAnimation(), reverseCurve: new Interval(0.0, _kMenuCloseIntervalEnd) @@ -143,7 +143,7 @@ class _PopupMenuRoute extends PopupRoute { bool get barrierDismissable => true; Color get barrierColor => null; - Widget buildPage(BuildContext context, Animated animation, Animated forwardAnimation) { + Widget buildPage(BuildContext context, Animation animation, Animation forwardAnimation) { return new _PopupMenu(route: this); } } diff --git a/packages/flutter/lib/src/material/progress_indicator.dart b/packages/flutter/lib/src/material/progress_indicator.dart index 026b0c1099..c58a9233ec 100644 --- a/packages/flutter/lib/src/material/progress_indicator.dart +++ b/packages/flutter/lib/src/material/progress_indicator.dart @@ -38,15 +38,15 @@ abstract class ProgressIndicator extends StatefulComponent { } class _ProgressIndicatorState extends State { - Animated _animation; + Animation _animation; AnimationController _controller; void initState() { super.initState(); _controller = new AnimationController( duration: const Duration(milliseconds: 1500) - )..addStatusListener((PerformanceStatus status) { - if (status == PerformanceStatus.completed) + )..addStatusListener((AnimationStatus status) { + if (status == AnimationStatus.completed) _restartAnimation(); })..forward(); _animation = new CurvedAnimation(parent: _controller, curve: Curves.ease); diff --git a/packages/flutter/lib/src/material/scaffold.dart b/packages/flutter/lib/src/material/scaffold.dart index d834591e89..4c861a6bb9 100644 --- a/packages/flutter/lib/src/material/scaffold.dart +++ b/packages/flutter/lib/src/material/scaffold.dart @@ -227,9 +227,9 @@ class ScaffoldState extends State { return controller; } - void _handleSnackBarStatusChange(PerformanceStatus status) { + void _handleSnackBarStatusChange(AnimationStatus status) { switch (status) { - case PerformanceStatus.dismissed: + case AnimationStatus.dismissed: assert(_snackBars.isNotEmpty); setState(() { _snackBars.removeFirst(); @@ -237,21 +237,21 @@ class ScaffoldState extends State { if (_snackBars.isNotEmpty) _snackBarController.forward(); break; - case PerformanceStatus.completed: + case AnimationStatus.completed: setState(() { assert(_snackBarTimer == null); // build will create a new timer if necessary to dismiss the snack bar }); break; - case PerformanceStatus.forward: - case PerformanceStatus.reverse: + case AnimationStatus.forward: + case AnimationStatus.reverse: break; } } void _hideSnackBar() { - assert(_snackBarController.status == PerformanceStatus.forward || - _snackBarController.status == PerformanceStatus.completed); + assert(_snackBarController.status == AnimationStatus.forward || + _snackBarController.status == AnimationStatus.completed); _snackBars.first._completer.complete(); _snackBarController.reverse(); _snackBarTimer = null; @@ -449,7 +449,7 @@ class _PersistentBottomSheetState extends State<_PersistentBottomSheet> { void initState() { super.initState(); - assert(config.animationController.status == PerformanceStatus.forward); + assert(config.animationController.status == AnimationStatus.forward); config.animationController.addStatusListener(_handleStatusChange); } @@ -467,8 +467,8 @@ class _PersistentBottomSheetState extends State<_PersistentBottomSheet> { config.animationController.reverse(); } - void _handleStatusChange(PerformanceStatus status) { - if (status == PerformanceStatus.dismissed && config.onDismissed != null) + void _handleStatusChange(AnimationStatus status) { + if (status == AnimationStatus.dismissed && config.onDismissed != null) config.onDismissed(); } diff --git a/packages/flutter/lib/src/material/slider.dart b/packages/flutter/lib/src/material/slider.dart index a4df078cf1..2d4cc6cff7 100644 --- a/packages/flutter/lib/src/material/slider.dart +++ b/packages/flutter/lib/src/material/slider.dart @@ -123,7 +123,7 @@ class _RenderSlider extends RenderConstrainedBox { double get _trackLength => size.width - 2.0 * _kReactionRadius; - Animated _reaction; + Animation _reaction; AnimationController _reactionController; HorizontalDragGestureRecognizer _drag; @@ -186,7 +186,7 @@ class _RenderSlider extends RenderConstrainedBox { canvas.drawRect(new Rect.fromLTRB(trackLeft, trackTop, trackActive, trackBottom), primaryPaint); Point activeLocation = new Point(trackActive, trackCenter); - if (_reaction.status != PerformanceStatus.dismissed) { + if (_reaction.status != AnimationStatus.dismissed) { Paint reactionPaint = new Paint()..color = _activeColor.withAlpha(kRadialReactionAlpha); canvas.drawCircle(activeLocation, _reaction.value, reactionPaint); } diff --git a/packages/flutter/lib/src/material/snack_bar.dart b/packages/flutter/lib/src/material/snack_bar.dart index 35ca86e1ca..5b4db850f1 100644 --- a/packages/flutter/lib/src/material/snack_bar.dart +++ b/packages/flutter/lib/src/material/snack_bar.dart @@ -65,7 +65,7 @@ class SnackBar extends StatelessComponent { final Widget content; final List actions; final Duration duration; - final Animated animation; + final Animation animation; Widget build(BuildContext context) { assert(animation != null); @@ -130,7 +130,7 @@ class SnackBar extends StatelessComponent { ); } - SnackBar withAnimation(Animated newAnimation, { Key fallbackKey }) { + SnackBar withAnimation(Animation newAnimation, { Key fallbackKey }) { return new SnackBar( key: key ?? fallbackKey, content: content, diff --git a/packages/flutter/lib/src/material/tabs.dart b/packages/flutter/lib/src/material/tabs.dart index 7b887f0e84..8edaf10593 100644 --- a/packages/flutter/lib/src/material/tabs.dart +++ b/packages/flutter/lib/src/material/tabs.dart @@ -382,7 +382,7 @@ class _TabsScrollBehavior extends BoundedBehavior { } abstract class TabBarSelectionPerformanceListener { - void handleStatusChange(PerformanceStatus status); + void handleStatusChange(AnimationStatus status); void handleProgressChange(); void handleSelectionDeactivate(); } @@ -415,7 +415,7 @@ class TabBarSelection extends StatefulComponent { class TabBarSelectionState extends State> { - Animated get animation => _controller.view; + Animation get animation => _controller.view; // Both the TabBar and TabBarView classes access _controller because they // alternately drive selection progress between tabs. final AnimationController _controller = new AnimationController(duration: _kTabBarScroll, value: 1.0); @@ -481,7 +481,7 @@ class TabBarSelectionState extends State> { // the previous and current selection index. double value; - if (_controller.status == PerformanceStatus.completed) + if (_controller.status == AnimationStatus.completed) value = 0.0; else if (_previousValue == values.first) value = _controller.value; @@ -584,11 +584,11 @@ class _TabBarState extends ScrollableState> implements TabBarSelect _selection = null; } - void handleStatusChange(PerformanceStatus status) { + void handleStatusChange(AnimationStatus status) { if (config.labels.length == 0) return; - if (_valueIsChanging && status == PerformanceStatus.completed) { + if (_valueIsChanging && status == AnimationStatus.completed) { _valueIsChanging = false; _indicatorTween ..begin = _tabIndicatorRect(math.max(0, _selection.index - 1)) @@ -868,7 +868,7 @@ class _TabBarViewState extends PageableListState implements TabBarSe _updateScrollBehaviorForSelectedIndex(selectedIndex); } - void handleStatusChange(PerformanceStatus status) { + void handleStatusChange(AnimationStatus status) { } void handleProgressChange() { @@ -876,14 +876,14 @@ class _TabBarViewState extends PageableListState implements TabBarSe return; // The TabBar is driving the TabBarSelection performance. - final Animated animation = _selection.animation; + final Animation animation = _selection.animation; - if (animation.status == PerformanceStatus.completed) { + if (animation.status == AnimationStatus.completed) { _updateItemsAndScrollBehavior(); return; } - if (animation.status != PerformanceStatus.forward) + if (animation.status != AnimationStatus.forward) return; final int selectedIndex = _selection.index; diff --git a/packages/flutter/lib/src/material/time_picker.dart b/packages/flutter/lib/src/material/time_picker.dart index 8e42d74899..195310e802 100644 --- a/packages/flutter/lib/src/material/time_picker.dart +++ b/packages/flutter/lib/src/material/time_picker.dart @@ -350,7 +350,7 @@ class _DialState extends State<_Dial> { } Tween _thetaTween; - Animated _theta; + Animation _theta; AnimationController _thetaController; bool _dragging = false; diff --git a/packages/flutter/lib/src/material/toggleable.dart b/packages/flutter/lib/src/material/toggleable.dart index e0816ae41b..4e66e28232 100644 --- a/packages/flutter/lib/src/material/toggleable.dart +++ b/packages/flutter/lib/src/material/toggleable.dart @@ -99,15 +99,15 @@ abstract class RenderToggleable extends RenderConstrainedBox { AnimationController get reactionController => _reactionController; AnimationController _reactionController; - Animated _reaction; + Animation _reaction; TapGestureRecognizer _tap; - void _handlePositionStateChanged(PerformanceStatus status) { + void _handlePositionStateChanged(AnimationStatus status) { if (isInteractive) { - if (status == PerformanceStatus.completed && !_value) + if (status == AnimationStatus.completed && !_value) onChanged(true); - else if (status == PerformanceStatus.dismissed && _value) + else if (status == AnimationStatus.dismissed && _value) onChanged(false); } } diff --git a/packages/flutter/lib/src/material/tooltip.dart b/packages/flutter/lib/src/material/tooltip.dart index a59a259dcd..45b4bb4899 100644 --- a/packages/flutter/lib/src/material/tooltip.dart +++ b/packages/flutter/lib/src/material/tooltip.dart @@ -78,14 +78,14 @@ class _TooltipState extends State { void initState() { super.initState(); _controller = new AnimationController(duration: config.fadeDuration) - ..addStatusListener((PerformanceStatus status) { + ..addStatusListener((AnimationStatus status) { switch (status) { - case PerformanceStatus.completed: + case AnimationStatus.completed: assert(_entry != null); assert(_timer == null); resetShowTimer(); break; - case PerformanceStatus.dismissed: + case AnimationStatus.dismissed: assert(_entry != null); assert(_timer == null); _entry.remove(); @@ -117,7 +117,7 @@ class _TooltipState extends State { } void resetShowTimer() { - assert(_controller.status == PerformanceStatus.completed); + assert(_controller.status == AnimationStatus.completed); assert(_entry != null); _timer = new Timer(config.showDuration, hideTooltip); } @@ -149,7 +149,7 @@ class _TooltipState extends State { Overlay.of(context).insert(_entry); } _timer?.cancel(); - if (_controller.status != PerformanceStatus.completed) { + if (_controller.status != AnimationStatus.completed) { _timer = null; _controller.forward(); } else { @@ -249,7 +249,7 @@ class _TooltipOverlay extends StatelessComponent { final double borderRadius; final double height; final EdgeDims padding; - final Animated animation; + final Animation animation; final Point target; final double verticalOffset; final EdgeDims screenEdgeMargin; diff --git a/packages/flutter/lib/src/widgets/dismissable.dart b/packages/flutter/lib/src/widgets/dismissable.dart index c889ab72df..19e4a08b94 100644 --- a/packages/flutter/lib/src/widgets/dismissable.dart +++ b/packages/flutter/lib/src/widgets/dismissable.dart @@ -73,8 +73,8 @@ class _DismissableState extends State { void initState() { super.initState(); _dismissController = new AnimationController(duration: _kCardDismissDuration); - _dismissController.addStatusListener((PerformanceStatus status) { - if (status == PerformanceStatus.completed) + _dismissController.addStatusListener((AnimationStatus status) { + if (status == AnimationStatus.completed) _handleDismissCompleted(); }); } @@ -251,9 +251,9 @@ class _DismissableState extends State { Widget build(BuildContext context) { if (_resizeController != null) { // make sure you remove this widget once it's been dismissed! - assert(_resizeController.status == PerformanceStatus.forward); + assert(_resizeController.status == AnimationStatus.forward); - Animated squashAxisExtent = new Tween( + Animation squashAxisExtent = new Tween( begin: _directionIsYAxis ? _size.width : _size.height, end: 0.0 ).animate(new CurvedAnimation( diff --git a/packages/flutter/lib/src/widgets/enter_exit_transition.dart b/packages/flutter/lib/src/widgets/enter_exit_transition.dart index 1ba2438bdf..c12aef5509 100644 --- a/packages/flutter/lib/src/widgets/enter_exit_transition.dart +++ b/packages/flutter/lib/src/widgets/enter_exit_transition.dart @@ -32,7 +32,7 @@ class SmoothlyResizingOverflowBox extends StatefulComponent { class _SmoothlyResizingOverflowBoxState extends State { SizeTween _sizeTween; CurveTween _curveTween; - Animated _size; + Animation _size; AnimationController _sizeController; void initState() { @@ -100,9 +100,9 @@ class _Entry { } } -typedef Widget TransitionBuilderCallback(Animated animation, Widget child); +typedef Widget TransitionBuilderCallback(Animation animation, Widget child); -Widget _identityTransition(Animated animation, Widget child) => child; +Widget _identityTransition(Animation animation, Widget child) => child; class EnterExitTransition extends StatefulComponent { EnterExitTransition({ diff --git a/packages/flutter/lib/src/widgets/heroes.dart b/packages/flutter/lib/src/widgets/heroes.dart index dc84523874..aff4cc23a4 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, Animated currentAnimation); + _HeroManifest _takeChild(Rect animationArea, Animation currentAnimation); } class Hero extends StatefulComponent { @@ -167,7 +167,7 @@ class HeroState extends State implements HeroHandle { bool get alwaysAnimate => config.alwaysAnimate; - _HeroManifest _takeChild(Rect animationArea, Animated currentAnimation) { + _HeroManifest _takeChild(Rect animationArea, Animation currentAnimation) { assert(_mode == _HeroMode.measured || _mode == _HeroMode.taken); final RenderBox renderObject = context.findRenderObject(); final Point heroTopLeft = renderObject.localToGlobal(Point.origin); @@ -263,7 +263,7 @@ class _HeroQuestState implements HeroHandle { bool get taken => _taken; bool _taken = false; - _HeroManifest _takeChild(Rect animationArea, Animated currentAnimation) { + _HeroManifest _takeChild(Rect animationArea, Animation currentAnimation) { assert(!taken); _taken = true; Set states = sourceStates; @@ -278,7 +278,7 @@ class _HeroQuestState implements HeroHandle { ); } - Widget build(BuildContext context, Animated animation) { + Widget build(BuildContext context, Animation animation) { return new PositionedTransition( rect: currentRect.animate(animation), child: new RotationTransition( @@ -376,14 +376,14 @@ class HeroParty { _heroes = _newHeroes; } - Animated _currentAnimation; + Animation _currentAnimation; void _clearCurrentAnimation() { _currentAnimation?.removeStatusListener(_handleUpdate); _currentAnimation = null; } - void setAnimation(Animated animation) { + void setAnimation(Animation animation) { assert(animation != null || _heroes.length == 0); if (animation != _currentAnimation) { _clearCurrentAnimation(); @@ -392,9 +392,9 @@ class HeroParty { } } - void _handleUpdate(PerformanceStatus status) { - if (status == PerformanceStatus.completed || - status == PerformanceStatus.dismissed) { + void _handleUpdate(AnimationStatus status) { + if (status == AnimationStatus.completed || + status == AnimationStatus.dismissed) { for (_HeroQuestState hero in _heroes) { if (hero.targetState != null) hero.targetState._setChild(hero.key); @@ -417,7 +417,7 @@ class HeroController extends NavigatorObserver { } HeroParty _party; - Animated _animation; + Animation _animation; PageRoute _from; PageRoute _to; @@ -452,7 +452,7 @@ class HeroController extends NavigatorObserver { void _checkForHeroQuest() { if (_from != null && _to != null && _from != _to) { - _to.offstage = _to.animation.status != PerformanceStatus.completed; + _to.offstage = _to.animation.status != AnimationStatus.completed; Scheduler.instance.addPostFrameCallback(_updateQuest); } } @@ -505,9 +505,9 @@ class HeroController extends NavigatorObserver { Map heroesTo = Hero.of(_to.subtreeContext, mostValuableKeys); _to.offstage = false; - Animated animation = _animation; + Animation animation = _animation; Curve curve = Curves.ease; - if (animation.status == PerformanceStatus.reverse) { + if (animation.status == AnimationStatus.reverse) { animation = new ReverseAnimation(animation); curve = new Interval(animation.value, 1.0, curve: curve); } diff --git a/packages/flutter/lib/src/widgets/implicit_animations.dart b/packages/flutter/lib/src/widgets/implicit_animations.dart index b189c385ed..9030ea7bd1 100644 --- a/packages/flutter/lib/src/widgets/implicit_animations.dart +++ b/packages/flutter/lib/src/widgets/implicit_animations.dart @@ -84,8 +84,8 @@ typedef Tween TweenVisitor(Tween tween, T targetValue, TweenConstructor abstract class AnimatedWidgetBaseState extends State { AnimationController _controller; - Animated get animation => _animation; - Animated _animation; + Animation get animation => _animation; + Animation _animation; void initState() { super.initState(); diff --git a/packages/flutter/lib/src/widgets/modal_barrier.dart b/packages/flutter/lib/src/widgets/modal_barrier.dart index aa39a3d3dc..3addd6daa7 100644 --- a/packages/flutter/lib/src/widgets/modal_barrier.dart +++ b/packages/flutter/lib/src/widgets/modal_barrier.dart @@ -46,12 +46,12 @@ class ModalBarrier extends StatelessComponent { class AnimatedModalBarrier extends AnimatedComponent { AnimatedModalBarrier({ Key key, - Animated color, + Animation color, this.dismissable: true }) : color = color, super(key: key, animation: color); /// If non-null, fill the barrier with this color. - final Animated color; + final Animation color; /// Whether touching the barrier will pop the current route off the [Navigator]. final bool dismissable; diff --git a/packages/flutter/lib/src/widgets/routes.dart b/packages/flutter/lib/src/widgets/routes.dart index c61ca452b6..d0dd45870a 100644 --- a/packages/flutter/lib/src/widgets/routes.dart +++ b/packages/flutter/lib/src/widgets/routes.dart @@ -92,8 +92,8 @@ abstract class TransitionRoute extends OverlayRoute { Duration get transitionDuration; bool get opaque; - Animated get animation => _animation; - Animated _animation; + Animation get animation => _animation; + Animation _animation; AnimationController _controller; /// Called to create the animation controller that will drive the transitions to @@ -108,25 +108,25 @@ abstract class TransitionRoute extends OverlayRoute { /// Called to create the animation that exposes the current progress of /// the transition controlled by the animation controller created by /// [createAnimationController()]. - Animated createAnimation() { + Animation createAnimation() { assert(_controller != null); return _controller.view; } T _result; - void handleStatusChanged(PerformanceStatus status) { + void handleStatusChanged(AnimationStatus status) { switch (status) { - case PerformanceStatus.completed: + case AnimationStatus.completed: if (overlayEntries.isNotEmpty) overlayEntries.first.opaque = opaque; break; - case PerformanceStatus.forward: - case PerformanceStatus.reverse: + case AnimationStatus.forward: + case AnimationStatus.reverse: if (overlayEntries.isNotEmpty) overlayEntries.first.opaque = false; break; - case PerformanceStatus.dismissed: + case AnimationStatus.dismissed: assert(!overlayEntries.first.opaque); finished(); // clear the overlays assert(overlayEntries.isEmpty); @@ -134,7 +134,7 @@ abstract class TransitionRoute extends OverlayRoute { } } - Animated get forwardAnimation => _forwardAnimation; + Animation get forwardAnimation => _forwardAnimation; final ProxyAnimation _forwardAnimation = new ProxyAnimation(kAlwaysDismissedAnimation); void install(OverlayEntry insertionPoint) { @@ -177,7 +177,7 @@ abstract class TransitionRoute extends OverlayRoute { void _updateForwardAnimation(Route nextRoute) { if (nextRoute is TransitionRoute && canTransitionTo(nextRoute) && nextRoute.canTransitionFrom(this)) { - Animated current = _forwardAnimation.masterAnimation; + Animation current = _forwardAnimation.masterAnimation; if (current != null) { if (current is TrainHoppingAnimation) { TrainHoppingAnimation newAnimation; @@ -320,7 +320,7 @@ class _ModalScopeState extends State<_ModalScope> { super.dispose(); } - void _animationStatusChanged(PerformanceStatus status) { + void _animationStatusChanged(AnimationStatus status) { setState(() { // The animation's states are our build state, and they changed already. }); @@ -342,7 +342,7 @@ class _ModalScopeState extends State<_ModalScope> { contents = new Focus( key: new GlobalObjectKey(config.route), child: new IgnorePointer( - ignoring: config.route.animation?.status == PerformanceStatus.reverse, + ignoring: config.route.animation?.status == AnimationStatus.reverse, child: config.route.buildTransitions( context, config.route.animation, @@ -396,8 +396,8 @@ abstract class ModalRoute extends TransitionRoute with LocalHistoryRoute null; - Widget buildPage(BuildContext context, Animated animation, Animated forwardAnimation); - Widget buildTransitions(BuildContext context, Animated animation, Animated forwardAnimation, Widget child) { + Widget buildPage(BuildContext context, Animation animation, Animation forwardAnimation); + Widget buildTransitions(BuildContext context, Animation animation, Animation forwardAnimation, Widget child) { return child; } @@ -442,7 +442,7 @@ abstract class ModalRoute extends TransitionRoute with LocalHistoryRoute color = new ColorTween( + Animation color = new ColorTween( begin: _kTransparent, end: barrierColor ).animate(new CurvedAnimation( @@ -456,9 +456,9 @@ abstract class ModalRoute extends TransitionRoute with LocalHistoryRoute animation; + final Animation animation; Widget build(BuildContext context); @@ -39,7 +39,7 @@ class _StatusTransitionState extends State { super.dispose(); } - void _animationStatusChanged(PerformanceStatus status) { + void _animationStatusChanged(AnimationStatus status) { setState(() { // The animation's state is our build state, and it changed already. }); diff --git a/packages/flutter/lib/src/widgets/transitions.dart b/packages/flutter/lib/src/widgets/transitions.dart index fc174ab8d3..e17ee25b6b 100644 --- a/packages/flutter/lib/src/widgets/transitions.dart +++ b/packages/flutter/lib/src/widgets/transitions.dart @@ -14,55 +14,6 @@ import 'framework.dart'; export 'package:flutter/animation.dart' show AnimationDirection; export 'package:flutter/rendering.dart' show RelativeRect; -abstract class TransitionComponent extends StatefulComponent { - TransitionComponent({ - Key key, - this.performance - }) : super(key: key) { - assert(performance != null); - } - - final PerformanceView performance; - - Widget build(BuildContext context); - - _TransitionState createState() => new _TransitionState(); - - void debugFillDescription(List description) { - super.debugFillDescription(description); - description.add('performance: $performance'); - } -} - -class _TransitionState extends State { - void initState() { - super.initState(); - config.performance.addListener(_performanceChanged); - } - - void didUpdateConfig(TransitionComponent oldConfig) { - if (config.performance != oldConfig.performance) { - oldConfig.performance.removeListener(_performanceChanged); - config.performance.addListener(_performanceChanged); - } - } - - void dispose() { - config.performance.removeListener(_performanceChanged); - super.dispose(); - } - - void _performanceChanged() { - setState(() { - // The performance's state is our build state, and it changed already. - }); - } - - Widget build(BuildContext context) { - return config.build(context); - } -} - abstract class AnimatedComponent extends StatefulComponent { AnimatedComponent({ Key key, @@ -71,7 +22,7 @@ abstract class AnimatedComponent extends StatefulComponent { assert(animation != null); } - final Animated animation; + final Animation animation; Widget build(BuildContext context); @@ -112,30 +63,15 @@ class _AnimatedComponentState extends State { } } -abstract class TransitionWithChild extends TransitionComponent { - TransitionWithChild({ - Key key, - this.child, - PerformanceView performance - }) : super(key: key, performance: performance); - - final Widget child; - - Widget build(BuildContext context) => buildWithChild(context, child); - - Widget buildWithChild(BuildContext context, Widget child); -} - class SlideTransition extends AnimatedComponent { SlideTransition({ Key key, - Animated position, - PerformanceView performance, + Animation position, this.transformHitTests: true, this.child }) : position = position, super(key: key, animation: position); - final Animated position; + final Animation position; final bool transformHitTests; final Widget child; @@ -151,12 +87,12 @@ class SlideTransition extends AnimatedComponent { class ScaleTransition extends AnimatedComponent { ScaleTransition({ Key key, - Animated scale, + Animation scale, this.alignment: const FractionalOffset(0.5, 0.5), this.child }) : scale = scale, super(key: key, animation: scale); - final Animated scale; + final Animation scale; final FractionalOffset alignment; final Widget child; @@ -175,11 +111,11 @@ class ScaleTransition extends AnimatedComponent { class RotationTransition extends AnimatedComponent { RotationTransition({ Key key, - Animated turns, + Animation turns, this.child }) : turns = turns, super(key: key, animation: turns); - final Animated turns; + final Animation turns; final Widget child; Widget build(BuildContext context) { @@ -196,11 +132,11 @@ class RotationTransition extends AnimatedComponent { class FadeTransition extends AnimatedComponent { FadeTransition({ Key key, - Animated opacity, + Animation opacity, this.child }) : opacity = opacity, super(key: key, animation: opacity); - final Animated opacity; + final Animation opacity; final Widget child; Widget build(BuildContext context) { @@ -211,11 +147,11 @@ class FadeTransition extends AnimatedComponent { class ColorTransition extends AnimatedComponent { ColorTransition({ Key key, - Animated color, + Animation color, this.child }) : color = color, super(key: key, animation: color); - final Animated color; + final Animation color; final Widget child; Widget build(BuildContext context) { @@ -226,61 +162,6 @@ class ColorTransition extends AnimatedComponent { } } -class SquashTransition extends TransitionWithChild { - SquashTransition({ - Key key, - this.width, - this.height, - PerformanceView performance, - Widget child - }) : super(key: key, - performance: performance, - child: child); - - final AnimatedValue width; - final AnimatedValue height; - - Widget buildWithChild(BuildContext context, Widget child) { - if (width != null) - performance.updateVariable(width); - if (height != null) - performance.updateVariable(height); - return new SizedBox(width: width?.value, height: height?.value, child: child); - } -} - -class AlignTransition extends TransitionWithChild { - AlignTransition({ - Key key, - this.alignment, - this.widthFactor, - this.heightFactor, - PerformanceView performance, - Widget child - }) : super(key: key, - performance: performance, - child: child); - - final AnimatedValue alignment; - final AnimatedValue widthFactor; - final AnimatedValue heightFactor; - - Widget buildWithChild(BuildContext context, Widget child) { - if (alignment != null) - performance.updateVariable(alignment); - if (widthFactor != null) - performance.updateVariable(widthFactor); - if (heightFactor != null) - performance.updateVariable(heightFactor); - return new Align( - alignment: alignment?.value, - widthFactor: widthFactor?.value, - heightFactor: heightFactor?.value, - child: child - ); - } -} - /// An animated variable containing a RelativeRectangle /// /// This class specializes the interpolation of AnimatedValue to @@ -302,13 +183,13 @@ class RelativeRectTween extends Tween { class PositionedTransition extends AnimatedComponent { PositionedTransition({ Key key, - Animated rect, + Animation rect, this.child }) : rect = rect, super(key: key, animation: rect) { assert(rect != null); } - final Animated rect; + final Animation rect; final Widget child; Widget build(BuildContext context) { @@ -322,31 +203,12 @@ class PositionedTransition extends AnimatedComponent { } } -class BuilderTransition extends TransitionComponent { - BuilderTransition({ - Key key, - this.variables: const [], - this.builder, - PerformanceView performance - }) : super(key: key, - performance: performance); - - final List variables; - final WidgetBuilder builder; - - Widget build(BuildContext context) { - for (int i = 0; i < variables.length; ++i) - performance.updateVariable(variables[i]); - return builder(context); - } -} - typedef Widget TransitionBuilder(BuildContext context, Widget child); class AnimatedBuilder extends AnimatedComponent { AnimatedBuilder({ Key key, - Animated animation, + Animation animation, this.builder, this.child }) : super(key: key, animation: animation); diff --git a/packages/flutter/test/widget/page_forward_transitions_test.dart b/packages/flutter/test/widget/page_forward_transitions_test.dart index 337ba45d86..8471d1b966 100644 --- a/packages/flutter/test/widget/page_forward_transitions_test.dart +++ b/packages/flutter/test/widget/page_forward_transitions_test.dart @@ -12,7 +12,7 @@ class TestTransition extends AnimatedComponent { Key key, this.childFirstHalf, this.childSecondHalf, - Animated animation + Animation animation }) : super(key: key, animation: animation) { assert(animation != null); } @@ -21,7 +21,7 @@ class TestTransition extends AnimatedComponent { final Widget childSecondHalf; Widget build(BuildContext context) { - final Animated animation = this.animation; + final Animation animation = this.animation; if (animation.value >= 0.5) return childSecondHalf; return childFirstHalf; @@ -33,7 +33,7 @@ class TestRoute extends PageRoute { final Widget child; Duration get transitionDuration => kMaterialPageRouteTransitionDuration; Color get barrierColor => null; - Widget buildPage(BuildContext context, Animated animation, Animated forwardAnimation) { + Widget buildPage(BuildContext context, Animation animation, Animation forwardAnimation) { return child; } }