溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Android?Flutter怎么實現(xiàn)仿閑魚動畫效果

發(fā)布時間:2023-02-23 14:37:38 來源:億速云 閱讀:106 作者:iii 欄目:開發(fā)技術(shù)

這篇文章主要講解了“Android Flutter怎么實現(xiàn)仿閑魚動畫效果”,文中的講解內(nèi)容簡單清晰,易于學(xué)習(xí)與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“Android Flutter怎么實現(xiàn)仿閑魚動畫效果”吧!

1、底部返回鍵旋轉(zhuǎn)動畫

底部返回按鈕動畫其實就是個旋轉(zhuǎn)動畫,利用Transform.rotate設(shè)置angle的值即可,這里使用了GetX來對angle進(jìn)行動態(tài)控制。

//返回鍵旋轉(zhuǎn)角度,初始旋轉(zhuǎn)45度,使其初始樣式為 +
var angle = (pi / 4).obs;

///關(guān)閉按鈕旋轉(zhuǎn)動畫控制器
late final AnimationController closeController;
late final Animation<double> closeAnimation;

///返回鍵旋轉(zhuǎn)動畫
closeController = AnimationController(
  duration: const Duration(milliseconds: 300),
  vsync: provider,
);

///返回鍵旋轉(zhuǎn)動畫
closeController = AnimationController(
  duration: const Duration(milliseconds: 300),
  vsync: provider,
);

///頁面渲染完才開始執(zhí)行,不然第一次打開不會啟動動畫
WidgetsBinding.instance.addPostFrameCallback((duration) {
  closeAnimation =
      Tween(begin: pi / 4, end: pi / 2).animate(closeController)
        ..addListener(() {
          angle.value = closeAnimation.value;
        });
  closeController.forward();
});


///關(guān)閉按鈕點(diǎn)擊事件
void close() {
  ///反轉(zhuǎn)動畫,并關(guān)閉頁面
  Future.delayed(
     const Duration(milliseconds: 120), () {
    Get.back();
  });

  closeController.reverse();
}


IconButton(
    onPressed: null,
    alignment: Alignment.center,
    icon: Transform.rotate(
      angle: controller.angle.value,
      child: SvgPicture.asset(
        "assets/user/ic-train-car-close.svg",
        width: 18,
        height: 18,
        color: Colors.black,
      ),
    ))

2、底部四個欄目變速上移動畫+漸變動畫

四個欄目其實就是個平移動畫,只不過閑魚是四個欄目一起平移,而我選擇了變速平移,這樣視覺效果上會好一點(diǎn)。

//透明度變化
List<AnimationController> opacityControllerList = [];
//上移動畫,由于每個欄目的移動速度不一樣,需要用List保存四個AnimationController,
//如果想像閑魚那種整體上移,則只用一個AnimationController即可。
List<AnimationController> offsetControllerList = [];
List<Animation<Offset>> offsetAnimationList = [];

//之所以用addIf,是因為項目中這幾個欄目的顯示是動態(tài)顯示的,這里就直接寫成true
Column(
    children: []
      ..addIf(
          true,
          buildItem('assets/user/ic-train-nomal-car.webp',"學(xué)車加練","自主預(yù)約,快速拿證"))
      ..addIf(
          true,
          buildItem('assets/user/ic-train-fuuxn-car.webp',"有證復(fù)訓(xùn)","優(yōu)質(zhì)陪練,輕松駕車"))
      ..addIf(
          true,
          buildItem('assets/user/ic-train-jiaxun-car.webp',"模擬加訓(xùn)","考前加訓(xùn),臨考不懼"))
      ..addIf(
          true,
          buildItem('assets/user/ic-train-jiakao-car.webp',"駕考報名","快捷報名無門檻"))
      ..add(playWidget())
      ..addAll([
        17.space,
      ]),
   )
      
//僅僅是為了在offsetController全部初始化完后執(zhí)行play()
Widget playWidget() {
  //執(zhí)行動畫
  play();
  return Container();
}

int i = 0;

Widget buildItem(String img,String tab,String slogan) {
  //由于底部欄目是動態(tài)顯示的,需要在創(chuàng)建Widget時一同創(chuàng)建offsetController和offsetAnimation
  i++;
  AnimationController offsetController = AnimationController(
    duration: Duration(milliseconds: 100 + i * 20),
    vsync: this,
  );
  Animation<Offset> offsetAnimation = Tween<Offset>(
    begin: const Offset(0, 2.5),
    end: const Offset(0, 0),
  ).animate(CurvedAnimation(
    parent: offsetController,
    // curve: Curves.easeInOutSine,
    curve: const Cubic(0.12, 0.28, 0.48, 1),
  ));

  AnimationController opacityController = AnimationController(
      duration: const Duration(milliseconds: 500),
      lowerBound: 0.2,
      upperBound: 1.0,
      vsync: this);

  opacityControllerList.add(opacityController);
  offsetControllerList.add(offsetController);
  offsetAnimationList.add(offsetAnimation);

  return SlideTransition(
    position: offsetAnimation,
    child: FadeTransition(
        opacity: opacityController,
        child: Container(
            margin: EdgeInsets.only(bottom: 16),
            height: 62,
            decoration: BoxDecoration(
                borderRadius: BorderRadius.all(Radius.circular(12)),
                color: const Color(0xfffafafa)),
            child:
            Row(mainAxisAlignment: MainAxisAlignment.center, children: [
              24.space,
              Image.asset(img, width: 44, height: 44),
              12.space,
              Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  mainAxisSize: MainAxisSize.min,
                  children: [
                    Text(tab,
                        style: const TextStyle(
                            color: Color(0XFF000000),
                            fontSize: 16,
                            fontWeight: FontWeight.bold)),
                    Text(slogan,
                        style: const TextStyle(
                            color: Color(0XFF6e6e6e), fontSize: 12)),
                  ]).expanded,
              Image.asset("assets/user/ic-train-arrow.webp",
                  width: 44, height: 44),
              17.space
            ])).inkWell(
            onTap: () {},
            delayMilliseconds: 50)),
  );
}

