Flutter實(shí)現(xiàn)不同縮放動(dòng)畫效果詳解
需求背景
組件縮放可以向著一個(gè)方向進(jìn)行縮放,放大列表中某一個(gè)Cell
期望它是向后進(jìn)行放大而非組件中心點(diǎn)開始縮放。具體效果如下圖所示:
可縮放組件介紹
ScaleTransition
ScaleTransition
具體實(shí)現(xiàn)如下代碼,設(shè)置AnimationController
控制器若需要增加數(shù)值操作可以再增加Animate
再調(diào)用forward
方法執(zhí)行。
PS:動(dòng)畫實(shí)現(xiàn)在以前文章中有介紹過
動(dòng)畫控制器 _scaleAnimationController = AnimationController( vsync: this, duration: Duration(milliseconds: 3000), ); scale = Tween(begin: 1.0, end: 1.29).animate(_scaleAnimationController); ScaleTransition( scale: scale, alignment: Alignment.centerLeft, child: Container( margin: EdgeInsets.all(50), color: Colors.yellow, height: 200, width: 100, ), ) _scaleAnimationController.forward();
如果希望修改縮放方向,可以為ScaleTransition
添加alignment
配置。例如centerLeft
表示組件靠左向右縮放。
ScaleTransition( scale: scale, alignment: Alignment.centerLeft, child: Container( margin: EdgeInsets.all(50), color: Colors.yellow, height: 200, width: 100, ), )
如圖所示默認(rèn)縮放是以組件中心點(diǎn)進(jìn)行縮放效果,設(shè)置alignment
則向著相反位置進(jìn)行縮放。
但ScaleTransition
并不能滿足需求功能,無法做到向著一個(gè)方向進(jìn)行縮放動(dòng)畫。
SizeTransition
SizeTransition
是以更改子組件尺寸實(shí)現(xiàn)動(dòng)畫效果,支持垂直或水平方向動(dòng)畫。
AnimationController _animationController = AnimationController(vsync: this, duration: Duration(seconds: 1)); _animationController.value = 1.0; Animation<double> _animation = CurvedAnimation( parent: _animationController, curve: Curves.fastLinearToSlowEaseIn); SizeTransition( sizeFactor: _animation, axis: Axis.horizontal, child: Container( color: Colors.blue, height: 100, width: 100, alignment: Alignment.center, child: Text("SizeTransition"), ), )
但在需求要求上還是不滿足期望的結(jié)果,SizeTransition
更適用在實(shí)現(xiàn)展開或是飛入的動(dòng)畫效果。
AnimatedSize
AnimatedSize
是自帶動(dòng)畫效果的組件,修改組件尺寸大小就能夠執(zhí)行縮放動(dòng)畫。
GestureDetector( child: AnimatedSize( duration: Duration(seconds: 2), child: Container( color: Colors.red, width: 100, height: height, alignment: Alignment.center, child: Container( height: 50, width: 50, color: Colors.yellow, child: Text("AnimatedSize"), ), ), ), onTap: () { height = 150; width = 150; setState(() {}); }, ),
但AnimatedSize
的問題在于它只作用于自身,若子布局設(shè)置了自身的尺寸就不會隨著父組件大小而變化。
AnimatedBuilder
AnimatedBuilder
主要結(jié)合Transform.scale
組件設(shè)置alignment
為Alignment.centerLeft
即可對組件實(shí)現(xiàn)向右縮放動(dòng)畫。
AnimationController _scaleAnimationController = AnimationController( vsync: this, duration: Duration(milliseconds: 3000), ); Animation<double> scale = Tween(begin: 1.0, end: 1.29).animate(_scaleAnimationController); AnimatedBuilder( animation: scale, builder: (context, widget) { return Transform.scale( alignment: Alignment.centerLeft, scale: scale.value, child: widget, ); }, child: GestureDetector( child: Container( margin: EdgeInsets.only(left: 15, right: 15), color: Colors.blue, width: 100, height: 50, child: Text("AnimatedBuilder"), ), onTap: (){ _scaleAnimationController.forward(); }, ), );
AnimatedBuilder
方式實(shí)現(xiàn)縮放需要為組件縮放預(yù)留好足夠空間進(jìn)行縮放放大操作,避免組件縮放后與其他組件出現(xiàn)重疊現(xiàn)象。
小結(jié)
實(shí)現(xiàn)縮放方式有多種但對于比較定制化縮放要求需求配合上Transform.scale
才能達(dá)到最終效果。Transform.scale
可以幫助動(dòng)畫實(shí)現(xiàn)上對于組件尺寸大小控制方向有所幫助。因此采用AnimatedBuilder
結(jié)合Transform.scale
是需求實(shí)現(xiàn)最優(yōu)方案。
以上就是Flutter實(shí)現(xiàn)不同縮放動(dòng)畫效果詳解的詳細(xì)內(nèi)容,更多關(guān)于Flutter縮放動(dòng)畫的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
詳解Flutter網(wǎng)絡(luò)圖片本地緩存的實(shí)現(xiàn)
這篇文章主要為大家介紹了詳解Flutter網(wǎng)絡(luò)圖片本地緩存的實(shí)現(xiàn)示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-04-04Android編程中避免內(nèi)存泄露的方法總結(jié)
這篇文章主要介紹了Android編程中避免內(nèi)存泄露的方法總結(jié),本文講解了最可能造成內(nèi)存泄露的幾個(gè)點(diǎn),并總結(jié)出如何應(yīng)對這些內(nèi)存泄露,需要的朋友可以參考下2014-08-08Android進(jìn)階Hook攔截系統(tǒng)實(shí)例化View過程實(shí)現(xiàn)App換膚功能
這篇文章主要為大家介紹了Android進(jìn)階Hook攔截系統(tǒng)實(shí)例化View過程實(shí)現(xiàn)App換膚功能詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01詳解Android Studio如何導(dǎo)入第三方類庫、jar包和so庫
這篇文章主要介紹了Android Studio如何導(dǎo)入第三方類庫、jar包和so庫的相關(guān)資料,需要的朋友可以參考下2017-06-06Flutter搞定寬高不統(tǒng)一布局開發(fā)的方法詳解
我們在開發(fā)移動(dòng)端界面的時(shí)候,經(jīng)常會遇到一組尺寸不一的組件需要作為同一組展示,所以本文就將利用Wrap組件搞定寬高不統(tǒng)一布局開發(fā),需要的可以參考一下2023-06-06android實(shí)現(xiàn)密碼框右側(cè)顯示小眼睛
這篇文章主要為大家詳細(xì)介紹了android實(shí)現(xiàn)密碼框右側(cè)顯示小眼睛,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-09-09