欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

基于Flutter制作一個(gè)火箭發(fā)射動(dòng)畫(huà)

 更新時(shí)間:2022年03月23日 15:57:20   作者:島上碼農(nóng)  
北京時(shí)間10月16日0時(shí)23分,神舟十三號(hào)飛船成功發(fā)射,為慶祝這一喜事,本文將用Flutter制作一個(gè)火箭發(fā)射動(dòng)畫(huà),感興趣的小伙伴可以動(dòng)手試一試

前言

北京時(shí)間10月16日0時(shí)23分,神舟十三號(hào)飛船成功發(fā)射,目前三名航天員已經(jīng)順利進(jìn)駐空間站,開(kāi)始為期6個(gè)月的“太空差旅”生活。

國(guó)家的航天技術(shù)的突飛猛進(jìn)也讓?shí)u上碼農(nóng)很自豪,今天看 Flutter 的動(dòng)畫(huà)知識(shí),看到了 AnimatedPositioned 這個(gè)組件,可以用于控制組件的相對(duì)位置移動(dòng)。結(jié)合這個(gè)神舟十三號(hào)的發(fā)射,靈機(jī)一動(dòng),正好可以使用AnimatedPositioned 這個(gè)組件實(shí)現(xiàn)火箭發(fā)射動(dòng)畫(huà)。話不多說(shuō),先上效果!

效果說(shuō)明

這里其實(shí)是兩張圖片疊加,一張是背景地球星空的背景圖,一張是火箭?;鸺诎l(fā)射過(guò)程中有兩個(gè)變化:

  • 高度越來(lái)越高,其實(shí)就是相對(duì)圖片背景圖底部的位置越來(lái)越大就可以實(shí)現(xiàn);
  • 尺寸越來(lái)越小,這個(gè)可以控制整個(gè)組件的尺寸實(shí)現(xiàn)。

然后是動(dòng)畫(huà)取消的選擇,火箭的速度是越來(lái)越快,試了幾個(gè) Flutter 自帶的曲線,發(fā)現(xiàn) easeInCubic 這個(gè)效果挺不錯(cuò)的,開(kāi)始慢,后面越來(lái)越快,和火箭發(fā)射的過(guò)程是類似的。

AnimatedPositioned介紹

AnimatedPositioned組件的使用方式其實(shí)和 AnimatedContainer 類似。只是AnimatedPositionedPositioned 組件的替代。構(gòu)造方法定義如下:

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,
  }) 

前面的參數(shù)和 Positioned 一樣,后面是動(dòng)畫(huà)控制參數(shù),這些參數(shù)的定義和 AnimatedContainer 的是一樣的:

  • curve:動(dòng)畫(huà)效果曲線;
  • duration:動(dòng)畫(huà)時(shí)長(zhǎng);
  • onEnd:動(dòng)畫(huà)結(jié)束后回調(diào)。

我們可以改變 lefttop,width等參數(shù)來(lái)實(shí)現(xiàn)動(dòng)畫(huà)過(guò)渡的效果。比如我們的火箭發(fā)射,就是修改 bottom (飛行高度控制)和 width (尺寸大小控制)來(lái)實(shí)現(xiàn)的。

火箭發(fā)射動(dòng)畫(huà)實(shí)現(xiàn)

有了上面的兩個(gè)分析,火箭發(fā)射動(dòng)畫(huà)就簡(jiǎn)單了!完整代碼如下:

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('火箭發(fā)射'),
        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(
          '發(fā)射',
          style: TextStyle(
            color: Colors.white,
          ),
          textAlign: TextAlign.center,
        ),
        onPressed: () {
          setState(() {
            rocketBottom = MediaQuery.of(context).size.height;
            rocketWidth = 40.0;
          });
        },
      ),
    );
  }
}

其中一開(kāi)始設(shè)置 bottom 為負(fù)值,是為了隱藏火箭的焰火,這樣會(huì)更有感覺(jué)一些。然后就是在點(diǎn)擊發(fā)射按鈕的時(shí)候,通過(guò) setState 更改底部距離和火箭尺寸就可以搞定了。

總結(jié)

通過(guò)神舟十三飛船發(fā)射,來(lái)一個(gè)火箭動(dòng)畫(huà)是不是挺有趣?其實(shí)這篇主要的知識(shí)點(diǎn)還是介紹 AnimatedPositioned 的應(yīng)用。通過(guò) AnimatedPositioned可以實(shí)現(xiàn)很多層疊組件的相對(duì)移動(dòng)變化效果,比如進(jìn)度條的滑塊,滑動(dòng)開(kāi)關(guān)等。各位 Flutter 玩家也可以利用 AnimatedPositioned 這個(gè)組件自己來(lái)玩一下好玩的動(dòng)畫(huà)效果哦!

到此這篇關(guān)于基于Flutter制作一個(gè)火箭發(fā)射動(dòng)畫(huà)的文章就介紹到這了,更多相關(guān)Flutter火箭發(fā)射動(dòng)畫(huà)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論