首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Flutter界面为什么在多次build以后(如执行动画),会绘制多层layer? 求大佬解释?

Flutter界面为什么在多次build以后(如执行动画),会绘制多层layer? 求大佬解释?

提问于 2020-05-22 19:51:41
回答 0关注 0查看 653

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();

}

}

```

这是动画执行完成后的界面
这是动画执行完成后的界面

回答

和开发者交流更多问题细节吧,去 写回答
相关文章

相似问题

相关问答用户
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档