From 82d84c766f3a317869ee59e742870e2b693b2d30 Mon Sep 17 00:00:00 2001 From: Matt Perry Date: Thu, 13 Aug 2015 17:29:32 -0400 Subject: [PATCH] Add a ValueAnimation helper class for AnimationPerfomance. Used for AnimationPerformance with a single AnimatedValue variable. --- examples/widgets/ensure_visible.dart | 4 ++-- .../lib/animation/animation_performance.dart | 19 +++++++++++++++++-- packages/flutter/lib/widgets/scrollable.dart | 14 ++++++-------- packages/flutter/lib/widgets/tabs.dart | 10 +++++----- 4 files changed, 30 insertions(+), 17 deletions(-) diff --git a/examples/widgets/ensure_visible.dart b/examples/widgets/ensure_visible.dart index b0399a8c29..8fad5d7513 100644 --- a/examples/widgets/ensure_visible.dart +++ b/examples/widgets/ensure_visible.dart @@ -38,7 +38,7 @@ class EnsureVisibleApp extends App { List cardModels; BlockViewportLayoutState layoutState = new BlockViewportLayoutState(); ScrollListener scrollListener; - AnimationPerformance scrollAnimation; + ValueAnimation scrollAnimation; void initState() { List cardHeights = [ @@ -51,7 +51,7 @@ class EnsureVisibleApp extends App { return new CardModel(i, cardHeights[i], color); }); - scrollAnimation = new AnimationPerformance() + scrollAnimation = new ValueAnimation() ..duration = const Duration(milliseconds: 200) ..variable = new AnimatedValue(0.0, curve: ease); diff --git a/packages/flutter/lib/animation/animation_performance.dart b/packages/flutter/lib/animation/animation_performance.dart index 9e21b1261f..700f572cdc 100644 --- a/packages/flutter/lib/animation/animation_performance.dart +++ b/packages/flutter/lib/animation/animation_performance.dart @@ -26,13 +26,17 @@ enum AnimationStatus { // manipulating |progress|, or |fling| the timeline causing a physics-based // simulation to take over the progression. class AnimationPerformance { - AnimationPerformance({this.variable, this.duration}) { + AnimationPerformance({AnimatedVariable variable, this.duration}) : + _variable = variable { _timeline = new Timeline(_tick); } - AnimatedVariable variable; + AnimatedVariable _variable; Duration duration; + AnimatedVariable get variable => _variable; + void set variable(AnimatedVariable v) { _variable = v; } + // Advances from 0 to 1. On each tick, we'll update our variable's values. Timeline _timeline; Timeline get timeline => _timeline; @@ -179,3 +183,14 @@ class AnimationPerformance { _checkStatusChanged(); } } + +// Simple helper class for an animation with a single value. +class ValueAnimation extends AnimationPerformance { + ValueAnimation({AnimatedValue variable, Duration duration}) : + super(variable: variable, duration: duration); + + AnimatedValue get variable => _variable as AnimatedValue; + void set variable(AnimatedValue v) { _variable = v; } + + T get value => variable.value; +} diff --git a/packages/flutter/lib/widgets/scrollable.dart b/packages/flutter/lib/widgets/scrollable.dart index 5fb1a10877..25e02e97de 100644 --- a/packages/flutter/lib/widgets/scrollable.dart +++ b/packages/flutter/lib/widgets/scrollable.dart @@ -7,7 +7,6 @@ import 'dart:sky' as sky; import 'package:newton/newton.dart'; import 'package:sky/animation/animated_simulation.dart'; -import 'package:sky/animation/animated_value.dart'; import 'package:sky/animation/animation_performance.dart'; import 'package:sky/animation/scroll_behavior.dart'; import 'package:sky/rendering/box.dart'; @@ -44,7 +43,7 @@ abstract class Scrollable extends StatefulComponent { ScrollDirection scrollDirection; AnimatedSimulation _toEndAnimation; // See _startToEndAnimation() - AnimationPerformance _toOffsetAnimation; // Started by scrollTo() + ValueAnimation _toOffsetAnimation; // Started by scrollTo() void initState() { _toEndAnimation = new AnimatedSimulation(_tickScrollOffset); @@ -86,11 +85,11 @@ abstract class Scrollable extends StatefulComponent { ); } - void _startToOffsetAnimation(double newScrollOffset, AnimationPerformance animation) { + void _startToOffsetAnimation(double newScrollOffset, ValueAnimation animation) { _stopToEndAnimation(); _stopToOffsetAnimation(); - (animation.variable as AnimatedValue) + animation.variable ..begin = scrollOffset ..end = newScrollOffset; @@ -102,8 +101,7 @@ abstract class Scrollable extends StatefulComponent { } void _updateToOffsetAnimation() { - AnimatedValue offset = _toOffsetAnimation.variable; - scrollTo(offset.value); + scrollTo(_toOffsetAnimation.value); } void _updateToOffsetAnimationStatus(AnimationStatus status) { @@ -139,7 +137,7 @@ abstract class Scrollable extends StatefulComponent { super.didUnmount(); } - bool scrollTo(double newScrollOffset, { AnimationPerformance animation }) { + bool scrollTo(double newScrollOffset, { ValueAnimation animation }) { if (newScrollOffset == _scrollOffset) return false; @@ -233,7 +231,7 @@ Scrollable findScrollableAncestor({ Widget target }) { return ancestor; } -bool ensureWidgetIsVisible(Widget target, { AnimationPerformance animation }) { +bool ensureWidgetIsVisible(Widget target, { ValueAnimation animation }) { assert(target.mounted); assert(target.root is RenderBox); diff --git a/packages/flutter/lib/widgets/tabs.dart b/packages/flutter/lib/widgets/tabs.dart index 6811c21c4b..00007c3740 100644 --- a/packages/flutter/lib/widgets/tabs.dart +++ b/packages/flutter/lib/widgets/tabs.dart @@ -406,15 +406,15 @@ class TabBar extends Scrollable { Size _tabBarSize; List _tabWidths; - AnimationPerformance _indicatorAnimation; - AnimationPerformance _scrollAnimation; + ValueAnimation _indicatorAnimation; + ValueAnimation _scrollAnimation; void initState() { super.initState(); - _indicatorAnimation = new AnimationPerformance() + _indicatorAnimation = new ValueAnimation() ..duration = _kTabBarScroll ..variable = new AnimatedRect(null, curve: ease); - _scrollAnimation = new AnimationPerformance() + _scrollAnimation = new ValueAnimation() ..duration = _kTabBarScroll ..variable = new AnimatedValue(0.0, curve: ease); } @@ -430,7 +430,7 @@ class TabBar extends Scrollable { scrollBehavior.isScrollable = source.isScrollable; } - AnimatedRect get _indicatorRect => _indicatorAnimation.variable as AnimatedRect; + AnimatedRect get _indicatorRect => _indicatorAnimation.variable; void _startIndicatorAnimation(int fromTabIndex, int toTabIndex) { _indicatorRect