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.
This commit is contained in:
Nate Wilson
2025-01-09 11:00:31 -07:00
committed by GitHub
parent e0b424b7cd
commit 3d433adf87

View File

@@ -359,8 +359,7 @@ abstract class ImplicitlyAnimatedWidgetState<T extends ImplicitlyAnimatedWidget>
with SingleTickerProviderStateMixin<T> {
/// 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<T extends ImplicitlyAnimatedWidget>
@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<T extends ImplicitlyAnimatedWidget>
_animation.dispose();
_animation = _createCurve();
}
_controller.duration = widget.duration;
controller.duration = widget.duration;
if (_constructTweens()) {
forEachTween((
Tween<dynamic>? tween,
dynamic targetValue,
TweenConstructor<dynamic> 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<dynamic> tween, dynamic targetValue) {
return targetValue != (tween.end ?? tween.begin);
}
void _updateTween(Tween<dynamic>? 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<T extends ImplicitlyAnimatedWidget>
) {
if (targetValue != null) {
tween ??= constructor(targetValue);
if (_shouldAnimateTween(tween, targetValue)) {
if (targetValue != (tween.end ?? tween.begin)) {
shouldStartAnimation = true;
} else {
tween.end ??= tween.begin;