From 3d433adf87268cc01dcb09b93c1b55245f48d0e1 Mon Sep 17 00:00:00 2001 From: Nate Wilson Date: Thu, 9 Jan 2025 11:00:31 -0700 Subject: [PATCH] `ImplicitlyAnimatedWidgetState` code cleanup (#160567) preparing for https://github.com/flutter/flutter/pull/154378 It'd be nice to clean up the implicitly animated widget logic, especially since it could make the upcoming PR easier to review. --- .../lib/src/widgets/implicit_animations.dart | 35 ++++++------------- 1 file changed, 10 insertions(+), 25 deletions(-) diff --git a/packages/flutter/lib/src/widgets/implicit_animations.dart b/packages/flutter/lib/src/widgets/implicit_animations.dart index 5a19b5c57c..32b6b36ca5 100644 --- a/packages/flutter/lib/src/widgets/implicit_animations.dart +++ b/packages/flutter/lib/src/widgets/implicit_animations.dart @@ -359,8 +359,7 @@ abstract class ImplicitlyAnimatedWidgetState with SingleTickerProviderStateMixin { /// The animation controller driving this widget's implicit animations. @protected - AnimationController get controller => _controller; - late final AnimationController _controller = AnimationController( + late final AnimationController controller = AnimationController( duration: widget.duration, debugLabel: kDebugMode ? widget.toStringShort() : null, vsync: this, @@ -374,7 +373,7 @@ abstract class ImplicitlyAnimatedWidgetState @override void initState() { super.initState(); - _controller.addStatusListener((AnimationStatus status) { + controller.addStatusListener((AnimationStatus status) { if (status.isCompleted) { widget.onEnd?.call(); } @@ -391,48 +390,34 @@ abstract class ImplicitlyAnimatedWidgetState _animation.dispose(); _animation = _createCurve(); } - _controller.duration = widget.duration; + controller.duration = widget.duration; if (_constructTweens()) { forEachTween(( Tween? tween, dynamic targetValue, TweenConstructor constructor, ) { - _updateTween(tween, targetValue); - return tween; + return tween + ?..begin = tween.evaluate(_animation) + ..end = targetValue; }); - _controller - ..value = 0.0 - ..forward(); + controller.forward(from: 0.0); didUpdateTweens(); } } CurvedAnimation _createCurve() { - return CurvedAnimation(parent: _controller, curve: widget.curve); + return CurvedAnimation(parent: controller, curve: widget.curve); } @protected @override void dispose() { _animation.dispose(); - _controller.dispose(); + controller.dispose(); super.dispose(); } - bool _shouldAnimateTween(Tween tween, dynamic targetValue) { - return targetValue != (tween.end ?? tween.begin); - } - - void _updateTween(Tween? tween, dynamic targetValue) { - if (tween == null) { - return; - } - tween - ..begin = tween.evaluate(_animation) - ..end = targetValue; - } - bool _constructTweens() { bool shouldStartAnimation = false; forEachTween(( @@ -442,7 +427,7 @@ abstract class ImplicitlyAnimatedWidgetState ) { if (targetValue != null) { tween ??= constructor(targetValue); - if (_shouldAnimateTween(tween, targetValue)) { + if (targetValue != (tween.end ?? tween.begin)) { shouldStartAnimation = true; } else { tween.end ??= tween.begin;