Flutter创建一个平移动画时,发现界面被重新build以后,会被绘制多遍。
怀疑是系统原因。 有没有大佬帮忙解释一下, 下面上代码:
```
class MessagePage extends StatefulWidget {
@override
State<StatefulWidget> createState() {
return _MessagePageState();
}
}
class _MessagePageState extends State<MessagePage> with SingleTickerProviderStateMixin{
@override
double position = 0;
AnimationController _animationController;
Animation<double> _animation;
initState(){
super.initState();
//初始化动画
_animationController = AnimationController(vsync: this, duration: Duration(seconds: 1));
_animation = Tween(begin: 0.0, end: 200.0).animate(_animationController);
_animation.addListener((){
setState(() {
});
});
_animationController.forward();
}
Widget build(BuildContext context) {
return Material(
/*
设置界面半透明(如果不设置半透明的话,
看不到多层覆盖,因为最新的绘制会遮挡住之前的绘制内容)
*/
color: Colors.blue.withOpacity(0.3),
child: Stack(
children: <Widget>[
Positioned(
left: _animation.value,
top: _animation.value,
child: Container(
color: Colors.red,
height: 100,
width: 100,
),
)
],
),
);
}
@override
void dispose() {
// TODO: implement dispose
super.dispose();
_animationController.dispose();
}
}
```
相似问题