<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
專案中遇到混合動畫的情況,每次實現都需要生命一堆屬性,讓程式碼變得雜亂,難以維護。
參考 iOS 組動畫 CAAimationGroup, 隨花半天時間封裝一個混合動畫元件 AnimatedGroup。
此元件基於極簡、高擴充套件、高適用的封裝原則,基本滿足當前專案開發。
// // AnimatedGroupDemo.dart // flutter_templet_project // // Created by shang on 12/6/21 5:52 PM. // Copyright © 12/6/21 shang. All rights reserved. // import 'package:flutter/material.dart'; import 'package:flutter_templet_project/basicWidget/animated_group.dart'; class AnimatedGroupDemo extends StatefulWidget { AnimatedGroupDemo({ Key? key, this.title}) : super(key: key); final String? title; @override _AnimatedGroupDemoState createState() => _AnimatedGroupDemoState(); } class _AnimatedGroupDemoState extends State<AnimatedGroupDemo> { GlobalKey<AnimatedGroupState> _globalKey = GlobalKey(); final _animations = <AnimatedGroupItemModel>[ AnimatedGroupItemModel( tween: Tween<double>(begin: .0, end: 300.0,), begin: 0.0, end: 0.6 ), AnimatedGroupItemModel( tween: ColorTween(begin: Colors.green, end: Colors.red,), begin: 0.0, end: 0.6 ), AnimatedGroupItemModel( tween: Tween<EdgeInsets>( begin: const EdgeInsets.only(left: .0), end: const EdgeInsets.only(left: 100.0), ), begin: 0.6, end: 1.0 ), ]; @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text(widget.title ?? "$widget"), ), body: Center( child: Column( children: [ ElevatedButton( child: Text("start animation"), onPressed: (){ _globalKey.currentState?.palyeAnimations(isRemovedOnCompletion: false); }, ), Container( width: 300, height: 300, child: AnimatedGroup( key: _globalKey, duration: Duration(milliseconds: 2000), animations: _animations, child: Text("AnimatedGroupWidget 混合動畫", style: TextStyle(color: Colors.white, backgroundColor: Colors.green),), builder: (BuildContext context, Widget? child, List<Animation<dynamic>> animations) { final aHeight = animations[0]; final aColor = animations[1]; final aPadding = animations[2]; return Stack( children: [ Container( alignment: Alignment.bottomCenter, padding: aPadding.value, child: Container( color: aColor.value, width: 50.0, height: aHeight.value, ), ), Center(child: child!) ], ); }, ), decoration: BoxDecoration( color: Colors.black.withOpacity(0.1), border: Border.all( color: Colors.black.withOpacity(0.5), ) ), ) ], ), ), ); } }
// // AnimatedGroupDemo.dart // flutter_templet_project // // Created by shang on 1/19/23 8:21 AM. // Copyright © 1/19/23 shang. All rights reserved. // import 'package:flutter/material.dart'; /// 混合動畫回撥型別 typedef AnimatedGroupBuilder = Widget Function(BuildContext context, Widget? child, List<Animation<dynamic>> animations); class AnimatedGroup extends StatefulWidget { /// 混合動畫 AnimatedGroup({ Key? key, required this.animations, required this.builder, this.controller, this.duration = const Duration(milliseconds: 2000), this.child, }) : super(key: key); /// 混合動畫陣列 List<AnimatedGroupItemModel> animations; /// 混合動畫回撥 AnimatedGroupBuilder builder; /// 控制器 AnimationController? controller; /// AnimationController 控制的 duration 屬性 Duration? duration; /// 不需要多次構建的部分 Widget? child; @override AnimatedGroupState createState() => AnimatedGroupState(); } /// 混合動畫 State class AnimatedGroupState extends State<AnimatedGroup> with TickerProviderStateMixin { AnimationController? _controller; /// 僅限於無法滿足功能時使用(透傳物件, 方便二次開發) AnimationController get controller => _controller!; List<Animation<dynamic>> _animations = []; @override void initState() { _controller = widget.controller ?? AnimationController(duration: widget.duration, vsync: this); _animations = widget.animations.map((e) => e.tween.animate(_buildAnim(e.begin, e.end))).toList(); super.initState(); } @override void dispose() { _controller?.dispose(); super.dispose(); } @override Widget build(BuildContext context) { return AnimatedBuilder( animation: _controller!, child: widget.child, builder: (BuildContext context, Widget? child){ return widget.builder(context, child, _animations); } ); } /// 開始執行動畫 /// /// isRemovedOnCompletion 是否單程動畫 /// /// isReverse 是否逆轉動畫 palyeAnimations({bool isRemovedOnCompletion = true, bool isReverse = false}) async { try { if (!isReverse) { await _controller?.forward().orCancel; if (!isRemovedOnCompletion) { await _controller?.reverse().orCancel; } } else { await _controller?.reverse().orCancel; if (!isRemovedOnCompletion) { await _controller?.forward().orCancel; } } } on TickerCanceled { // the animation got canceled, probably because we were disposed }; } /// 建立動畫物件 CurvedAnimation _buildAnim(double begin, double end) { return CurvedAnimation( parent: _controller!, curve: Interval( begin, end, curve: Curves.ease, ), ); } } /// 混合動畫單個動畫模型 class AnimatedGroupItemModel{ /// 混合動畫單個動畫模型 AnimatedGroupItemModel({ required this.tween, required this.begin, required this.end, }); /// 動畫 Tween Tween tween; /// 動畫開始時間 (0 - 1.0) double begin; /// 動畫結束時間 (0 - 1.0) double end; }
程式碼複製到專案中可直接執行;
以上就是Flutter封裝組動畫混合動畫AnimatedGroup範例詳解的詳細內容,更多關於Flutter封裝AnimatedGroup的資料請關注it145.com其它相關文章!
相關文章
<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
综合看Anker超能充系列的性价比很高,并且与不仅和iPhone12/苹果<em>Mac</em>Book很配,而且适合多设备充电需求的日常使用或差旅场景,不管是安卓还是Switch同样也能用得上它,希望这次分享能给准备购入充电器的小伙伴们有所
2021-06-01 09:31:42
除了L4WUDU与吴亦凡已经多次共事,成为了明面上的厂牌成员,吴亦凡还曾带领20XXCLUB全队参加2020年的一场音乐节,这也是20XXCLUB首次全员合照,王嗣尧Turbo、陈彦希Regi、<em>Mac</em> Ova Seas、林渝植等人全部出场。然而让
2021-06-01 09:31:34
目前应用IPFS的机构:1 谷歌<em>浏览器</em>支持IPFS分布式协议 2 万维网 (历史档案博物馆)数据库 3 火狐<em>浏览器</em>支持 IPFS分布式协议 4 EOS 等数字货币数据存储 5 美国国会图书馆,历史资料永久保存在 IPFS 6 加
2021-06-01 09:31:24
开拓者的车机是兼容苹果和<em>安卓</em>,虽然我不怎么用,但确实兼顾了我家人的很多需求:副驾的门板还配有解锁开关,有的时候老婆开车,下车的时候偶尔会忘记解锁,我在副驾驶可以自己开门:第二排设计很好,不仅配置了一个很大的
2021-06-01 09:30:48
不仅是<em>安卓</em>手机,苹果手机的降价力度也是前所未有了,iPhone12也“跳水价”了,发布价是6799元,如今已经跌至5308元,降价幅度超过1400元,最新定价确认了。iPhone12是苹果首款5G手机,同时也是全球首款5nm芯片的智能机,它
2021-06-01 09:30:45