您好,登錄后才能下訂單哦!
本文小編為大家詳細介紹“Android Flutter怎么在點擊事件上添加動畫效果”,內容詳細,步驟清晰,細節處理妥當,希望這篇“Android Flutter怎么在點擊事件上添加動畫效果”文章能幫助大家解決疑惑,下面跟著小編的思路慢慢深入,一起來學習新知識吧。
底部返回按鈕動畫其實就是個旋轉動畫,利用Transform.rotate設置angle的值即可,這里使用了GetX來對angle進行動態控制。
//返回鍵旋轉角度,初始旋轉45度,使其初始樣式為 + var angle = (pi / 4).obs; ///關閉按鈕旋轉動畫控制器 late final AnimationController closeController; late final Animation<double> closeAnimation; ///返回鍵旋轉動畫 closeController = AnimationController( duration: const Duration(milliseconds: 300), vsync: provider, ); ///返回鍵旋轉動畫 closeController = AnimationController( duration: const Duration(milliseconds: 300), vsync: provider, ); ///頁面渲染完才開始執行,不然第一次打開不會啟動動畫 WidgetsBinding.instance.addPostFrameCallback((duration) { closeAnimation = Tween(begin: pi / 4, end: pi / 2).animate(closeController) ..addListener(() { angle.value = closeAnimation.value; }); closeController.forward(); }); ///關閉按鈕點擊事件 void close() { ///反轉動畫,并關閉頁面 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, ), ))
四個欄目其實就是個平移動畫,只不過閑魚是四個欄目一起平移,而我選擇了變速平移,這樣視覺效果上會好一點。
//透明度變化 List<AnimationController> opacityControllerList = []; //上移動畫,由于每個欄目的移動速度不一樣,需要用List保存四個AnimationController, //如果想像閑魚那種整體上移,則只用一個AnimationController即可。 List<AnimationController> offsetControllerList = []; List<Animation<Offset>> offsetAnimationList = []; //之所以用addIf,是因為項目中這幾個欄目的顯示是動態顯示的,這里就直接寫成true Column( children: [] ..addIf( true, buildItem('assets/user/ic-train-nomal-car.webp',"學車加練","自主預約,快速拿證")) ..addIf( true, buildItem('assets/user/ic-train-fuuxn-car.webp',"有證復訓","優質陪練,輕松駕車")) ..addIf( true, buildItem('assets/user/ic-train-jiaxun-car.webp',"模擬加訓","考前加訓,臨考不懼")) ..addIf( true, buildItem('assets/user/ic-train-jiakao-car.webp',"駕考報名","快捷報名無門檻")) ..add(playWidget()) ..addAll([ 17.space, ]), ) //僅僅是為了在offsetController全部初始化完后執行play() Widget playWidget() { //執行動畫 play(); return Container(); } int i = 0; Widget buildItem(String img,String tab,String slogan) { //由于底部欄目是動態顯示的,需要在創建Widget時一同創建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)), ); } //執行動畫 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()); }); } } ///關閉按鈕點擊事件 void close() { ///反轉動畫,并關閉頁面 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(); }
漸變動畫使用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"), ), ///關閉按鈕點擊事件 void close() { imgController.reverse(); }
///頂部標題下移動畫控制器 late final AnimationController offsetTopController; late final Animation<Offset> offsetTopAnimation; ///頂部標題漸變動畫控制器 late final AnimationController opacityTopController; ///頂部標題上移動畫 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('易練只為您提供優質教練,為您的安全保駕護航', style: TextStyle( color: Color(0XFF141414), fontSize: 15)), ], ))), ///關閉按鈕點擊事件 void close() { offsetTopController.reverse(); opacityTopController.reverse(); }
最后,在關閉頁面的時候不要忘記注銷動畫。
///關閉時注銷動畫 void dispose() { for (int i = offsetControllerList.length - 1; i > 0; i--) { offsetControllerList[i].dispose(); } offsetTopController.dispose(); opacityTopController.dispose(); imgController.dispose(); closeController.dispose(); }
讀到這里,這篇“Android Flutter怎么在點擊事件上添加動畫效果”文章已經介紹完畢,想要掌握這篇文章的知識點還需要大家自己動手實踐使用過才能領會,如果想了解更多相關內容的文章,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。