Flutter實現不同縮放動畫效果詳解
需求背景
組件縮放可以向著一個方向進行縮放,放大列表中某一個Cell期望它是向后進行放大而非組件中心點開始縮放。具體效果如下圖所示:

可縮放組件介紹
ScaleTransition
ScaleTransition具體實現如下代碼,設置AnimationController控制器若需要增加數值操作可以再增加Animate再調用forward方法執(zhí)行。
PS:動畫實現在以前文章中有介紹過
動畫控制器
_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,
),
)
如圖所示默認縮放是以組件中心點進行縮放效果,設置alignment則向著相反位置進行縮放。

但ScaleTransition并不能滿足需求功能,無法做到向著一個方向進行縮放動畫。
SizeTransition
SizeTransition是以更改子組件尺寸實現動畫效果,支持垂直或水平方向動畫。
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"),
),
)

但在需求要求上還是不滿足期望的結果,SizeTransition更適用在實現展開或是飛入的動畫效果。
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的問題在于它只作用于自身,若子布局設置了自身的尺寸就不會隨著父組件大小而變化。
AnimatedBuilder
AnimatedBuilder主要結合Transform.scale組件設置alignment為Alignment.centerLeft即可對組件實現向右縮放動畫。
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方式實現縮放需要為組件縮放預留好足夠空間進行縮放放大操作,避免組件縮放后與其他組件出現重疊現象。
小結
實現縮放方式有多種但對于比較定制化縮放要求需求配合上Transform.scale才能達到最終效果。Transform.scale可以幫助動畫實現上對于組件尺寸大小控制方向有所幫助。因此采用AnimatedBuilder結合Transform.scale是需求實現最優(yōu)方案。
以上就是Flutter實現不同縮放動畫效果詳解的詳細內容,更多關于Flutter縮放動畫的資料請關注腳本之家其它相關文章!
相關文章
全面解析Android中對EditText輸入實現監(jiān)聽的方法
這篇文章主要介紹了Android中對EditText輸入實現監(jiān)聽的方法,包括一個仿iOS的帶清除功能的ClearEditText輸入框控件的詳細使用介紹,需要的朋友可以參考下2016-04-04
Android APK應用安裝原理解析之AndroidManifest使用PackageParser.parserPac
這篇文章主要介紹了Android APK應用安裝原理解析之AndroidManifest使用PackageParser.parserPackage原理,結合實例形式分析了PackageManagerService調用PackageParser.parserPackage方法解析APK清單相關原理與操作技巧,需要的朋友可以參考下2017-12-12

