From 3bc906d3726c90328841520c67e77b8ccc0424e9 Mon Sep 17 00:00:00 2001 From: matthew-carroll Date: Thu, 7 Jun 2018 14:04:30 -0700 Subject: [PATCH] Extracted an ImplicitlyAnimatedWidgetState from AnimatedWidgetBaseState (#18181) Extracted a superclass from AnimatedWidgetBaseState that is called ImplicitlyAnimatedWidgetState. ImplicitlyAnimatedWidgetState implicitly animates, but it does not call setState() automatically. Subclasses must respond to animation changes themselves. This is a useful optimization for Widgets whose render objects listen to animations directly. (#18173) --- .../lib/src/widgets/implicit_animations.dart | 45 ++++++++++++++----- 1 file changed, 35 insertions(+), 10 deletions(-) diff --git a/packages/flutter/lib/src/widgets/implicit_animations.dart b/packages/flutter/lib/src/widgets/implicit_animations.dart index 3a01cce2e7..6b84e066f8 100644 --- a/packages/flutter/lib/src/widgets/implicit_animations.dart +++ b/packages/flutter/lib/src/widgets/implicit_animations.dart @@ -210,7 +210,7 @@ abstract class ImplicitlyAnimatedWidget extends StatefulWidget { final Duration duration; @override - AnimatedWidgetBaseState createState(); + ImplicitlyAnimatedWidgetState createState(); @override void debugFillProperties(DiagnosticPropertiesBuilder properties) { @@ -230,10 +230,17 @@ typedef Tween TweenVisitor(Tween tween, T targetValue, TweenConstructor /// A base class for widgets with implicit animations. /// -/// Subclasses must implement the [forEachTween] method to help -/// [AnimatedWidgetBaseState] iterate through the subclasses' widget's fields -/// and animate them. -abstract class AnimatedWidgetBaseState extends State with SingleTickerProviderStateMixin { +/// [ImplicitlyAnimatedWidgetState] requires that subclasses respond to the +/// animation, themselves. If you would like `setState()` to be called +/// automatically as the animation changes, use [AnimatedWidgetBaseState]. +/// +/// Subclasses must implement the [forEachTween] method to allow +/// [ImplicitlyAnimatedWidgetState] to iterate through the subclasses' widget's +/// fields and animate them. +abstract class ImplicitlyAnimatedWidgetState extends State with SingleTickerProviderStateMixin { + /// The animation controller driving this widget's implicit animations. + @protected + AnimationController get controller => _controller; AnimationController _controller; /// The animation driving this widget's implicit animations. @@ -247,7 +254,7 @@ abstract class AnimatedWidgetBaseState exten duration: widget.duration, debugLabel: '${widget.toStringShort()}', vsync: this, - )..addListener(_handleAnimationChanged); + ); _updateCurve(); _constructTweens(); } @@ -282,10 +289,6 @@ abstract class AnimatedWidgetBaseState exten super.dispose(); } - void _handleAnimationChanged() { - setState(() { }); - } - bool _shouldAnimateTween(Tween tween, dynamic targetValue) { return targetValue != (tween.end ?? tween.begin); } @@ -331,6 +334,28 @@ abstract class AnimatedWidgetBaseState exten void forEachTween(TweenVisitor visitor); } +/// A base class for widgets with implicit animations that need to rebuild their +/// widget tree as the animation runs. +/// +/// This class calls [build] each frame that the animation tickets. For a +/// variant that does not rebuild each frame, consider subclassing +/// [ImplicitlyAnimatedWidgetState] directly. +/// +/// Subclasses must implement the [forEachTween] method to allow +/// [AnimatedWidgetBaseState] to iterate through the subclasses' widget's fields +/// and animate them. +abstract class AnimatedWidgetBaseState extends ImplicitlyAnimatedWidgetState { + @override + void initState() { + super.initState(); + controller.addListener(_handleAnimationChanged); + } + + void _handleAnimationChanged() { + setState(() { /* The animation ticked. Rebuild with new animation value */ }); + } +} + /// A container that gradually changes its values over a period of time. /// /// The [AnimatedContainer] will automatically animate between the old and