Commit e4940a01 authored by Adam Barth's avatar Adam Barth

Merge pull request #1314 from abarth/port_material2

Finish porting material.dart to AnimationController
parents 5ac6f931 08b27fd7
...@@ -122,22 +122,22 @@ class AnimationController extends Animated<double> ...@@ -122,22 +122,22 @@ class AnimationController extends Animated<double>
} }
/// A label that is used in the [toString] output. Intended to aid with /// A label that is used in the [toString] output. Intended to aid with
/// identifying performance instances in debug output. /// identifying animation controller instances in debug output.
final String debugLabel; final String debugLabel;
/// Returns a [Animation] for this performance, /// Returns a [Animated<double>] for this animation controller,
/// so that a pointer to this object can be passed around without /// so that a pointer to this object can be passed around without
/// allowing users of that pointer to mutate the Performance state. /// allowing users of that pointer to mutate the AnimationController state.
Animated<double> get view => this; Animated<double> get view => this;
/// The length of time this performance should last. /// The length of time this animation should last.
Duration duration; Duration duration;
SimulationStepper _timeline; SimulationStepper _timeline;
AnimationDirection get direction => _direction; AnimationDirection get direction => _direction;
AnimationDirection _direction = AnimationDirection.forward; AnimationDirection _direction = AnimationDirection.forward;
/// The progress of this performance along the timeline. /// The progress of this animation along the timeline.
/// ///
/// Note: Setting this value stops the current animation. /// Note: Setting this value stops the current animation.
double get value => _timeline.value.clamp(0.0, 1.0); double get value => _timeline.value.clamp(0.0, 1.0);
...@@ -282,7 +282,7 @@ class CurvedAnimation extends Animated<double> with ProxyAnimatedMixin { ...@@ -282,7 +282,7 @@ class CurvedAnimation extends Animated<double> with ProxyAnimatedMixin {
/// ///
/// 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
/// performance is used to animate. /// a animation is used to animate.
AnimationDirection _curveDirection; AnimationDirection _curveDirection;
void _handleStatusChanged(PerformanceStatus status) { void _handleStatusChanged(PerformanceStatus status) {
......
...@@ -61,22 +61,22 @@ class DrawerController extends StatefulComponent { ...@@ -61,22 +61,22 @@ class DrawerController extends StatefulComponent {
class DrawerControllerState extends State<DrawerController> { class DrawerControllerState extends State<DrawerController> {
void initState() { void initState() {
super.initState(); super.initState();
_performance = new Performance(duration: _kBaseSettleDuration) _controller = new AnimationController(duration: _kBaseSettleDuration)
..addListener(_performanceChanged) ..addListener(_animationChanged)
..addStatusListener(_performanceStatusChanged); ..addStatusListener(_animationStatusChanged);
} }
void dispose() { void dispose() {
_performance _controller
..removeListener(_performanceChanged) ..removeListener(_animationChanged)
..removeStatusListener(_performanceStatusChanged) ..removeStatusListener(_animationStatusChanged)
..stop(); ..stop();
super.dispose(); super.dispose();
} }
void _performanceChanged() { void _animationChanged() {
setState(() { setState(() {
// The performance's state is our build state, and it changed already. // The animation controller's state is our build state, and it changed already.
}); });
} }
...@@ -92,7 +92,7 @@ class DrawerControllerState extends State<DrawerController> { ...@@ -92,7 +92,7 @@ class DrawerControllerState extends State<DrawerController> {
} }
} }
void _performanceStatusChanged(PerformanceStatus status) { void _animationStatusChanged(PerformanceStatus status) {
switch (status) { switch (status) {
case PerformanceStatus.forward: case PerformanceStatus.forward:
_ensureHistoryEntry(); _ensureHistoryEntry();
...@@ -113,7 +113,7 @@ class DrawerControllerState extends State<DrawerController> { ...@@ -113,7 +113,7 @@ class DrawerControllerState extends State<DrawerController> {
close(); close();
} }
Performance _performance; AnimationController _controller;
double _width = _kEdgeDragWidth; double _width = _kEdgeDragWidth;
void _handleSizeChanged(Size newSize) { void _handleSizeChanged(Size newSize) {
...@@ -123,20 +123,20 @@ class DrawerControllerState extends State<DrawerController> { ...@@ -123,20 +123,20 @@ class DrawerControllerState extends State<DrawerController> {
} }
void _handlePointerDown(_) { void _handlePointerDown(_) {
_performance.stop(); _controller.stop();
_ensureHistoryEntry(); _ensureHistoryEntry();
} }
void _move(double delta) { void _move(double delta) {
_performance.progress += delta / _width; _controller.value += delta / _width;
} }
void _settle(Offset velocity) { void _settle(Offset velocity) {
if (_performance.isDismissed) if (_controller.isDismissed)
return; return;
if (velocity.dx.abs() >= _kMinFlingVelocity) { if (velocity.dx.abs() >= _kMinFlingVelocity) {
_performance.fling(velocity: velocity.dx / _width); _controller.fling(velocity: velocity.dx / _width);
} else if (_performance.progress < 0.5) { } else if (_controller.value < 0.5) {
close(); close();
} else { } else {
open(); open();
...@@ -144,18 +144,18 @@ class DrawerControllerState extends State<DrawerController> { ...@@ -144,18 +144,18 @@ class DrawerControllerState extends State<DrawerController> {
} }
void open() { void open() {
_performance.fling(velocity: 1.0); _controller.fling(velocity: 1.0);
} }
void close() { void close() {
_performance.fling(velocity: -1.0); _controller.fling(velocity: -1.0);
} }
final AnimatedColorValue _color = new AnimatedColorValue(Colors.transparent, end: Colors.black54); final ColorTween _color = new ColorTween(begin: Colors.transparent, end: Colors.black54);
final GlobalKey _gestureDetectorKey = new GlobalKey(); final GlobalKey _gestureDetectorKey = new GlobalKey();
Widget build(BuildContext context) { Widget build(BuildContext context) {
if (_performance.status == PerformanceStatus.dismissed) { if (_controller.status == PerformanceStatus.dismissed) {
return new Align( return new Align(
alignment: const FractionalOffset(0.0, 0.5), alignment: const FractionalOffset(0.0, 0.5),
child: new GestureDetector( child: new GestureDetector(
...@@ -167,7 +167,6 @@ class DrawerControllerState extends State<DrawerController> { ...@@ -167,7 +167,6 @@ class DrawerControllerState extends State<DrawerController> {
) )
); );
} else { } else {
_performance.updateVariable(_color);
return new GestureDetector( return new GestureDetector(
key: _gestureDetectorKey, key: _gestureDetectorKey,
onHorizontalDragUpdate: _move, onHorizontalDragUpdate: _move,
...@@ -179,7 +178,7 @@ class DrawerControllerState extends State<DrawerController> { ...@@ -179,7 +178,7 @@ class DrawerControllerState extends State<DrawerController> {
onTap: close, onTap: close,
child: new DecoratedBox( child: new DecoratedBox(
decoration: new BoxDecoration( decoration: new BoxDecoration(
backgroundColor: _color.value backgroundColor: _color.evaluate(_controller)
), ),
child: new Container() child: new Container()
) )
...@@ -190,7 +189,7 @@ class DrawerControllerState extends State<DrawerController> { ...@@ -190,7 +189,7 @@ class DrawerControllerState extends State<DrawerController> {
onPointerDown: _handlePointerDown, onPointerDown: _handlePointerDown,
child: new Align( child: new Align(
alignment: const FractionalOffset(1.0, 0.5), alignment: const FractionalOffset(1.0, 0.5),
widthFactor: _performance.progress, widthFactor: _controller.value,
child: new SizeObserver( child: new SizeObserver(
onSizeChanged: _handleSizeChanged, onSizeChanged: _handleSizeChanged,
child: new RepaintBoundary( child: new RepaintBoundary(
......
...@@ -344,15 +344,21 @@ class _InkSplash extends InkFeature implements InkSplash { ...@@ -344,15 +344,21 @@ class _InkSplash extends InkFeature implements InkSplash {
this.repositionToReferenceBox, this.repositionToReferenceBox,
VoidCallback onRemoved VoidCallback onRemoved
}) : super(renderer: renderer, referenceBox: referenceBox, onRemoved: onRemoved) { }) : super(renderer: renderer, referenceBox: referenceBox, onRemoved: onRemoved) {
_radius = new ValuePerformance<double>( _radiusController = new AnimationController(duration: _kUnconfirmedSplashDuration)
variable: new AnimatedValue<double>(_kSplashInitialSize, end: targetRadius), ..addListener(renderer.markNeedsPaint)
duration: _kUnconfirmedSplashDuration ..forward();
)..addListener(renderer.markNeedsPaint) _radius = new Tween<double>(
..play(); begin: _kSplashInitialSize,
_alpha = new ValuePerformance<int>( end: targetRadius
variable: new AnimatedIntValue(color.alpha, end: 0), ).animate(_radiusController);
duration: _kHighlightFadeDuration
)..addListener(_handleAlphaChange); _alphaController = new AnimationController(duration: _kHighlightFadeDuration)
..addListener(renderer.markNeedsPaint)
..addStatusListener(_handleAlphaStatusChanged);
_alpha = new IntTween(
begin: color.alpha,
end: 0
).animate(_alphaController);
} }
final Point position; final Point position;
...@@ -361,30 +367,32 @@ class _InkSplash extends InkFeature implements InkSplash { ...@@ -361,30 +367,32 @@ class _InkSplash extends InkFeature implements InkSplash {
final bool clipToReferenceBox; final bool clipToReferenceBox;
final bool repositionToReferenceBox; final bool repositionToReferenceBox;
ValuePerformance<double> _radius; Animated<double> _radius;
ValuePerformance<int> _alpha; AnimationController _radiusController;
Animated<int> _alpha;
AnimationController _alphaController;
void confirm() { void confirm() {
int duration = (targetRadius / _kSplashConfirmedVelocity).floor(); int duration = (targetRadius / _kSplashConfirmedVelocity).floor();
_radius.duration = new Duration(milliseconds: duration); _radiusController
_radius.play(); ..duration = new Duration(milliseconds: duration)
_alpha.play(); ..forward();
_alphaController.forward();
} }
void cancel() { void cancel() {
_alpha.play(); _alphaController.forward();
} }
void _handleAlphaChange() { void _handleAlphaStatusChanged(PerformanceStatus status) {
if (_alpha.value == _alpha.variable.end) if (status == PerformanceStatus.completed)
dispose(); dispose();
else
renderer.markNeedsPaint();
} }
void dispose() { void dispose() {
_radius.stop(); _radiusController.stop();
_alpha.stop(); _alphaController.stop();
super.dispose(); super.dispose();
} }
...@@ -398,7 +406,7 @@ class _InkSplash extends InkFeature implements InkSplash { ...@@ -398,7 +406,7 @@ class _InkSplash extends InkFeature implements InkSplash {
if (clipToReferenceBox) if (clipToReferenceBox)
canvas.clipRect(Point.origin & referenceBox.size); canvas.clipRect(Point.origin & referenceBox.size);
if (repositionToReferenceBox) if (repositionToReferenceBox)
center = Point.lerp(center, Point.origin, _radius.progress); center = Point.lerp(center, Point.origin, _radiusController.value);
canvas.drawCircle(center, _radius.value, paint); canvas.drawCircle(center, _radius.value, paint);
canvas.restore(); canvas.restore();
} else { } else {
...@@ -407,7 +415,7 @@ class _InkSplash extends InkFeature implements InkSplash { ...@@ -407,7 +415,7 @@ class _InkSplash extends InkFeature implements InkSplash {
canvas.clipRect(originOffset.toPoint() & referenceBox.size); canvas.clipRect(originOffset.toPoint() & referenceBox.size);
} }
if (repositionToReferenceBox) if (repositionToReferenceBox)
center = Point.lerp(center, referenceBox.size.center(Point.origin), _radius.progress); center = Point.lerp(center, referenceBox.size.center(Point.origin), _radiusController.value);
canvas.drawCircle(center + originOffset, _radius.value, paint); canvas.drawCircle(center + originOffset, _radius.value, paint);
if (clipToReferenceBox) if (clipToReferenceBox)
canvas.restore(); canvas.restore();
...@@ -424,11 +432,14 @@ class _InkHighlight extends InkFeature implements InkHighlight { ...@@ -424,11 +432,14 @@ class _InkHighlight extends InkFeature implements InkHighlight {
VoidCallback onRemoved VoidCallback onRemoved
}) : _color = color, }) : _color = color,
super(renderer: renderer, referenceBox: referenceBox, onRemoved: onRemoved) { super(renderer: renderer, referenceBox: referenceBox, onRemoved: onRemoved) {
_alpha = new ValuePerformance<int>( _alphaController = new AnimationController(duration: _kHighlightFadeDuration)
variable: new AnimatedIntValue(0, end: color.alpha), ..addListener(renderer.markNeedsPaint)
duration: _kHighlightFadeDuration ..addStatusListener(_handleAlphaStatusChanged)
)..addListener(_handleAlphaChange) ..forward();
..play(); _alpha = new IntTween(
begin: 0,
end: color.alpha
).animate(_alphaController);
} }
Color get color => _color; Color get color => _color;
...@@ -444,27 +455,27 @@ class _InkHighlight extends InkFeature implements InkHighlight { ...@@ -444,27 +455,27 @@ class _InkHighlight extends InkFeature implements InkHighlight {
bool get active => _active; bool get active => _active;
bool _active = true; bool _active = true;
ValuePerformance<int> _alpha;
Animated<int> _alpha;
AnimationController _alphaController;
void activate() { void activate() {
_active = true; _active = true;
_alpha.forward(); _alphaController.forward();
} }
void deactivate() { void deactivate() {
_active = false; _active = false;
_alpha.reverse(); _alphaController.reverse();
} }
void _handleAlphaChange() { void _handleAlphaStatusChanged(PerformanceStatus status) {
if (_alpha.value == 0.0 && !_active) if (status == PerformanceStatus.dismissed && !_active)
dispose(); dispose();
else
renderer.markNeedsPaint();
} }
void dispose() { void dispose() {
_alpha.stop(); _alphaController.stop();
super.dispose(); super.dispose();
} }
......
...@@ -444,8 +444,8 @@ class _PersistentBottomSheet extends StatefulComponent { ...@@ -444,8 +444,8 @@ class _PersistentBottomSheet extends StatefulComponent {
class _PersistentBottomSheetState extends State<_PersistentBottomSheet> { class _PersistentBottomSheetState extends State<_PersistentBottomSheet> {
// We take ownership of the performance given in the first configuration. // We take ownership of the animation controller given in the first configuration.
// We also share control of that performance with out BottomSheet widget. // We also share control of that animation with out BottomSheet widget.
void initState() { void initState() {
super.initState(); super.initState();
......
...@@ -60,16 +60,17 @@ class ScrollbarPainter extends ScrollableListPainter { ...@@ -60,16 +60,17 @@ class ScrollbarPainter extends ScrollableListPainter {
paintScrollbar(context, offset); paintScrollbar(context, offset);
} }
ValuePerformance<double> _fade; AnimationController _fade;
Future scrollStarted() { Future scrollStarted() {
_fade ??= new ValuePerformance<double>() if (_fade == null) {
..duration = _kScrollbarThumbFadeDuration _fade = new AnimationController(duration: _kScrollbarThumbFadeDuration);
..variable = new AnimatedValue<double>(0.0, end: 1.0, curve: Curves.ease) CurvedAnimation curve = new CurvedAnimation(parent: _fade, curve: Curves.ease);
..addListener(() { curve.addListener(() {
_opacity = _fade.value; _opacity = curve.value;
renderObject?.markNeedsPaint(); renderObject?.markNeedsPaint();
}); });
}
return _fade.forward(); return _fade.forward();
} }
......
...@@ -90,10 +90,14 @@ class _RenderSlider extends RenderConstrainedBox { ...@@ -90,10 +90,14 @@ class _RenderSlider extends RenderConstrainedBox {
..onStart = _handleDragStart ..onStart = _handleDragStart
..onUpdate = _handleDragUpdate ..onUpdate = _handleDragUpdate
..onEnd = _handleDragEnd; ..onEnd = _handleDragEnd;
_reaction = new ValuePerformance<double>( _reactionController = new AnimationController(duration: kRadialReactionDuration);
variable: new AnimatedValue<double>(_kThumbRadius, end: _kReactionRadius, curve: Curves.ease), _reaction = new Tween<double>(
duration: kRadialReactionDuration begin: _kThumbRadius,
)..addListener(markNeedsPaint); end: _kReactionRadius
).animate(new CurvedAnimation(
parent: _reactionController,
curve: Curves.ease
))..addListener(markNeedsPaint);
} }
double get value => _value; double get value => _value;
...@@ -118,7 +122,9 @@ class _RenderSlider extends RenderConstrainedBox { ...@@ -118,7 +122,9 @@ class _RenderSlider extends RenderConstrainedBox {
ValueChanged<double> onChanged; ValueChanged<double> onChanged;
double get _trackLength => size.width - 2.0 * _kReactionRadius; double get _trackLength => size.width - 2.0 * _kReactionRadius;
ValuePerformance<double> _reaction;
Animated<double> _reaction;
AnimationController _reactionController;
HorizontalDragGestureRecognizer _drag; HorizontalDragGestureRecognizer _drag;
bool _active = false; bool _active = false;
...@@ -129,7 +135,7 @@ class _RenderSlider extends RenderConstrainedBox { ...@@ -129,7 +135,7 @@ class _RenderSlider extends RenderConstrainedBox {
_active = true; _active = true;
_currentDragValue = (globalToLocal(globalPosition).x - _kReactionRadius) / _trackLength; _currentDragValue = (globalToLocal(globalPosition).x - _kReactionRadius) / _trackLength;
onChanged(_currentDragValue.clamp(0.0, 1.0)); onChanged(_currentDragValue.clamp(0.0, 1.0));
_reaction.forward(); _reactionController.forward();
markNeedsPaint(); markNeedsPaint();
} }
} }
...@@ -145,7 +151,7 @@ class _RenderSlider extends RenderConstrainedBox { ...@@ -145,7 +151,7 @@ class _RenderSlider extends RenderConstrainedBox {
if (_active) { if (_active) {
_active = false; _active = false;
_currentDragValue = 0.0; _currentDragValue = 0.0;
_reaction.reverse(); _reactionController.reverse();
markNeedsPaint(); markNeedsPaint();
} }
} }
......
...@@ -147,24 +147,24 @@ class _RenderSwitch extends RenderToggleable { ...@@ -147,24 +147,24 @@ class _RenderSwitch extends RenderToggleable {
void _handleDragStart(Point globalPosition) { void _handleDragStart(Point globalPosition) {
if (onChanged != null) if (onChanged != null)
reaction.forward(); reactionController.forward();
} }
void _handleDragUpdate(double delta) { void _handleDragUpdate(double delta) {
if (onChanged != null) { if (onChanged != null) {
position.variable position
..curve = null ..curve = null
..reverseCurve = null; ..reverseCurve = null;
position.progress += delta / _trackInnerLength; positionController.value += delta / _trackInnerLength;
} }
} }
void _handleDragEnd(Offset velocity) { void _handleDragEnd(Offset velocity) {
if (position.progress >= 0.5) if (position.value >= 0.5)
position.forward(); positionController.forward();
else else
position.reverse(); positionController.reverse();
reaction.reverse(); reactionController.reverse();
} }
void handleEvent(PointerEvent event, BoxHitTestEntry entry) { void handleEvent(PointerEvent event, BoxHitTestEntry entry) {
...@@ -181,8 +181,8 @@ class _RenderSwitch extends RenderToggleable { ...@@ -181,8 +181,8 @@ class _RenderSwitch extends RenderToggleable {
final bool isActive = onChanged != null; final bool isActive = onChanged != null;
Color thumbColor = isActive ? Color.lerp(inactiveColor, activeColor, position.progress) : inactiveColor; Color thumbColor = isActive ? Color.lerp(inactiveColor, activeColor, position.value) : inactiveColor;
Color trackColor = isActive ? Color.lerp(inactiveTrackColor, activeTrackColor, position.progress) : inactiveTrackColor; Color trackColor = isActive ? Color.lerp(inactiveTrackColor, activeTrackColor, position.value) : inactiveTrackColor;
// Paint the track // Paint the track
Paint paint = new Paint() Paint paint = new Paint()
......
...@@ -416,7 +416,7 @@ class TabBarSelection<T> extends StatefulComponent { ...@@ -416,7 +416,7 @@ class TabBarSelection<T> extends StatefulComponent {
class TabBarSelectionState<T> extends State<TabBarSelection<T>> { class TabBarSelectionState<T> extends State<TabBarSelection<T>> {
Animated<double> get animation => _controller.view; Animated<double> get animation => _controller.view;
// Both the TabBar and TabBarView classes access _performance because they // Both the TabBar and TabBarView classes access _controller because they
// alternately drive selection progress between tabs. // alternately drive selection progress between tabs.
final AnimationController _controller = new AnimationController(duration: _kTabBarScroll, value: 1.0); final AnimationController _controller = new AnimationController(duration: _kTabBarScroll, value: 1.0);
final Map<T, int> _valueToIndex = new Map<T, int>(); final Map<T, int> _valueToIndex = new Map<T, int>();
...@@ -470,7 +470,7 @@ class TabBarSelectionState<T> extends State<TabBarSelection<T>> { ...@@ -470,7 +470,7 @@ class TabBarSelectionState<T> extends State<TabBarSelection<T>> {
_valueIsChanging = true; _valueIsChanging = true;
// If the selected value change was triggered by a drag gesture, the current // If the selected value change was triggered by a drag gesture, the current
// value of _performance.progress will reflect where the gesture ended. While // value of _controller.value will reflect where the gesture ended. While
// the drag was underway progress indicates where the indicator and TabBarView // the drag was underway progress indicates where the indicator and TabBarView
// scrollPosition are vis the indices of the two tabs adjacent to the selected // scrollPosition are vis the indices of the two tabs adjacent to the selected
// one. So 0.5 means the drag didn't move at all, 0.0 means the drag extended // one. So 0.5 means the drag didn't move at all, 0.0 means the drag extended
......
...@@ -336,10 +336,12 @@ class _Dial extends StatefulComponent { ...@@ -336,10 +336,12 @@ class _Dial extends StatefulComponent {
class _DialState extends State<_Dial> { class _DialState extends State<_Dial> {
void initState() { void initState() {
super.initState(); super.initState();
_theta = new ValuePerformance( _thetaController = new AnimationController(duration: _kDialAnimateDuration);
variable: new AnimatedValue<double>(_getThetaForTime(config.selectedTime), curve: Curves.ease), _thetaTween = new Tween<double>(begin: _getThetaForTime(config.selectedTime));
duration: _kDialAnimateDuration _theta = _thetaTween.animate(new CurvedAnimation(
)..addListener(() => setState(() { })); parent: _thetaController,
curve: Curves.ease
))..addListener(() => setState(() { }));
} }
void didUpdateConfig(_Dial oldConfig) { void didUpdateConfig(_Dial oldConfig) {
...@@ -347,7 +349,9 @@ class _DialState extends State<_Dial> { ...@@ -347,7 +349,9 @@ class _DialState extends State<_Dial> {
_animateTo(_getThetaForTime(config.selectedTime)); _animateTo(_getThetaForTime(config.selectedTime));
} }
ValuePerformance<double> _theta; Tween<double> _thetaTween;
Animated<double> _theta;
AnimationController _thetaController;
bool _dragging = false; bool _dragging = false;
static double _nearest(double target, double a, double b) { static double _nearest(double target, double a, double b) {
...@@ -358,11 +362,12 @@ class _DialState extends State<_Dial> { ...@@ -358,11 +362,12 @@ class _DialState extends State<_Dial> {
double currentTheta = _theta.value; double currentTheta = _theta.value;
double beginTheta = _nearest(targetTheta, currentTheta, currentTheta + _kTwoPi); double beginTheta = _nearest(targetTheta, currentTheta, currentTheta + _kTwoPi);
beginTheta = _nearest(targetTheta, beginTheta, currentTheta - _kTwoPi); beginTheta = _nearest(targetTheta, beginTheta, currentTheta - _kTwoPi);
_theta _thetaTween
..variable.begin = beginTheta ..begin = beginTheta
..variable.end = targetTheta ..end = targetTheta;
..progress = 0.0 _thetaController
..play(); ..value = 0.0
..forward();
} }
double _getThetaForTime(TimeOfDay time) { double _getThetaForTime(TimeOfDay time) {
...@@ -397,7 +402,9 @@ class _DialState extends State<_Dial> { ...@@ -397,7 +402,9 @@ class _DialState extends State<_Dial> {
void _updateThetaForPan() { void _updateThetaForPan() {
setState(() { setState(() {
Offset offset = _position - _center; Offset offset = _position - _center;
_theta.variable.value = (math.atan2(offset.dx, offset.dy) - math.PI / 2.0) % _kTwoPi; _thetaTween
..begin = (math.atan2(offset.dx, offset.dy) - math.PI / 2.0) % _kTwoPi
..end = null;
}); });
} }
......
...@@ -34,16 +34,24 @@ abstract class RenderToggleable extends RenderConstrainedBox { ...@@ -34,16 +34,24 @@ abstract class RenderToggleable extends RenderConstrainedBox {
..onTap = _handleTap ..onTap = _handleTap
..onTapUp = _handleTapUp ..onTapUp = _handleTapUp
..onTapCancel = _handleTapCancel; ..onTapCancel = _handleTapCancel;
_position = new ValuePerformance<double>(
variable: new AnimatedValue<double>(0.0, end: 1.0), _positionController = new AnimationController(
duration: _kToggleDuration, duration: _kToggleDuration,
progress: _value ? 1.0 : 0.0 value: _value ? 1.0 : 0.0
);
_position = new CurvedAnimation(
parent: _positionController
)..addListener(markNeedsPaint) )..addListener(markNeedsPaint)
..addStatusListener(_handlePositionStateChanged); ..addStatusListener(_handlePositionStateChanged);
_reaction = new ValuePerformance<double>(
variable: new AnimatedValue<double>(minRadialReactionRadius, end: kRadialReactionRadius, curve: Curves.ease), _reactionController = new AnimationController(duration: kRadialReactionDuration);
duration: kRadialReactionDuration _reaction = new Tween<double>(
)..addListener(markNeedsPaint); begin: minRadialReactionRadius,
end: kRadialReactionRadius
).animate(new CurvedAnimation(
parent: _reactionController,
curve: Curves.ease
))..addListener(markNeedsPaint);
} }
bool get value => _value; bool get value => _value;
...@@ -53,10 +61,10 @@ abstract class RenderToggleable extends RenderConstrainedBox { ...@@ -53,10 +61,10 @@ abstract class RenderToggleable extends RenderConstrainedBox {
if (value == _value) if (value == _value)
return; return;
_value = value; _value = value;
_position.variable _position
..curve = Curves.easeIn ..curve = Curves.easeIn
..reverseCurve = Curves.easeOut; ..reverseCurve = Curves.easeOut;
_position.play(value ? AnimationDirection.forward : AnimationDirection.reverse); _positionController.play(value ? AnimationDirection.forward : AnimationDirection.reverse);
} }
Color get activeColor => _activeColor; Color get activeColor => _activeColor;
...@@ -83,11 +91,15 @@ abstract class RenderToggleable extends RenderConstrainedBox { ...@@ -83,11 +91,15 @@ abstract class RenderToggleable extends RenderConstrainedBox {
ValueChanged<bool> onChanged; ValueChanged<bool> onChanged;
ValuePerformance<double> get position => _position; CurvedAnimation get position => _position;
ValuePerformance<double> _position; CurvedAnimation _position;
AnimationController get positionController => _positionController;
AnimationController _positionController;
ValuePerformance<double> get reaction => _reaction; AnimationController get reactionController => _reactionController;
ValuePerformance<double> _reaction; AnimationController _reactionController;
Animated<double> _reaction;
TapGestureRecognizer _tap; TapGestureRecognizer _tap;
...@@ -102,7 +114,7 @@ abstract class RenderToggleable extends RenderConstrainedBox { ...@@ -102,7 +114,7 @@ abstract class RenderToggleable extends RenderConstrainedBox {
void _handleTapDown(Point globalPosition) { void _handleTapDown(Point globalPosition) {
if (isInteractive) if (isInteractive)
_reaction.forward(); _reactionController.forward();
} }
void _handleTap() { void _handleTap() {
...@@ -112,12 +124,12 @@ abstract class RenderToggleable extends RenderConstrainedBox { ...@@ -112,12 +124,12 @@ abstract class RenderToggleable extends RenderConstrainedBox {
void _handleTapUp(Point globalPosition) { void _handleTapUp(Point globalPosition) {
if (isInteractive) if (isInteractive)
_reaction.reverse(); _reactionController.reverse();
} }
void _handleTapCancel() { void _handleTapCancel() {
if (isInteractive) if (isInteractive)
_reaction.reverse(); _reactionController.reverse();
} }
bool hitTestSelf(Point position) => true; bool hitTestSelf(Point position) => true;
...@@ -128,10 +140,10 @@ abstract class RenderToggleable extends RenderConstrainedBox { ...@@ -128,10 +140,10 @@ abstract class RenderToggleable extends RenderConstrainedBox {
} }
void paintRadialReaction(Canvas canvas, Offset offset) { void paintRadialReaction(Canvas canvas, Offset offset) {
if (!reaction.isDismissed) { if (!_reaction.isDismissed) {
// TODO(abarth): We should have a different reaction color when position is zero. // TODO(abarth): We should have a different reaction color when position is zero.
Paint reactionPaint = new Paint()..color = activeColor.withAlpha(kRadialReactionAlpha); Paint reactionPaint = new Paint()..color = activeColor.withAlpha(kRadialReactionAlpha);
canvas.drawCircle(offset.toPoint(), reaction.value, reactionPaint); canvas.drawCircle(offset.toPoint(), _reaction.value, reactionPaint);
} }
} }
} }
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