//執(zhí)行動畫
void play() async {
  for (int i = 0; i < offsetControllerList.length; i++) {
    opacityControllerList[i].forward();

    ///欄目正序依次延遲(40 + 2 * i) * i的時間,曲線速率
    Future.delayed(Duration(milliseconds: (40 + 2 * i) * i), () {
      offsetControllerList[i]
          .forward()
          .whenComplete(() => offsetControllerList[i].stop());
    });
  }
}



///關(guān)閉按鈕點(diǎn)擊事件
void close() {
  ///反轉(zhuǎn)動畫,并關(guān)閉頁面
  Future.delayed(
     const Duration(milliseconds: 120), () {
    Get.back();
  });

  for (int i = offsetControllerList.length - 1; i >= 0; i--) {
    ///欄目倒敘依次延遲(40 + 2 * (offsetControllerList.length-1-i)) * (offsetControllerList.length-1-i))的時間
    Future.delayed(
        Duration(
            milliseconds:
            (40 + 2 * (offsetControllerList.length-1-i)) * (offsetControllerList.length-1-i)), () {
      offsetControllerList[i].reverse();
    });
  }
  opacityTopController.reverse();
}

3、中間圖片漸變動畫

漸變動畫使用FadeTransition即可。

///圖片透明度漸變動畫控制器
late final AnimationController imgController;

///圖片透明度漸變動畫
imgController = AnimationController(
    duration: const Duration(milliseconds: 500),
    lowerBound: 0.0,
    upperBound: 1.0,
    vsync: provider);
imgController.forward().whenComplete(() => imgController.stop());

///漸變過渡
FadeTransition(
  opacity: imgController,
  child:
  Image.asset("assets/user/ic-traincar-guide.webp"),
),

///關(guān)閉按鈕點(diǎn)擊事件
void close() {
  imgController.reverse();
}

4、頂部文案漸變動畫+下移動畫

///頂部標(biāo)題下移動畫控制器
late final AnimationController offsetTopController;
late final Animation<Offset> offsetTopAnimation;

///頂部標(biāo)題漸變動畫控制器
late final AnimationController opacityTopController;


///頂部標(biāo)題上移動畫
offsetTopController = AnimationController(
  duration: const Duration(milliseconds: 300),
  vsync: provider,
);
offsetTopController
    .forward()
    .whenComplete(() => offsetTopController.stop());
offsetTopAnimation = Tween<Offset>(
  begin: const Offset(0, -0.8),
  end: const Offset(0, 0),
).animate(CurvedAnimation(
  parent: offsetTopController,
  curve: Curves.easeInOutCubic,
));
offsetTopController
    .forward()
    .whenComplete(() => offsetTopController.stop());
    
//UI
SlideTransition(
    position: offsetTopAnimation,
    child: FadeTransition(
        opacity: opacityTopController,
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          mainAxisAlignment: MainAxisAlignment.start,
          mainAxisSize: MainAxisSize.min,
          children: [
            80.space,
            const Text(
              '練車指南',
              style: TextStyle(
                color: Color(0XFF141414),
                fontSize: 32,
                fontWeight: FontWeight.w800,
              ),
            ),
            2.space,
            const Text('易練只為您提供優(yōu)質(zhì)教練,為您的安全保駕護(hù)航',
                style: TextStyle(
                    color: Color(0XFF141414),
                    fontSize: 15)),
          ],
        ))),
        

///關(guān)閉按鈕點(diǎn)擊事件
void close() {
  offsetTopController.reverse();
  opacityTopController.reverse();

}

5、注銷動畫

最后,在關(guān)閉頁面的時候不要忘記注銷動畫。

///關(guān)閉時注銷動畫
void dispose() {
  for (int i = offsetControllerList.length - 1; i > 0; i--) {
    offsetControllerList[i].dispose();
  }
  offsetTopController.dispose();
  opacityTopController.dispose();
  imgController.dispose();
  closeController.dispose();
}

感謝各位的閱讀,以上就是“Android Flutter怎么實現(xiàn)仿閑魚動畫效果”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對Android Flutter怎么實現(xiàn)仿閑魚動畫效果這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關(guān)知識點(diǎn)的文章,歡迎關(guān)注!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI