Port the remainder of the framework to AnimationController

There should be no more uses of Performance or AnimatedValue in the framework
or the examples.
This commit is contained in:
Adam Barth
2016-01-20 12:53:48 -08:00
parent aabd1ce457
commit e459e7124a
7 changed files with 78 additions and 63 deletions

View File

@@ -18,7 +18,7 @@ class PageSelectorDemo extends StatelessComponent {
builder: (BuildContext context, Widget child) {
Color background = selection.value == iconName ? _selectedColor.end : _selectedColor.begin;
if (selection.valueIsChanging) {
// Then the selection's performance is animating from previousValue to value.
// Then the selection's animation is animating from previousValue to value.
if (selection.value == iconName)
background = _selectedColor.evaluate(animation);
else if (selection.previousValue == iconName)

View File

@@ -11,7 +11,6 @@ import 'tween.dart';
class AlwaysCompleteAnimation extends Animated<double> {
const AlwaysCompleteAnimation();
// this performance never changes state
void addListener(VoidCallback listener) { }
void removeListener(VoidCallback listener) { }
void addStatusListener(PerformanceStatusListener listener) { }
@@ -26,7 +25,6 @@ const AlwaysCompleteAnimation kAlwaysCompleteAnimation = const AlwaysCompleteAni
class AlwaysDismissedAnimation extends Animated<double> {
const AlwaysDismissedAnimation();
// this performance never changes state
void addListener(VoidCallback listener) { }
void removeListener(VoidCallback listener) { }
void addStatusListener(PerformanceStatusListener listener) { }

View File

@@ -5,31 +5,32 @@
import 'dart:async';
import 'package:newton/newton.dart';
import 'animated_value.dart';
import 'curves.dart';
import 'ticker.dart';
/// A simulation that varies from [begin] to [end] over [duration] using [curve].
///
/// This class is an adaptor between the Simulation interface and the
/// AnimatedValue interface.
/// A simulation that varies from begin to end over duration using curve.
class _TweenSimulation extends Simulation {
_TweenSimulation(double begin, double end, Duration duration, Curve curve)
: _durationInSeconds = duration.inMicroseconds / Duration.MICROSECONDS_PER_SECOND,
_tween = new AnimatedValue<double>(begin, end: end, curve: curve) {
_TweenSimulation(this._begin, this._end, Duration duration, this._curve)
: _durationInSeconds = duration.inMicroseconds / Duration.MICROSECONDS_PER_SECOND {
assert(_durationInSeconds > 0.0);
assert(begin != null);
assert(end != null);
assert(_begin != null);
assert(_end != null);
}
final double _durationInSeconds;
final AnimatedValue<double> _tween;
final double _begin;
final double _end;
final Curve _curve;
double x(double timeInSeconds) {
assert(timeInSeconds >= 0.0);
final double t = (timeInSeconds / _durationInSeconds).clamp(0.0, 1.0);
_tween.setProgress(t, AnimationDirection.forward);
return _tween.value;
double t = (timeInSeconds / _durationInSeconds).clamp(0.0, 1.0);
if (t == 0.0)
return _begin;
else if (t == 1.0)
return _end;
else
return _begin + (_end - _begin) * _curve.transform(t);
}
double dx(double timeInSeconds) => 1.0;

View File

@@ -100,6 +100,10 @@ abstract class Evaluatable<T> {
Animated<T> animate(Animated<double> parent) {
return new _AnimatedEvaluation<T>(parent, this);
}
Evaluatable<T> chain(Evaluatable<double> parent) {
return new _ChainedEvaluation<T>(parent, this);
}
}
class _AnimatedEvaluation<T> extends Animated<T> with ProxyAnimatedMixin {
@@ -113,6 +117,31 @@ class _AnimatedEvaluation<T> extends Animated<T> with ProxyAnimatedMixin {
T get value => _evaluatable.evaluate(parent);
}
class AlwaysStoppedAnimation extends Animated<double> {
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<T> extends Evaluatable<T> {
_ChainedEvaluation(this._parent, this._evaluatable);
final Evaluatable<double> _parent;
final Evaluatable<T> _evaluatable;
T evaluate(Animated<double> animation) {
double value = _parent.evaluate(animation);
return _evaluatable.evaluate(new AlwaysStoppedAnimation(value));
}
}
class AnimationController extends Animated<double>
with EagerListenerMixin, LocalPerformanceListenersMixin, LocalPerformanceStatusListenersMixin {
AnimationController({ this.duration, double value, this.debugLabel }) {

View File

@@ -854,9 +854,8 @@ abstract class RenderBoxContainerDefaultsMixin<ChildType extends RenderBox, Pare
}
}
class AnimatedFractionalOffsetValue extends AnimatedValue<FractionalOffset> {
AnimatedFractionalOffsetValue(FractionalOffset begin, { FractionalOffset end, Curve curve, Curve reverseCurve })
: super(begin, end: end, curve: curve, reverseCurve: reverseCurve);
class FractionalOffsetTween extends Tween<FractionalOffset> {
FractionalOffsetTween({ FractionalOffset begin, FractionalOffset end }) : super(begin: begin, end: end);
FractionalOffset lerp(double t) => FractionalOffset.lerp(begin, end, t);
}

View File

@@ -30,32 +30,41 @@ class SmoothlyResizingOverflowBox extends StatefulComponent {
}
class _SmoothlyResizingOverflowBoxState extends State<SmoothlyResizingOverflowBox> {
ValuePerformance<Size> _size;
SizeTween _sizeTween;
CurveTween _curveTween;
Animated<Size> _size;
AnimationController _sizeController;
void initState() {
super.initState();
_size = new ValuePerformance(
variable: new AnimatedSizeValue(config.size, curve: config.curve),
duration: config.duration
)..addListener(() {
setState(() {});
});
_sizeController = new AnimationController(duration: config.duration);
_sizeTween = new SizeTween(begin: config.size);
_curveTween = new CurveTween(curve: config.curve);
_size = _sizeTween.chain(_curveTween).animate(_sizeController)
..addListener(() {
setState(() {});
});
}
void didUpdateConfig(SmoothlyResizingOverflowBox oldConfig) {
_size.duration = config.duration;
_size.variable.curve = config.curve;
bool needsAnimation = false;
if (config.size != oldConfig.size) {
AnimatedSizeValue variable = _size.variable;
variable.begin = variable.value;
variable.end = config.size;
_size.progress = 0.0;
_size.play();
_sizeTween
..begin = _size.value
..end = config.size;
needsAnimation = true;
}
_sizeController.duration = config.duration;
_curveTween.curve = config.curve;
if (needsAnimation) {
_sizeController
..value = 0.0
..forward();
}
}
void dispose() {
_size.stop();
_sizeController.stop();
super.dispose();
}

View File

@@ -193,24 +193,6 @@ class RotationTransition extends AnimatedComponent {
}
}
class OldFadeTransition extends TransitionWithChild {
OldFadeTransition({
Key key,
this.opacity,
PerformanceView performance,
Widget child
}) : super(key: key,
performance: performance,
child: child);
final AnimatedValue<double> opacity;
Widget buildWithChild(BuildContext context, Widget child) {
performance.updateVariable(opacity);
return new Opacity(opacity: opacity.value, child: child);
}
}
class FadeTransition extends AnimatedComponent {
FadeTransition({
Key key,
@@ -226,20 +208,17 @@ class FadeTransition extends AnimatedComponent {
}
}
class ColorTransition extends TransitionWithChild {
class ColorTransition extends AnimatedComponent {
ColorTransition({
Key key,
this.color,
PerformanceView performance,
Widget child
}) : super(key: key,
performance: performance,
child: child);
Animated<Color> color,
this.child
}) : color = color, super(key: key, animation: color);
final AnimatedColorValue color;
final Animated<Color> color;
final Widget child;
Widget buildWithChild(BuildContext context, Widget child) {
performance.updateVariable(color);
Widget build(BuildContext context) {
return new DecoratedBox(
decoration: new BoxDecoration(backgroundColor: color.value),
child: child