Commit 3bbeee7b authored by Adam Barth's avatar Adam Barth

Remove AnimationDirection

This concept is now private to AnimationController. All the clients actually
want the AnimationStatus.
parent 19b9464e
...@@ -14,39 +14,44 @@ class _ProgressIndicatorDemoState extends State<ProgressIndicatorDemo> { ...@@ -14,39 +14,44 @@ class _ProgressIndicatorDemoState extends State<ProgressIndicatorDemo> {
super.initState(); super.initState();
controller = new AnimationController( controller = new AnimationController(
duration: const Duration(milliseconds: 1500) duration: const Duration(milliseconds: 1500)
)..play(AnimationDirection.forward); )..forward();
animation = new CurvedAnimation( animation = new CurvedAnimation(
parent: controller, parent: controller,
curve: new Interval(0.0, 0.9, curve: Curves.ease), curve: new Interval(0.0, 0.9, curve: Curves.ease),
reverseCurve: Curves.ease reverseCurve: Curves.ease
)..addStatusListener((AnimationStatus status) { )..addStatusListener((AnimationStatus status) {
if (status == AnimationStatus.dismissed || status == AnimationStatus.completed) if (status == AnimationStatus.dismissed)
reverseValueAnimationDirection(); controller.forward();
else if (status == AnimationStatus.completed)
controller.reverse();
}); });
} }
Animation<double> animation; Animation<double> animation;
AnimationController controller; AnimationController controller;
void handleTap() { void _handleTap() {
setState(() { setState(() {
// valueAnimation.isAnimating is part of our build state // valueAnimation.isAnimating is part of our build state
if (controller.isAnimating) if (controller.isAnimating) {
controller.stop(); controller.stop();
else } else {
controller.resume(); switch (controller.status) {
case AnimationStatus.dismissed:
case AnimationStatus.forward:
controller.forward();
break;
case AnimationStatus.reverse:
case AnimationStatus.completed:
controller.reverse();
break;
}
}
}); });
} }
void reverseValueAnimationDirection() { Widget _buildIndicators(BuildContext context, Widget child) {
AnimationDirection direction = (controller.direction == AnimationDirection.forward)
? AnimationDirection.reverse
: AnimationDirection.forward;
controller.play(direction);
}
Widget buildIndicators(BuildContext context, Widget child) {
List<Widget> indicators = <Widget>[ List<Widget> indicators = <Widget>[
new SizedBox( new SizedBox(
width: 200.0, width: 200.0,
...@@ -82,13 +87,13 @@ class _ProgressIndicatorDemoState extends State<ProgressIndicatorDemo> { ...@@ -82,13 +87,13 @@ class _ProgressIndicatorDemoState extends State<ProgressIndicatorDemo> {
body: new DefaultTextStyle( body: new DefaultTextStyle(
style: Theme.of(context).text.title, style: Theme.of(context).text.title,
child: new GestureDetector( child: new GestureDetector(
onTap: handleTap, onTap: _handleTap,
behavior: HitTestBehavior.opaque, behavior: HitTestBehavior.opaque,
child: new Container( child: new Container(
padding: const EdgeDims.symmetric(vertical: 12.0, horizontal: 8.0), padding: const EdgeDims.symmetric(vertical: 12.0, horizontal: 8.0),
child: new AnimatedBuilder( child: new AnimatedBuilder(
animation: animation, animation: animation,
builder: buildIndicators builder: _buildIndicators
) )
) )
) )
......
...@@ -4,15 +4,6 @@ ...@@ -4,15 +4,6 @@
import 'dart:ui' show Color, Size, Rect, VoidCallback; import 'dart:ui' show Color, Size, Rect, VoidCallback;
/// 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 /// The status of an animation
enum AnimationStatus { enum AnimationStatus {
/// The animation is stopped at the beginning /// The animation is stopped at the beginning
...@@ -62,9 +53,6 @@ abstract class Animation<T> { ...@@ -62,9 +53,6 @@ abstract class Animation<T> {
/// The current status of this animation. /// The current status of this animation.
AnimationStatus get status; AnimationStatus get status;
/// The current direction of the animation.
AnimationDirection get direction;
/// The current value of the animation. /// The current value of the animation.
T get value; T get value;
...@@ -79,7 +67,6 @@ abstract class Animation<T> { ...@@ -79,7 +67,6 @@ abstract class Animation<T> {
} }
String toStringDetails() { String toStringDetails() {
assert(status != null); assert(status != null);
assert(direction != null);
String icon; String icon;
switch (status) { switch (status) {
case AnimationStatus.forward: case AnimationStatus.forward:
...@@ -89,24 +76,10 @@ abstract class Animation<T> { ...@@ -89,24 +76,10 @@ abstract class Animation<T> {
icon = '\u25C0'; // < icon = '\u25C0'; // <
break; break;
case AnimationStatus.completed: case AnimationStatus.completed:
switch (direction) { icon = '\u23ED'; // >>|
case AnimationDirection.forward:
icon = '\u23ED'; // >>|
break;
case AnimationDirection.reverse:
icon = '\u29CF'; // <|
break;
}
break; break;
case AnimationStatus.dismissed: case AnimationStatus.dismissed:
switch (direction) { icon = '\u23EE'; // |<<
case AnimationDirection.forward:
icon = '\u29D0'; // |>
break;
case AnimationDirection.reverse:
icon = '\u23EE'; // |<<
break;
}
break; break;
} }
assert(icon != null); assert(icon != null);
......
...@@ -13,6 +13,15 @@ import 'curves.dart'; ...@@ -13,6 +13,15 @@ import 'curves.dart';
import 'forces.dart'; import 'forces.dart';
import 'listener_helpers.dart'; import 'listener_helpers.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
}
/// A controller for an animation. /// A controller for an animation.
/// ///
/// An animation controller can drive an animation forward or backward and can /// An animation controller can drive an animation forward or backward and can
...@@ -79,9 +88,6 @@ class AnimationController extends Animation<double> ...@@ -79,9 +88,6 @@ class AnimationController extends Animation<double>
/// The length of time this animation should last. /// The length of time this animation should last.
Duration duration; Duration duration;
AnimationDirection get direction => _direction;
AnimationDirection _direction = AnimationDirection.forward;
Ticker _ticker; Ticker _ticker;
Simulation _simulation; Simulation _simulation;
...@@ -106,31 +112,28 @@ class AnimationController extends Animation<double> ...@@ -106,31 +112,28 @@ class AnimationController extends Animation<double>
/// Whether this animation is currently animating in either the forward or reverse direction. /// Whether this animation is currently animating in either the forward or reverse direction.
bool get isAnimating => _ticker.isTicking; bool get isAnimating => _ticker.isTicking;
_AnimationDirection _direction;
AnimationStatus get status { AnimationStatus get status {
if (!isAnimating && value == upperBound) if (!isAnimating && value == upperBound)
return AnimationStatus.completed; return AnimationStatus.completed;
if (!isAnimating && value == lowerBound) if (!isAnimating && value == lowerBound)
return AnimationStatus.dismissed; return AnimationStatus.dismissed;
return _direction == AnimationDirection.forward ? return _direction == _AnimationDirection.forward ?
AnimationStatus.forward : AnimationStatus.forward :
AnimationStatus.reverse; AnimationStatus.reverse;
} }
/// Starts running this animation forwards (towards the end). /// Starts running this animation forwards (towards the end).
Future forward() => play(AnimationDirection.forward); Future forward() {
_direction = _AnimationDirection.forward;
/// Starts running this animation in reverse (towards the beginning). return animateTo(upperBound);
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. /// Starts running this animation in reverse (towards the beginning).
Future resume() { Future reverse() {
return animateTo(_direction == AnimationDirection.forward ? upperBound : lowerBound); _direction = _AnimationDirection.reverse;
return animateTo(lowerBound);
} }
/// Drives the animation from its current value to target. /// Drives the animation from its current value to target.
...@@ -165,7 +168,7 @@ class AnimationController extends Animation<double> ...@@ -165,7 +168,7 @@ class AnimationController extends Animation<double>
/// animation will complete, otherwise it will dismiss. /// animation will complete, otherwise it will dismiss.
Future fling({ double velocity: 1.0, Force force }) { Future fling({ double velocity: 1.0, Force force }) {
force ??= kDefaultSpringForce; force ??= kDefaultSpringForce;
_direction = velocity < 0.0 ? AnimationDirection.reverse : AnimationDirection.forward; _direction = velocity < 0.0 ? _AnimationDirection.reverse : _AnimationDirection.forward;
return animateWith(force.release(value, velocity)); return animateWith(force.release(value, velocity));
} }
......
...@@ -16,7 +16,6 @@ class _AlwaysCompleteAnimation extends Animation<double> { ...@@ -16,7 +16,6 @@ class _AlwaysCompleteAnimation extends Animation<double> {
void addStatusListener(AnimationStatusListener listener) { } void addStatusListener(AnimationStatusListener listener) { }
void removeStatusListener(AnimationStatusListener listener) { } void removeStatusListener(AnimationStatusListener listener) { }
AnimationStatus get status => AnimationStatus.completed; AnimationStatus get status => AnimationStatus.completed;
AnimationDirection get direction => AnimationDirection.forward;
double get value => 1.0; double get value => 1.0;
} }
...@@ -35,7 +34,6 @@ class _AlwaysDismissedAnimation extends Animation<double> { ...@@ -35,7 +34,6 @@ class _AlwaysDismissedAnimation extends Animation<double> {
void addStatusListener(AnimationStatusListener listener) { } void addStatusListener(AnimationStatusListener listener) { }
void removeStatusListener(AnimationStatusListener listener) { } void removeStatusListener(AnimationStatusListener listener) { }
AnimationStatus get status => AnimationStatus.dismissed; AnimationStatus get status => AnimationStatus.dismissed;
AnimationDirection get direction => AnimationDirection.forward;
double get value => 0.0; double get value => 0.0;
} }
...@@ -57,7 +55,6 @@ class AlwaysStoppedAnimation<T> extends Animation<T> { ...@@ -57,7 +55,6 @@ class AlwaysStoppedAnimation<T> extends Animation<T> {
void addStatusListener(AnimationStatusListener listener) { } void addStatusListener(AnimationStatusListener listener) { }
void removeStatusListener(AnimationStatusListener listener) { } void removeStatusListener(AnimationStatusListener listener) { }
AnimationStatus get status => AnimationStatus.forward; AnimationStatus get status => AnimationStatus.forward;
AnimationDirection get direction => AnimationDirection.forward;
} }
/// Implements most of the [Animation] interface, by deferring its behavior to a /// Implements most of the [Animation] interface, by deferring its behavior to a
...@@ -77,7 +74,6 @@ abstract class AnimationWithParentMixin<T> { ...@@ -77,7 +74,6 @@ abstract class AnimationWithParentMixin<T> {
void removeStatusListener(AnimationStatusListener listener) => parent.removeStatusListener(listener); void removeStatusListener(AnimationStatusListener listener) => parent.removeStatusListener(listener);
AnimationStatus get status => parent.status; AnimationStatus get status => parent.status;
AnimationDirection get direction => parent.direction;
} }
/// An animation that is a proxy for another animation. /// An animation that is a proxy for another animation.
...@@ -92,13 +88,11 @@ class ProxyAnimation extends Animation<double> ...@@ -92,13 +88,11 @@ class ProxyAnimation extends Animation<double>
_parent = animation; _parent = animation;
if (_parent == null) { if (_parent == null) {
_status = AnimationStatus.dismissed; _status = AnimationStatus.dismissed;
_direction = AnimationDirection.forward;
_value = 0.0; _value = 0.0;
} }
} }
AnimationStatus _status; AnimationStatus _status;
AnimationDirection _direction;
double _value; double _value;
/// The animation whose value this animation will proxy. /// The animation whose value this animation will proxy.
...@@ -112,7 +106,6 @@ class ProxyAnimation extends Animation<double> ...@@ -112,7 +106,6 @@ class ProxyAnimation extends Animation<double>
return; return;
if (_parent != null) { if (_parent != null) {
_status = _parent.status; _status = _parent.status;
_direction = _parent.direction;
_value = _parent.value; _value = _parent.value;
if (isListening) if (isListening)
didStopListening(); didStopListening();
...@@ -126,7 +119,6 @@ class ProxyAnimation extends Animation<double> ...@@ -126,7 +119,6 @@ class ProxyAnimation extends Animation<double>
if (_status != _parent.status) if (_status != _parent.status)
notifyStatusListeners(_parent.status); notifyStatusListeners(_parent.status);
_status = null; _status = null;
_direction = null;
_value = null; _value = null;
} }
} }
...@@ -146,7 +138,6 @@ class ProxyAnimation extends Animation<double> ...@@ -146,7 +138,6 @@ class ProxyAnimation extends Animation<double>
} }
AnimationStatus get status => _parent != null ? _parent.status : _status; AnimationStatus get status => _parent != null ? _parent.status : _status;
AnimationDirection get direction => _parent != null ? _parent.direction : _direction;
double get value => _parent != null ? _parent.value : _value; double get value => _parent != null ? _parent.value : _value;
} }
...@@ -186,7 +177,6 @@ class ReverseAnimation extends Animation<double> ...@@ -186,7 +177,6 @@ class ReverseAnimation extends Animation<double>
} }
AnimationStatus get status => _reverseStatus(parent.status); AnimationStatus get status => _reverseStatus(parent.status);
AnimationDirection get direction => _reverseDirection(parent.direction);
double get value => 1.0 - parent.value; double get value => 1.0 - parent.value;
AnimationStatus _reverseStatus(AnimationStatus status) { AnimationStatus _reverseStatus(AnimationStatus status) {
...@@ -197,13 +187,6 @@ class ReverseAnimation extends Animation<double> ...@@ -197,13 +187,6 @@ class ReverseAnimation extends Animation<double>
case AnimationStatus.dismissed: return AnimationStatus.completed; case AnimationStatus.dismissed: return AnimationStatus.completed;
} }
} }
AnimationDirection _reverseDirection(AnimationDirection direction) {
switch (direction) {
case AnimationDirection.forward: return AnimationDirection.reverse;
case AnimationDirection.reverse: return AnimationDirection.forward;
}
}
} }
/// An animation that applies a curve to another animation. /// An animation that applies a curve to another animation.
...@@ -238,7 +221,7 @@ class CurvedAnimation extends Animation<double> with AnimationWithParentMixin<do ...@@ -238,7 +221,7 @@ class CurvedAnimation extends Animation<double> with AnimationWithParentMixin<do
/// The curve direction is only reset when we hit the beginning or the end of /// 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 /// the timeline to avoid discontinuities in the value of any variables this
/// a animation is used to animate. /// a animation is used to animate.
AnimationDirection _curveDirection; AnimationStatus _curveDirection;
void _handleStatusChanged(AnimationStatus status) { void _handleStatusChanged(AnimationStatus status) {
switch (status) { switch (status) {
...@@ -247,16 +230,16 @@ class CurvedAnimation extends Animation<double> with AnimationWithParentMixin<do ...@@ -247,16 +230,16 @@ class CurvedAnimation extends Animation<double> with AnimationWithParentMixin<do
_curveDirection = null; _curveDirection = null;
break; break;
case AnimationStatus.forward: case AnimationStatus.forward:
_curveDirection ??= AnimationDirection.forward; _curveDirection ??= AnimationStatus.forward;
break; break;
case AnimationStatus.reverse: case AnimationStatus.reverse:
_curveDirection ??= AnimationDirection.reverse; _curveDirection ??= AnimationStatus.reverse;
break; break;
} }
} }
double get value { double get value {
final bool useForwardCurve = reverseCurve == null || (_curveDirection ?? parent.direction) == AnimationDirection.forward; final bool useForwardCurve = reverseCurve == null || (_curveDirection ?? parent.status) != AnimationStatus.reverse;
Curve activeCurve = useForwardCurve ? curve : reverseCurve; Curve activeCurve = useForwardCurve ? curve : reverseCurve;
double t = parent.value; double t = parent.value;
...@@ -324,7 +307,6 @@ class TrainHoppingAnimation extends Animation<double> ...@@ -324,7 +307,6 @@ class TrainHoppingAnimation extends Animation<double>
} }
AnimationStatus get status => _currentTrain.status; AnimationStatus get status => _currentTrain.status;
AnimationDirection get direction => _currentTrain.direction;
double _lastValue; double _lastValue;
void _valueChangeHandler() { void _valueChangeHandler() {
......
...@@ -51,7 +51,7 @@ class _BottomSheetState extends State<BottomSheet> { ...@@ -51,7 +51,7 @@ class _BottomSheetState extends State<BottomSheet> {
return renderBox.size.height; return renderBox.size.height;
} }
bool get _dismissUnderway => config.animationController.direction == AnimationDirection.reverse; bool get _dismissUnderway => config.animationController.status == AnimationStatus.reverse;
void _handleDragUpdate(double delta) { void _handleDragUpdate(double delta) {
if (_dismissUnderway) if (_dismissUnderway)
......
...@@ -809,7 +809,6 @@ class _TabBarViewState extends PageableListState<TabBarView> implements TabBarSe ...@@ -809,7 +809,6 @@ class _TabBarViewState extends PageableListState<TabBarView> implements TabBarSe
TabBarSelectionState _selection; TabBarSelectionState _selection;
List<Widget> _items; List<Widget> _items;
AnimationDirection _scrollDirection = AnimationDirection.forward;
int get _tabCount => config.children.length; int get _tabCount => config.children.length;
...@@ -906,16 +905,11 @@ class _TabBarViewState extends PageableListState<TabBarView> implements TabBarSe ...@@ -906,16 +905,11 @@ class _TabBarViewState extends PageableListState<TabBarView> implements TabBarSe
if (selectedIndex < previousSelectedIndex) { if (selectedIndex < previousSelectedIndex) {
_updateItemsFromChildren(selectedIndex, previousSelectedIndex); _updateItemsFromChildren(selectedIndex, previousSelectedIndex);
_scrollDirection = AnimationDirection.reverse; scrollTo(1.0 - animation.value);
} else { } else {
_updateItemsFromChildren(previousSelectedIndex, selectedIndex); _updateItemsFromChildren(previousSelectedIndex, selectedIndex);
_scrollDirection = AnimationDirection.forward;
}
if (_scrollDirection == AnimationDirection.forward)
scrollTo(animation.value); scrollTo(animation.value);
else }
scrollTo(1.0 - animation.value);
} }
void dispatchOnScroll() { void dispatchOnScroll() {
......
...@@ -66,7 +66,10 @@ abstract class RenderToggleable extends RenderConstrainedBox implements Semantic ...@@ -66,7 +66,10 @@ abstract class RenderToggleable extends RenderConstrainedBox implements Semantic
_position _position
..curve = Curves.easeIn ..curve = Curves.easeIn
..reverseCurve = Curves.easeOut; ..reverseCurve = Curves.easeOut;
_positionController.play(value ? AnimationDirection.forward : AnimationDirection.reverse); if (value)
_positionController.forward();
else
_positionController.reverse();
} }
Color get activeColor => _activeColor; Color get activeColor => _activeColor;
......
...@@ -479,7 +479,8 @@ class HeroController extends NavigatorObserver { ...@@ -479,7 +479,8 @@ class HeroController extends NavigatorObserver {
void _addHeroToOverlay(Widget hero, Object tag, OverlayState overlay) { void _addHeroToOverlay(Widget hero, Object tag, OverlayState overlay) {
OverlayEntry entry = new OverlayEntry(builder: (_) => hero); OverlayEntry entry = new OverlayEntry(builder: (_) => hero);
if (_animation.direction == AnimationDirection.forward) assert(_animation.status != AnimationStatus.dismissed && _animation.status != AnimationStatus.completed);
if (_animation.status == AnimationStatus.forward)
_to.insertHeroOverlayEntry(entry, tag, overlay); _to.insertHeroOverlayEntry(entry, tag, overlay);
else else
_from.insertHeroOverlayEntry(entry, tag, overlay); _from.insertHeroOverlayEntry(entry, tag, overlay);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment