Flutter使用AnimatedSwitcher實(shí)現(xiàn)場(chǎng)景切換動(dòng)畫(huà)
前言
在應(yīng)用中,我們經(jīng)常會(huì)遇到切換組件的場(chǎng)景,比如點(diǎn)擊一個(gè)按鈕后,將當(dāng)前的圖片為另一張圖片;或者是翻轉(zhuǎn)卡片,顯示卡片詳情。在 Flutter 中提供了 AnimatedSwitcher 這個(gè)動(dòng)畫(huà)組件來(lái)實(shí)現(xiàn)頁(yè)面內(nèi)的場(chǎng)景切換。

AnimatedSwitcher.gif
AnimatedSwitcher 介紹
AnimatedSwitcher 通過(guò)動(dòng)效完成其子組件的切換,默認(rèn)的效果是 FadeTransition。當(dāng)其子組件發(fā)生改變的時(shí)候,就會(huì)按設(shè)定的轉(zhuǎn)變效果進(jì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,
})
與之前其他的動(dòng)畫(huà)組件不同,AnimatedSwitcher的參數(shù)除了 duration 之外,其他的有所不同,具體如下:
reverseDuration:反向時(shí)長(zhǎng),也就是切換為舊組件的時(shí)長(zhǎng),不設(shè)置的話就和duration一致。switchInCurve:切入動(dòng)畫(huà)曲線,也就是新組件切換進(jìn)入的曲線;switchOutCurve:切出動(dòng)畫(huà)曲線,也就是舊組件切換出去時(shí)的曲線;transitionBuilder:切換轉(zhuǎn)變動(dòng)畫(huà)構(gòu)建,是一個(gè)函數(shù),定義如下,可以用這個(gè)方法來(lái)構(gòu)建自己的切換動(dòng)效。
typedef?AnimatedSwitcherTransitionBuilder?=?Widget?Function(Widget?child,?Animation<double>?animation);
layoutBuilder:可以設(shè)置新組件在組件樹(shù)中的布局,也是一個(gè)函數(shù):
typedef?AnimatedSwitcherLayoutBuilder?=?Widget?Function(Widget??currentChild,?List<Widget>?previousChildren);
默認(rèn)布局為 defaultLayoutBuilder,也就是將當(dāng)前組件放置在最頂層:
static?Widget?defaultLayoutBuilder(Widget??currentChild,?List<Widget>?previousChildren)?{
??return?Stack(
????children:?<Widget>[
??????...previousChildren,
??????if?(currentChild?!=?null)?currentChild,
????],
????alignment:?Alignment.center,
??);
}
關(guān)于AnimatedSwitcher有一個(gè)地方需要特別注意,那就是如果切換的兩個(gè)組件相同的話,AnimatedSwitcher會(huì)以為組件沒(méi)有改變,而不會(huì)進(jìn)行動(dòng)效切換。文檔說(shuō)明如下:
The child is considered to be "new" if it has a different type or [Key]
實(shí)際上是根據(jù) Widget 的 canUpdate 判斷的:
static?int?_debugConcreteSubtype(Widget?widget)?{
??return?widget?is?StatefulWidget???1?:
?????????widget?is?StatelessWidget???2?:
?????????0;
}
因此,如果兩個(gè)組件類(lèi)型相同,需要使用不同的 Key 來(lái)區(qū)分,通常是使用 ValueKey。
應(yīng)用
下面我們來(lái)實(shí)現(xiàn)開(kāi)篇的動(dòng)效,這個(gè)使用的是 SizeTransition 尺寸變化轉(zhuǎn)變動(dòng)效完成兩張圖片的切換,這樣會(huì)讓組件的尺寸從小變到大,有一種掀開(kāi)面紗的感覺(jué)。代碼如下:
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 動(dòng)畫(huà)組件的使用。AnimatedSwitcher可以用于界面中組件的切換過(guò)渡動(dòng)效,并且可以控制切入切出的時(shí)長(zhǎng)、動(dòng)畫(huà)取消、過(guò)渡效果和布局,從而可以實(shí)現(xiàn)一些有趣的切換效果。另外,需要注意的是,如果要切換的子組件類(lèi)型相同,則需要使用 Key 進(jìn)行區(qū)分,否則動(dòng)效不會(huì)呈現(xiàn)出來(lái)。
到此這篇關(guān)于Flutter使用AnimatedSwitcher實(shí)現(xiàn)場(chǎng)景切換動(dòng)畫(huà)的文章就介紹到這了,更多相關(guān)Flutter場(chǎng)景切換動(dòng)畫(huà)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Android實(shí)現(xiàn)炫酷的網(wǎng)絡(luò)直播彈幕功能
這篇文章主要為大家詳細(xì)介紹了Android仿網(wǎng)絡(luò)直播彈幕功能的實(shí)現(xiàn)方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11
實(shí)例解析Android ImageView的scaleType屬性
通過(guò)本文給大家介紹ImageView這個(gè)控件的一些使用方法,以及其最重要的一個(gè)屬性: scaleType,對(duì)imageview的scaletype相關(guān)知識(shí)感興趣的朋友一起學(xué)習(xí)吧2016-01-01
Android?側(cè)滑按鈕的實(shí)現(xiàn)代碼
這篇文章主要介紹了Android?側(cè)滑按鈕的實(shí)現(xiàn),本文結(jié)合示例代碼圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-04-04
Android開(kāi)發(fā)實(shí)現(xiàn)錄屏小功能
這篇文章主要介紹了Android開(kāi)發(fā)實(shí)現(xiàn)錄屏小功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-07-07
Android中Notification 提示對(duì)話框
Notification,俗稱(chēng)通知,是一種具有全局效果的通知,它展示在屏幕的頂端,首先會(huì)表現(xiàn)為一個(gè)圖標(biāo)的形式,當(dāng)用戶(hù)向下滑動(dòng)的時(shí)候,展示出通知具體的內(nèi)容2016-01-01
基于Android實(shí)現(xiàn)百度地圖定位過(guò)程詳解
這篇文章主要介紹了基于Android實(shí)現(xiàn)百度地圖定位過(guò)程詳解,需要的朋友可以參考下2015-11-11
使用CountDownTimer類(lèi)輕松實(shí)現(xiàn)倒計(jì)時(shí)功能
Android中有個(gè)countDownTimer類(lèi),從名字上就可以看出來(lái),它的功能是記錄下載時(shí)間,將后臺(tái)線程的創(chuàng)建和Handler隊(duì)列封裝成為了一個(gè)方便的調(diào)用。2014-07-07
Android開(kāi)發(fā)中實(shí)現(xiàn)應(yīng)用的前后臺(tái)切換效果
這篇文章主要介紹了Android開(kāi)發(fā)中實(shí)現(xiàn)應(yīng)用的前后臺(tái)切換效果的方法,文章最后還附帶了監(jiān)聽(tīng)程序是否進(jìn)入后臺(tái)的判斷方法,需要的朋友可以參考下2016-02-02
Android AIDL——進(jìn)程通信機(jī)制詳解
這篇文章主要介紹了Android AIDL——進(jìn)程通信機(jī)制詳解的相關(guān)資料,并附簡(jiǎn)單實(shí)例,和實(shí)現(xiàn)效果圖,需要的朋友可以參考下2016-10-10
Android?Banner本地和網(wǎng)絡(luò)輪播圖使用介紹
大家好,本篇文章講的是Android?Banner本地和網(wǎng)絡(luò)輪播圖使用介紹,感興趣的同學(xué)趕快來(lái)看一看吧,希望本篇文章對(duì)你起到幫助2021-11-11

