写了个Tween<Color> 报错: NoSuchMethodError:the method '-' was called on null
color = Tween<Color>(begin: Colors.green, end: Colors.red).animate(
CurvedAnimation(
parent: controller, curve: Interval(.0, .6, curve: Curves.ease)));
写错了, 正确写法是使用ColorTween:
color = ColorTween(begin: Colors.green, end: Colors.red).animate(
CurvedAnimation(
parent: controller, curve: Interval(.0, .6, curve: Curves.ease)));
class Tween 应该处理的是double类型渐变效果, 颜色渐变应使用ColorTween
class ColorTween extends Tween<Color?> {
/// Creates a [Color] tween.
///
/// The [begin] and [end] properties may be null; the null value
/// is treated as transparent.
///
/// We recommend that you do not pass [Colors.transparent] as [begin]
/// or [end] if you want the effect of fading in or out of transparent.
/// Instead prefer null. [Colors.transparent] refers to black transparent and
/// thus will fade out of or into black which is likely unwanted.
ColorTween({ Color? begin, Color? end }) : super(begin: begin, end: end);
/// Returns the value this variable has at the given animation clock value.
@override
Color? lerp(double t) => Color.lerp(begin, end, t);
}
网上找到类似问题: flutter报错:NoSuchMethodError: The method '>' was called on null.
是由于 Column或者Row的children里有Flexible导致。
不得不说,对Flutter Tween类还是不熟悉,而且Flutter类型判断还是不完善。