1. <strong id="7actg"></strong>
    2. <table id="7actg"></table>

    3. <address id="7actg"></address>
      <address id="7actg"></address>
      1. <object id="7actg"><tt id="7actg"></tt></object>

        【Flutter】 五彩紙屑動(dòng)畫效果

        共 12615字,需瀏覽 26分鐘

         ·

        2021-09-10 23:26

        在在這個(gè)博客中,我們將「探索 Flutter 中的五彩紙屑動(dòng)畫」。我們將看到如何實(shí)現(xiàn)五彩紙屑動(dòng)畫的演示程序,并在您的 flutter 應(yīng)用程序中使用 「confetti」 包展示多彩的爆炸效果。

        confetti 地址:https://pub.dev/packages/confetti

        五彩紙屑是屏幕上隨處可見的彩色五彩紙屑的效果??刂莆宀始埿嫉乃俣?、角度、重力和尺寸。下面的demo中當(dāng)用戶點(diǎn)擊按鈕時(shí),會(huì)出現(xiàn)五顏六色的五彩紙屑。

        這個(gè)演示視頻展示了如何在Flutter中創(chuàng)建五彩紙屑動(dòng)畫。它展示了如何在你的 flutter 應(yīng)用程序中使用「confetti」包來制作五彩紙屑動(dòng)畫。當(dāng)用戶點(diǎn)擊按鈕時(shí),它會(huì)顯示五顏六色的五彩紙屑爆炸,然后發(fā)生,用戶可以處理爆炸類型、角度等。

        屬性

        • 「ConfettiController」:該屬性不能為空。唯一需要的屬性是 「ConfettiController」.
        • 「blastDirectionality」:這個(gè)屬性用于一個(gè)枚舉,它采用兩個(gè)值之一——方向性或爆炸性。默認(rèn)設(shè)置為定向。
        • 「shouldLoop」:該屬性用于確定emissionDuration 是否會(huì)重置,從而導(dǎo)致連續(xù)的粒子被發(fā)射。
        • 「maxBlastForce」:此屬性用于確定在粒子生命的前 5 幀內(nèi)施加到粒子的最大爆炸力。默認(rèn) maxBlastForce 設(shè)置為“20”。
        • 「blastDirection」:該屬性用于徑向值確定粒子發(fā)射方向。默認(rèn)設(shè)置為“PI”(180 度)。PI 的值將發(fā)射到畫布/屏幕的左側(cè)。
        • 「numberOfParticles」:此屬性用于每次發(fā)射時(shí)發(fā)射。默認(rèn)設(shè)置為“10”。

        使用

        1. 添加依賴

          confetti: ^0.5.5
        2. 導(dǎo)入

          import 'package:confetti/confetti.dart';
        3. 執(zhí)行 「flutter packages get」 命令

        實(shí)現(xiàn)

        初始化 「ConfettiController」

        ConfettiController controllerTopCenter;

        @override
        void initState() {
          // TODO: implement initState
          super.initState();
          setState(() {
            initController();
          });

        }

        void initController() {
          controllerTopCenter =
              ConfettiController(duration: const Duration(seconds: 1));
        }

        創(chuàng)建一個(gè)按鈕和獎(jiǎng)杯圖片

        SafeArea(
          child: Stack(
            children: <Widget>[
              Align(
                alignment: Alignment.center,
                child: Column(
                  children: <Widget>[
                    Image.asset(
                      "assets/trophy.png",
                      width: MediaQuery.of(context).size.width*0.5,
                      height: MediaQuery.of(context).size.height*0.5,
                    ),
                  ],
                ),
              ),
              buildButton()
            ],
          ),
          
          Align buildButton() {
          return Align(
            alignment: Alignment.bottomCenter,
            child: Padding(
              padding: const EdgeInsets.symmetric(vertical: 100),
              child: RaisedButton(
                onPressed: (){},
                shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(20)),
                color: Colors.red,
                textColor: Colors.white,
                child: Padding(
                  padding: const EdgeInsets.all(16.0),
                  child: Text(
                    "Congratulations!",
                    style: TextStyle(
                      fontSize: 30,
                      color: Colors.white,
                    ),
                  ),
                ),
              ),
            ),
          );
        }

        創(chuàng)建 「ConfettiWidget」

        Align buildConfettiWidget(controller, double blastDirection) {
          return Align(
            alignment: Alignment.topCenter,
            child: ConfettiWidget(
              maximumSize: Size(3030),
              shouldLoop: false,
              confettiController: controller,
              blastDirection: blastDirection,
              blastDirectionality: BlastDirectionality.directional,
              maxBlastForce: 20// set a lower max blast force
              minBlastForce: 8// set a lower min blast force
              emissionFrequency: 1,
              minBlastForce: 8// a lot of particles at once
              gravity: 1,
            ),
          );
        }

        點(diǎn)擊按鈕播放

        onPressed: (){ 
          controllerTopCenter.play(); 
        },

        完整代碼

        import 'dart:math';
        import 'package:confetti/confetti.dart';
        import 'package:flutter/material.dart';


        class MyHomePage extends StatefulWidget {
          @override
          _MyHomePageState createState() => _MyHomePageState();
        }

        class _MyHomePageState extends State<MyHomePage{
          ConfettiController controllerTopCenter;
          @override
          void initState() {
            // TODO: implement initState
            super.initState();
            setState(() {
              initController();
            });

          }

          void initController() {
            controllerTopCenter =
                ConfettiController(duration: const Duration(seconds: 1));
          }


          @override
          Widget build(BuildContext context) {
            return Scaffold(
              backgroundColor: Colors.pink[50],
              appBar: AppBar(
                backgroundColor: Colors.cyan,
                title: Text("Flutter Confetti Animation Demo"),
                automaticallyImplyLeading: false,
              ),

              body: SafeArea(
                child: Stack(
                  children: <Widget>[
                    buildConfettiWidget(controllerTopCenter, pi / 1),
                    buildConfettiWidget(controllerTopCenter, pi / 4),
                    Align(
                      alignment: Alignment.center,
                      child: Column(
                        children: <Widget>[
                          Image.asset(
                            "assets/trophy.png",
                            width: MediaQuery.of(context).size.width*0.5,
                            height: MediaQuery.of(context).size.height*0.5,
                          ),
                        ],
                      ),
                    ),
                    buildButton()
                  ],
                ),
              ),
            );
          }

          Align buildButton() {
            return Align(
              alignment: Alignment.bottomCenter,
              child: Padding(
                padding: const EdgeInsets.symmetric(vertical: 100),
                child: RaisedButton(
                  onPressed: (){
                    controllerTopCenter.play();

                  },
                  shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(20)),
                  color: Colors.red,
                  textColor: Colors.white,
                  child: Padding(
                    padding: const EdgeInsets.all(16.0),
                    child: Text(
                      "Congratulations!",
                      style: TextStyle(
                        fontSize: 30,
                        color: Colors.white,
                      ),
                    ),
                  ),
                ),
              ),
            );
          }

          Align buildConfettiWidget(controller, double blastDirection) {
            return Align(
              alignment: Alignment.topCenter,
              child: ConfettiWidget(
                maximumSize: Size(3030),
                shouldLoop: false,
                confettiController: controller,
                blastDirection: blastDirection,
                blastDirectionality: BlastDirectionality.directional,
                maxBlastForce: 20// set a lower max blast force
                minBlastForce: 8// set a lower min blast force
                emissionFrequency: 1,
                numberOfParticles: 8// a lot of particles at once
                gravity: 1,
              ),
            );
          }
        }

        原文鏈接:https://medium.com/flutterdevs/explore-confetti-animation-in-flutter-340edbe2951

        你可能還喜歡

        關(guān)注「老孟Flutter」
        讓你每天進(jìn)步一點(diǎn)點(diǎn)
        瀏覽 106
        點(diǎn)贊
        評(píng)論
        收藏
        分享

        手機(jī)掃一掃分享

        分享
        舉報(bào)
        評(píng)論
        圖片
        表情
        推薦
        點(diǎn)贊
        評(píng)論
        收藏
        分享

        手機(jī)掃一掃分享

        分享
        舉報(bào)
        1. <strong id="7actg"></strong>
        2. <table id="7actg"></table>

        3. <address id="7actg"></address>
          <address id="7actg"></address>
          1. <object id="7actg"><tt id="7actg"></tt></object>
            www色天堂| 懂色AV成人 | 超碰逼逼网 | 逼逼影院| 久久久三级片电影 | 色人人操| 午夜草视频| 在线免费黄色视频网站 | 无码视频免费看 | 蝌蚪PORNY中文自拍 |