【Flutter】Animation 动画 ( AnimatedBuilder 动画使用流程 | 创建动画控制器 | 创建动画 | 创建动画作用的组件 | 关联动画与组件 | 动画执行 )
文章目錄
- ?、AnimatedBuilder 引入
- 一、創(chuàng)建動(dòng)畫控制器
- 二、創(chuàng)建動(dòng)畫
- 三、創(chuàng)建動(dòng)畫作用的組件
- 四、創(chuàng)建 AnimatedBuilder 關(guān)聯(lián)動(dòng)畫與組件
- 五、動(dòng)畫運(yùn)行
- 六、完整代碼示例
- 七、相關(guān)資源
AnimatedBuilder 動(dòng)畫使用流程 :
① 創(chuàng)建動(dòng)畫控制器
② 創(chuàng)建動(dòng)畫
③ 創(chuàng)建動(dòng)畫作用的組件
④ 創(chuàng)建 AnimatedBuilder 關(guān)聯(lián)動(dòng)畫與組件
⑤ 執(zhí)行動(dòng)畫
?、AnimatedBuilder 引入
在上一篇博客 【Flutter】Animation 動(dòng)畫 ( AnimatedWidget 動(dòng)畫使用流程 | 創(chuàng)建動(dòng)畫控制器 | 創(chuàng)建動(dòng)畫 | 創(chuàng)建 AnimatedWidget 動(dòng)畫組件 | 動(dòng)畫運(yùn)行 ) 中 , 使用了 AnimatedWidget 組件實(shí)現(xiàn)動(dòng)畫 , 省略了手動(dòng)添加監(jiān)聽器 , 并在監(jiān)聽器中手動(dòng)調(diào)用 setState 更新動(dòng)畫的操作 ;
使用 AnimatedWidget 方法實(shí)現(xiàn)的動(dòng)畫 , 與 Widget 組件的耦合性還是很高 , 這里引入 AnimatedBuilder , 可以將 Animation 動(dòng)畫 和 Widget 組件分離 ;
AnimatedBuilder 可以構(gòu)建通用 Widget , AnimatedBuilder 可以用于拆分動(dòng)畫 與 組件 ;
動(dòng)畫開發(fā)中需要分離的功能 :
- 顯示動(dòng)畫作用的組件
- 定義 Animation 動(dòng)畫對(duì)象
- 將 Animation 渲染到組件上
AnimatedBuilder 在監(jiān)聽機(jī)制上與 AnimatedWidget 類似 , 也是自動(dòng)添加監(jiān)聽器 , 監(jiān)聽動(dòng)畫的執(zhí)行過程 , 自動(dòng)調(diào)用 setState 方法更新界面 ;
一、創(chuàng)建動(dòng)畫控制器
AnimationController 構(gòu)造函數(shù)參數(shù)說明 :
AnimationController({double? value, /// 動(dòng)畫的初始值Duration? duration, /// 動(dòng)畫正向播放持續(xù)時(shí)間Duration? reverseDuration, /// 動(dòng)畫逆序播放持續(xù)時(shí)間String? debugLabel, /// 調(diào)試期間標(biāo)識(shí)動(dòng)畫的標(biāo)志double lowerBound: 0.0, /// 動(dòng)畫最小值double upperBound: 1.0, /// 動(dòng)畫最大值 AnimationBehavior animationBehavior: AnimationBehavior.normal,/// 上下文的 TickerProvider , 用于防止屏幕外的動(dòng)畫消耗不必要的資源 , /// 一般將 StatefulWidget 作為 vsync 值required TickerProvider vsync} )上述參數(shù)中 , 只需要設(shè)置 required TickerProvider vsync 參數(shù) 與 Duration? duration 參數(shù)即可 ;
創(chuàng)建動(dòng)畫控制器代碼示例 :
/// 1. 初始化動(dòng)畫控制器animationController = AnimationController(// 動(dòng)畫繪制到屏幕外部時(shí), 減少消耗vsync: this,// 動(dòng)畫持續(xù)時(shí)間 2 秒duration: Duration(seconds: 3),);二、創(chuàng)建動(dòng)畫
這里創(chuàng)建 Tween 補(bǔ)間動(dòng)畫 , 設(shè)置動(dòng)畫的初始值 000 , 結(jié)束值 300300300 , 動(dòng)畫在執(zhí)行的 333 秒時(shí)間內(nèi) ( 動(dòng)畫控制器中指定的動(dòng)畫持續(xù)時(shí)間 ) , 自動(dòng)計(jì)算出每個(gè)時(shí)間點(diǎn)的 000 ~ 300300300 之間的動(dòng)畫值 ;
創(chuàng)建動(dòng)畫代碼示例 :
/// 2 . 構(gòu)造 Tween 補(bǔ)間動(dòng)畫 ,/// 設(shè)置動(dòng)畫控制器 AnimationController 給該補(bǔ)間動(dòng)畫/// 動(dòng)畫的值是正方形組件的寬高animation = Tween<double>(begin: 0,end: 300).animate(animationController)三、創(chuàng)建動(dòng)畫作用的組件
創(chuàng)建一個(gè)純無狀態(tài)組件 StatelessWidget , 該組件與 Animation 動(dòng)畫對(duì)象各自都是獨(dú)立的 , 使用 AnimatedBuilder 耦合 動(dòng)畫 與 組件 ;
代碼示例 :
/// 3 . 定義純組件, 動(dòng)畫應(yīng)用與該組件上 class AnimationWidget extends StatelessWidget{@overrideWidget build(BuildContext context) {return // 動(dòng)畫的主體組件// 布局組件中使用動(dòng)畫的值 , 以達(dá)到動(dòng)畫效果Container(decoration: BoxDecoration(color: Colors.red),);} }四、創(chuàng)建 AnimatedBuilder 關(guān)聯(lián)動(dòng)畫與組件
創(chuàng)建 AnimatedBuilder , 關(guān)聯(lián)動(dòng)畫與組件 ;
首先要把 AnimatedBuilder , Animation 動(dòng)畫 , Widget 組件 , 都封裝在一個(gè) StatelessWidget 組件中 , Flutter 中一切皆組件 ;
然后在這個(gè)組件中返回一個(gè)包含 AnimatedBuilder 組件的組件 , 其中將 Animation 動(dòng)畫 和 Widget 組件都設(shè)置在該 AnimatedBuilder 中 , Animation 動(dòng)畫設(shè)置在 animation 字段中 , child 字段需要設(shè)置到 build 字段中 , 設(shè)置的方法如下 :
AnimatedBuilder(animation: animation,builder: (context, child) => Container(height: animation.value,width: animation.value,child: child,),child: child,)代碼示例 :
/// 4 . 將組件與動(dòng)畫結(jié)合起來 class AnimationTransition extends StatelessWidget{/// 構(gòu)造方法AnimationTransition({this.child, this.animation});/// 動(dòng)畫作用的組件final Widget child;/// 動(dòng)畫final Animation<double> animation;@overrideWidget build(BuildContext context) {/// AnimatedBuilder 會(huì)自動(dòng)監(jiān)聽 animation 的變化/// 然后渲染 child 組件上的動(dòng)畫return Column(children: [Text("動(dòng)畫狀態(tài) : ${animation.status}", textDirection: TextDirection.ltr,),Text("動(dòng)畫值 : ${animation.value?.round()}", textDirection: TextDirection.ltr,),Container(height: 50,),AnimatedBuilder(animation: animation,builder: (context, child) => Container(height: animation.value,width: animation.value,child: child,),child: child,)],);} }五、動(dòng)畫運(yùn)行
監(jiān)聽 GestureDetector 的 onTap 點(diǎn)擊事件 , 點(diǎn)擊該組件后 , 調(diào)用 animationController.forward() 方法 , 運(yùn)行動(dòng)畫 ;
代碼示例 :
GestureDetector(// 5 . 點(diǎn)擊按鈕開啟動(dòng)畫onTap: (){/// 按鈕點(diǎn)擊事件/// 首先將動(dòng)畫初始化animationController.reset();/// 正向執(zhí)行動(dòng)畫, 即從初始值執(zhí)行到結(jié)束值animationController.forward();},child: Container(alignment: Alignment.center,color: Colors.green,height: 50,child: Text(// 顯示文本"動(dòng)畫開始",/// 文字方向 : 從左到右textDirection: TextDirection.ltr,),),),六、完整代碼示例
完整代碼示例 :
import 'package:flutter/material.dart';void main() {runApp(AnimationApp()); }/// 3 . 定義純組件, 動(dòng)畫應(yīng)用與該組件上 class AnimationWidget extends StatelessWidget{@overrideWidget build(BuildContext context) {return // 動(dòng)畫的主體組件// 布局組件中使用動(dòng)畫的值 , 以達(dá)到動(dòng)畫效果Container(decoration: BoxDecoration(color: Colors.red),);} }/// 4 . 將組件與動(dòng)畫結(jié)合起來 class AnimationTransition extends StatelessWidget{/// 構(gòu)造方法AnimationTransition({this.child, this.animation});/// 動(dòng)畫作用的組件final Widget child;/// 動(dòng)畫final Animation<double> animation;@overrideWidget build(BuildContext context) {/// AnimatedBuilder 會(huì)自動(dòng)監(jiān)聽 animation 的變化/// 然后渲染 child 組件上的動(dòng)畫return Column(children: [Text("動(dòng)畫狀態(tài) : ${animation.status}", textDirection: TextDirection.ltr,),Text("動(dòng)畫值 : ${animation.value?.round()}", textDirection: TextDirection.ltr,),Container(height: 50,),AnimatedBuilder(animation: animation,builder: (context, child) => Container(height: animation.value,width: animation.value,child: child,),child: child,)],);} }/// 動(dòng)畫示例主界面組件 /// 該組件是有狀態(tài)的, 因此需要定義 StatefulWidget 組件 class AnimationApp extends StatefulWidget{@override_AnimationAppState createState() => _AnimationAppState(); }/// 為 StatefulWidget 組件創(chuàng)建 State 類 /// 每個(gè) StatefulWidget 都需要一個(gè)配套的 State 類 class _AnimationAppState extends State<AnimationApp>with SingleTickerProviderStateMixin{/// 動(dòng)畫類Animation<double> animation;/// 動(dòng)畫控制器AnimationController animationController;@overridevoid initState() {super.initState();/// 1. 初始化動(dòng)畫控制器animationController = AnimationController(// 動(dòng)畫繪制到屏幕外部時(shí), 減少消耗vsync: this,// 動(dòng)畫持續(xù)時(shí)間 2 秒duration: Duration(seconds: 3),);/// 2 . 構(gòu)造 Tween 補(bǔ)間動(dòng)畫 ,/// 設(shè)置動(dòng)畫控制器 AnimationController 給該補(bǔ)間動(dòng)畫/// 動(dòng)畫的值是正方形組件的寬高animation = Tween<double>(begin: 0,end: 300).animate(animationController);}/// 該方法與 initState 對(duì)應(yīng)@overridevoid dispose() {/// 釋放動(dòng)畫控制器animationController.dispose();super.dispose();}@overrideWidget build(BuildContext context) {returnColumn(children: [Container(height: 50,),GestureDetector(// 5 . 點(diǎn)擊按鈕開啟動(dòng)畫onTap: (){/// 按鈕點(diǎn)擊事件/// 首先將動(dòng)畫初始化animationController.reset();/// 正向執(zhí)行動(dòng)畫, 即從初始值執(zhí)行到結(jié)束值animationController.forward();},child: Container(alignment: Alignment.center,color: Colors.green,height: 50,child: Text(// 顯示文本"動(dòng)畫開始",/// 文字方向 : 從左到右textDirection: TextDirection.ltr,),),),Container(height: 50,),AnimationTransition(animation: animation, child: AnimationWidget())],);}}運(yùn)行效果 : 動(dòng)畫值不能更新 ;
七、相關(guān)資源
參考資料 :
- Flutter 官網(wǎng) : https://flutter.dev/
- Flutter 插件下載地址 : https://pub.dev/packages
- Flutter 開發(fā)文檔 : https://flutter.cn/docs ( 強(qiáng)烈推薦 )
- 官方 GitHub 地址 : https://github.com/flutter
- Flutter 中文社區(qū) : https://flutter.cn/
- Flutter 實(shí)用教程 : https://flutter.cn/docs/cookbook
- Flutter CodeLab : https://codelabs.flutter-io.cn/
- Dart 中文文檔 : https://dart.cn/
- Dart 開發(fā)者官網(wǎng) : https://api.dart.dev/
- Flutter 中文網(wǎng) : https://flutterchina.club/ , http://flutter.axuer.com/docs/
- Flutter 相關(guān)問題 : https://flutterchina.club/faq/ ( 入門階段推薦看一遍 )
- GitHub 上的 Flutter 開源示例 : https://download.csdn.net/download/han1202012/15989510
- Flutter 實(shí)戰(zhàn)電子書 : https://book.flutterchina.club/chapter1/
重要的專題 :
- Flutter 動(dòng)畫參考文檔 : https://flutterchina.club/animations/
博客源碼下載 :
-
GitHub 地址 : https://github.com/han1202012/flutter_animation ( 隨博客進(jìn)度一直更新 , 有可能沒有本博客的源碼 )
-
博客源碼快照 : https://download.csdn.net/download/han1202012/16188742 ( 本篇博客的源碼快照 , 可以找到本博客的源碼 )
總結(jié)
以上是生活随笔為你收集整理的【Flutter】Animation 动画 ( AnimatedBuilder 动画使用流程 | 创建动画控制器 | 创建动画 | 创建动画作用的组件 | 关联动画与组件 | 动画执行 )的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【Flutter】Animation 动
- 下一篇: 【错误记录】Flutter 界面报错 (