網(wǎng)站首頁 編程語言 正文
需求背景
組件縮放可以向著一個方向進(jìn)行縮放,放大列表中某一個Cell
期望它是向后進(jìn)行放大而非組件中心點(diǎn)開始縮放。具體效果如下圖所示:
可縮放組件介紹
ScaleTransition
ScaleTransition
具體實(shí)現(xiàn)如下代碼,設(shè)置AnimationController
控制器若需要增加數(shù)值操作可以再增加Animate
再調(diào)用forward
方法執(zhí)行。
PS:動畫實(shí)現(xiàn)在以前文章中有介紹過
動畫控制器 _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
并不能滿足需求功能,無法做到向著一個方向進(jìn)行縮放動畫。
SizeTransition
SizeTransition
是以更改子組件尺寸實(shí)現(xiàn)動畫效果,支持垂直或水平方向動畫。
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)展開或是飛入的動畫效果。
AnimatedSize
AnimatedSize
是自帶動畫效果的組件,修改組件尺寸大小就能夠執(zhí)行縮放動畫。
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)向右縮放動畫。
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
可以幫助動畫實(shí)現(xiàn)上對于組件尺寸大小控制方向有所幫助。因此采用AnimatedBuilder
結(jié)合Transform.scale
是需求實(shí)現(xiàn)最優(yōu)方案。
原文鏈接:https://juejin.cn/post/7111719803614101541
相關(guān)推薦
- 2022-05-11 C#?中使用Stopwatch計時器實(shí)現(xiàn)暫停計時繼續(xù)計時功能_C#教程
- 2022-08-25 高級消息隊列協(xié)議AMQP簡介_其它綜合
- 2022-02-14 taro將頁面滾動到指定位置
- 2022-11-05 關(guān)于Python?Tkinter?復(fù)選框?->Checkbutton_python
- 2022-04-18 使用numpy對數(shù)組求平均時如何忽略nan值_python
- 2023-03-22 python實(shí)現(xiàn)四舍五入方式_python
- 2022-04-19 Python使用描述器實(shí)現(xiàn)ORM模型的方法詳解_python
- 2023-08-16 uniapp中v-model數(shù)據(jù)無法讀取問題 failed for prop “value“
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支