Flutter使用AnimatedSwitcher實現(xiàn)場景切換動畫
前言
在應用中,我們經(jīng)常會遇到切換組件的場景,比如點擊一個按鈕后,將當前的圖片為另一張圖片;或者是翻轉(zhuǎn)卡片,顯示卡片詳情。在 Flutter 中提供了 AnimatedSwitcher
這個動畫組件來實現(xiàn)頁面內(nèi)的場景切換。
AnimatedSwitcher.gif
AnimatedSwitcher 介紹
AnimatedSwitcher
通過動效完成其子組件的切換,默認的效果是 FadeTransition
。當其子組件發(fā)生改變的時候,就會按設定的轉(zhuǎn)變效果進行轉(zhuǎn)換。AnimatedSwitcher
的構(gòu)造方法如下:
const?AnimatedSwitcher({ ??Key??key, ??this.child, ??required?this.duration, ??this.reverseDuration, ??this.switchInCurve?=?Curves.linear, ??this.switchOutCurve?=?Curves.linear, ??this.transitionBuilder?=?AnimatedSwitcher.defaultTransitionBuilder, ??this.layoutBuilder?=?AnimatedSwitcher.defaultLayoutBuilder, })
與之前其他的動畫組件不同,AnimatedSwitcher
的參數(shù)除了 duration
之外,其他的有所不同,具體如下:
reverseDuration
:反向時長,也就是切換為舊組件的時長,不設置的話就和duration
一致。switchInCurve
:切入動畫曲線,也就是新組件切換進入的曲線;switchOutCurve
:切出動畫曲線,也就是舊組件切換出去時的曲線;transitionBuilder
:切換轉(zhuǎn)變動畫構(gòu)建,是一個函數(shù),定義如下,可以用這個方法來構(gòu)建自己的切換動效。
typedef?AnimatedSwitcherTransitionBuilder?=?Widget?Function(Widget?child,?Animation<double>?animation);
layoutBuilder
:可以設置新組件在組件樹中的布局,也是一個函數(shù):
typedef?AnimatedSwitcherLayoutBuilder?=?Widget?Function(Widget??currentChild,?List<Widget>?previousChildren);
默認布局為 defaultLayoutBuilder
,也就是將當前組件放置在最頂層:
static?Widget?defaultLayoutBuilder(Widget??currentChild,?List<Widget>?previousChildren)?{ ??return?Stack( ????children:?<Widget>[ ??????...previousChildren, ??????if?(currentChild?!=?null)?currentChild, ????], ????alignment:?Alignment.center, ??); }
關(guān)于AnimatedSwitcher
有一個地方需要特別注意,那就是如果切換的兩個組件相同的話,AnimatedSwitcher
會以為組件沒有改變,而不會進行動效切換。文檔說明如下:
The child is considered to be "new" if it has a different type or [Key]
實際上是根據(jù) Widget
的 canUpdate
判斷的:
static?int?_debugConcreteSubtype(Widget?widget)?{ ??return?widget?is?StatefulWidget???1?: ?????????widget?is?StatelessWidget???2?: ?????????0; }
因此,如果兩個組件類型相同,需要使用不同的 Key
來區(qū)分,通常是使用 ValueKey
。
應用
下面我們來實現(xiàn)開篇的動效,這個使用的是 SizeTransition
尺寸變化轉(zhuǎn)變動效完成兩張圖片的切換,這樣會讓組件的尺寸從小變到大,有一種掀開面紗的感覺。代碼如下:
class?AnimatedSwitcherDemo?extends?StatefulWidget?{ ??AnimatedSwitcherDemo({Key??key})?:?super(key:?key); ??@override ??_AnimatedSwitcherDemoState?createState()?=>?_AnimatedSwitcherDemoState(); } class?_AnimatedSwitcherDemoState?extends?State<AnimatedSwitcherDemo>?{ ??Widget??_animatedWidget; ??bool?test?=?false; ??@override ??void?initState()?{ ????super.initState(); ????_animatedWidget?=?ClipOval( ??????child:?Image.asset('images/beauty.jpeg'), ??????key:?ValueKey(1), ????); ??} ??@override ??Widget?build(BuildContext?context)?{ ????return?Scaffold( ??????appBar:?AppBar( ????????title:?Text('AnimatedSwticher'), ????????brightness:?Brightness.dark, ????????backgroundColor:?Colors.black, ??????), ??????backgroundColor:?Colors.black, ??????body:?Center( ????????child:?Container( ??????????padding:?EdgeInsets.all(10.0), ??????????child:?AnimatedSwitcher( ????????????child:?_animatedWidget, ????????????duration:?const?Duration(milliseconds:?1000), ????????????transitionBuilder:?(child,?animation)?{ ??????????????return?SizeTransition( ????????????????sizeFactor:?animation, ????????????????child:?child, ??????????????); ????????????}, ??????????), ????????), ??????), ??????floatingActionButton:?FloatingActionButton( ????????child:?Icon(Icons.play_arrow), ????????onPressed:?()?{ ??????????setState(()?{ ????????????test?=?!test; ????????????_animatedWidget?=?test ??????????????????ClipOval( ????????????????????child:?Image.asset('images/beauty2.jpeg'), ????????????????????key:?ValueKey(2), ??????????????????) ????????????????:?ClipOval( ????????????????????child:?Image.asset('images/beauty.jpeg'), ????????????????????key:?ValueKey(1), ??????????????????); ??????????}); ????????}, ??????), ????); ??} }
總結(jié)
本篇介紹了 AnimatedSwitcher
動畫組件的使用。AnimatedSwitcher
可以用于界面中組件的切換過渡動效,并且可以控制切入切出的時長、動畫取消、過渡效果和布局,從而可以實現(xiàn)一些有趣的切換效果。另外,需要注意的是,如果要切換的子組件類型相同,則需要使用 Key
進行區(qū)分,否則動效不會呈現(xiàn)出來。
到此這篇關(guān)于Flutter使用AnimatedSwitcher實現(xiàn)場景切換動畫的文章就介紹到這了,更多相關(guān)Flutter場景切換動畫內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Android實現(xiàn)炫酷的網(wǎng)絡直播彈幕功能
這篇文章主要為大家詳細介紹了Android仿網(wǎng)絡直播彈幕功能的實現(xiàn)方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-11-11實例解析Android ImageView的scaleType屬性
通過本文給大家介紹ImageView這個控件的一些使用方法,以及其最重要的一個屬性: scaleType,對imageview的scaletype相關(guān)知識感興趣的朋友一起學習吧2016-01-01使用CountDownTimer類輕松實現(xiàn)倒計時功能
Android中有個countDownTimer類,從名字上就可以看出來,它的功能是記錄下載時間,將后臺線程的創(chuàng)建和Handler隊列封裝成為了一個方便的調(diào)用。2014-07-07Android開發(fā)中實現(xiàn)應用的前后臺切換效果
這篇文章主要介紹了Android開發(fā)中實現(xiàn)應用的前后臺切換效果的方法,文章最后還附帶了監(jiān)聽程序是否進入后臺的判斷方法,需要的朋友可以參考下2016-02-02Android?Banner本地和網(wǎng)絡輪播圖使用介紹
大家好,本篇文章講的是Android?Banner本地和網(wǎng)絡輪播圖使用介紹,感興趣的同學趕快來看一看吧,希望本篇文章對你起到幫助2021-11-11