新西兰服务器

基于Flutter怎么制作一个火箭发射动画


基于Flutter怎么制作一个火箭发射动画

发布时间:2022-03-23 17:47:03 来源:高防服务器网 阅读:56 作者:iii 栏目:开发技术

本文小编为大家详细介绍“基于Flutter怎么制作一个火箭发射动画”,内容详细,步骤清晰,细节处理妥当,希望这篇“基于Flutter怎么制作一个火箭发射动画”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。

AnimatedPositioned介绍

AnimatedPositioned组件的使用方式其实和 AnimatedContainer 类似。只是AnimatedPositionedPositioned 组件的替代。构造方法定义如下:

const AnimatedPositioned({      Key? key,      required this.child,      this.left,      this.top,      this.right,      this.bottom,      this.width,      this.height,      Curve curve = Curves.linear,      required Duration duration,      VoidCallback? onEnd,    })

前面的参数和 Positioned 一样,后面是动画控制参数,这些参数的定义和 AnimatedContainer 的是一样的:

  • curve:动画效果曲线;

  • duration:动画时长;

  • onEnd:动画结束后回调。

我们可以改变 lefttopwidth等参数来实现动画过渡的效果。比如我们的火箭发射,就是修改 bottom (飞行高度控制)和 width (尺寸大小控制)来实现的。

火箭发射动画实现

有了上面的两个分析,火箭发射动画就简单了!完整代码如下:

class RocketLaunch extends StatefulWidget {    RocketLaunch({Key? key}) : super(key: key);      @override    _RocketLaunchState createState() => _RocketLaunchState();  }    class _RocketLaunchState extends State<RocketLaunch> {    var rocketBottom = -80.0;    var rocketWidth = 160.0;    @override    Widget build(BuildContext context) {      return Scaffold(        appBar: AppBar(          title: Text('火箭发射'),          brightness: Brightness.dark,          backgroundColor: Colors.black,        ),        backgroundColor: Colors.black,        body: Center(          child: Stack(            alignment: Alignment.bottomCenter,            children: [              Image.asset(                'images/earth.jpeg',                height: double.infinity,                fit: BoxFit.fill,              ),              AnimatedPositioned(                child: Image.asset(                  'images/rocket.png',                  fit: BoxFit.fitWidth,                ),                bottom: rocketBottom,                width: rocketWidth,                duration: Duration(seconds: 5),                curve: Curves.easeInCubic,              ),            ],          ),        ),        floatingActionButton: FloatingActionButton(          child: Text(            '发射',            style: TextStyle(              color: Colors.white,            ),            textAlign: TextAlign.center,          ),          onPressed: () {            setState(() {              rocketBottom = MediaQuery.of(context).size.height;              rocketWidth = 40.0;            });          },        ),      );    }  }

其中一开始设置 bottom 为负值,是为了隐藏火箭的焰火,这样会更有感觉一些。然后就是在点击发射按钮的时候,通过 setState 更改底部距离和火箭尺寸就可以搞定了。

读到这里,这篇“基于Flutter怎么制作一个火箭发射动画”文章已经介绍完毕,想要掌握这篇文章的知识点还需要大家自己动手实践使用过才能领会,如果想了解更多相关内容的文章,欢迎关注高防服务器网行业资讯频道。

[微信提示:高防服务器能助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。

[图文来源于网络,不代表本站立场,如有侵权,请联系高防服务器网删除]
[