我有一个带有一个子部件的简单AnimatedWidget。
AnimatedContainer(
duration: Duration(milliseconds: 2000),
curve: Curves.bounceOut,
decoration: BoxDecoration(
color: Colors.purple,
),
child: FlutterLogo(
size: _boxSize,
),
),_boxSize被动画化的地方是这样的:
void _startAnimation() => setState(() {
_boxSize *= 1.7;
});然而,AnimatedContainer 并没有为子小部件使用。需要更改AnimatedContainer的直接属性才能使动画工作。
这符合文件规定:
The [AnimatedContainer] will automatically animate between the old
and new values of properties when they change using the provided curve
and duration. Properties that are null are not animated.
Its child and descendants are not animated.什么是,等效的 of AnimatedContainer,它也能够动画它的孩子
发布于 2020-03-17 04:20:26
很少有小部件能使孩子动画片。您可以使用AnimatedSwitcher Widget交换具有首选大小的新的颤振徽标小部件。
发布于 2019-04-03 04:09:56
没有神奇的小部件可以简单地递归地将所有的子部件动画化。但我认为你想要的是一个隐含的动画小部件。即。您将更改一个小部件的构造函数参数,当它改变时,它将从一个值动画到另一个值。
最简单的方法可能是带有ImplicitlyAnimatedWidget的AnimatedWidgetBaseState。因此,对于您的示例来说,要动画一个boxSize属性,如下所示:
class AnimatedFlutterLogo extends ImplicitlyAnimatedWidget {
const AnimatedFlutterLogo({Key key, @required this.boxSize, @required Duration duration})
: super(key: key, duration: duration);
final double boxSize;
@override
ImplicitlyAnimatedWidgetState<ImplicitlyAnimatedWidget> createState() => _AnimatedFlutterLogoState();
}
class _AnimatedFlutterLogoState extends AnimatedWidgetBaseState<AnimatedFlutterLogo> {
Tween<double> _boxSize;
@override
void forEachTween(visitor) {
_boxSize = visitor(_boxSize, widget.boxSize, (dynamic value) => Tween<double>(begin: value));
}
@override
Widget build(BuildContext context) {
return Container(
child: FlutterLogo(
size: _boxSize?.evaluate(animation),
),
);
}
}这已经相当简洁了,唯一真正的样板是forEachTween(visitor)方法,它必须为您想要动画的所有属性创建Tween对象。
https://stackoverflow.com/questions/55484121
复制相似问题