让ColorTween前进到立即结束并在不改变控制器的情况下正常反转?

Make ColorTween forward to end immediately and reverse normally without changing the controller in flutter?

我有以下代码:

Animation _colorTween = ColorTween(begin: Colors.black, end: Colors.white).animate(animation);

AnimatedBuilder(
  animation: _colorTween,
  builder: (_, _) => customBuilder(_colorTween.value),
),

变量animation来自另一个小部件,我根本无法更改它。我什至无法控制动画的前进和后退,因为它是由另一个内置小部件控制的。

所以我想知道是否可以在调用 forward 时让颜色立即变为 Colors.white。相反,我可以接受默认行为。

首先,我不会为您的输入动画命名 '_colorTween'。这只是令人困惑,因为它是动画!

但是由于您已经在使用自定义构建器,您可能会在自定义构建函数中执行类似的操作:

switch(_colorTween.status) {
case AnimationStatus.forward:
  return <Colors.white or something like it>
default:
  return <normal calculation>
}

虽然如果动画运行到一半,然后改变方向,这可能会导致一些奇怪的事情。如果需要,您可以在其中放置更多逻辑来处理该问题。