当前位置:网站首页>flutter 记录学习不一样的动画(二)
flutter 记录学习不一样的动画(二)
2022-07-30 03:40:00 【Mr-dream】
一、AnimatedWidget 简介
AnimatedWidget 基本类可以从动画代码中区分出核心 widget 代码。 AnimatedWidget 不需要保持 State 对象来 hold 动画。AnimatedWidget 帮助类(代替 addListener() 和 setState())创建动画 widget。
利用 AnimatedWidget 创建一个可以运行重复使用动画的 widget。
Flutter API 中的 AnimatedWidget:AnimatedBuilder, RotationTransition, ScaleTransition, SizeTransition, SlideTransition。
二、AnimatedWidget 动画示例
一个从矩形的宽高收缩
class MyAnimationWidget extends StatefulWidget {
const MyAnimationWidget({
Key? key}) : super(key: key);
@override
_MyAnimationWidgetState createState() => _MyAnimationWidgetState();
}
class _MyAnimationWidgetState extends State<MyAnimationWidget>
with SingleTickerProviderStateMixin {
late AnimationController controller;
late Animation<double> animation;
@override
initState() {
super.initState();
controller = AnimationController(
duration: const Duration(milliseconds: 2000), vsync: this);
animation = Tween(begin: 0.0, end: 300.0).animate(controller);
controller.forward();
}
@override
void dispose() {
controller.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: AnimationLogo(animation),
),
);
}
}
class AnimationLogo extends AnimatedWidget {
const AnimatedLogo({
super.key, required Animation<double> animation})
: super(listenable: animation);
@override
Widget build(BuildContext context) {
final animation = listenable as Animation<double>;
return Center(
child: Container(
height: animation.value,
width: animation.value,
color: Colors.red,
),
);
}
}
三、使用 AnimatedBuilder 进行重构
AnimatedBuilder 知道如何渲染过渡效果
但 AnimatedBuilder 不会渲染 widget,也不会控制动画对象。
使用 AnimatedBuilder 描述一个动画是其他 widget 构建方法的一部分。如果只是单纯需要用可重复使用的动画定义一个 widget。
Flutter API 中 AnimatedBuilders:BottomSheet, ExpansionTile, PopupMenu, ProgressIndicator, RefreshIndicator, Scaffold, SnackBar, TabBar, TextField。
AnimatedWidget 示例代码中有个问题,就是改变动画需要改变渲染 logo 的widget。较好的解决办法是,将任务区分到不同类里:
a. 渲染 logo
b. 定义动画对象
c. 渲染过渡效果
AnimatedBuilder 动画示例
class _MyAnimationWidgetState extends State<MyAnimationWidget>
with SingleTickerProviderStateMixin {
late AnimationController controller;
late Animation<double> animation;
@override
initState() {
super.initState();
controller = AnimationController(
duration: const Duration(milliseconds: 2000), vsync: this);
animation = Tween(begin: 0.0, end: 300.0).animate(controller);
controller.forward();
}
@override
void dispose() {
controller.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: AnimationLogo(
animation,
child: LogoWidget(),
),
),
);
}
}
class LogoWidget extends StatelessWidget {
const LogoWidget({
Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Center(
child: Container(
color: Colors.red,
),
);
}
}
class AnimationLogo extends StatelessWidget {
final Animation<double> animation;
final Widget child;
AnimationLogo(this.animation, {
Key? key, required this.child});
@override
Widget build(BuildContext context) {
return AnimatedBuilder(
animation: animation,
builder: (BuildContext context, Widget? child) {
return SizedBox(
height: animation.value,
width: animation.value,
child: child,
);
},
child: child,
);
}
}
四、总结
从上面代码可以看出widget和动画效果分开了,所以AnimatedBuilder进一步将AnimatedWidget 进行分离,这样就可以让动画效果进行复用了,非常不错的。
边栏推荐
猜你喜欢
Answer these 3 interview questions correctly, and the salary will go up by 20K
Awesome, Tencent technical experts handed Redis technical notes, and the download volume has exceeded 30W
星光不问赶路人!武汉校区小姐姐三个月成功转行软件测试,收获9k+13薪!
Three years of experience will only be a little bit (functional testing), and you may not even be able to find a job after resigning.
联邦学习综述(一)——联邦学习的背景、定义及价值
开放地址法哈希实现——线性探测法
基于全志D1-H和XR806的名贵植物监控装置
如何有效进行回顾会议(上)?
Gateway routing gateway
First acquaintance with the web
随机推荐
CMake的安装和测试
【Node访问MongoDB数据库】
小程序毕设作品之微信积分商城小程序毕业设计成品(2)小程序功能
FreeRTOS个人笔记-内存管理
Nacos Configuration Center
Are you still using the command line to read logs?Quickly use Kibana, visual log analysis YYDS
HCIP OSPF
Awesome, Tencent technical experts handed Redis technical notes, and the download volume has exceeded 30W
组织在线化:组织数字化变革的新趋势
精品:淘宝/天猫获取购买到的商品订单详情 API
小程序毕设作品之微信二手交易小程序毕业设计成品(2)小程序功能
状态空间表示
AI智能安防视频平台EasyCVR如何配置音视频同时传输?
LoadBalancer load balancing
Testers, what else do you need to know besides testing?
Nacos service registration and discovery
小程序毕设作品之微信二手交易小程序毕业设计成品(8)毕业设计论文模板
联邦学习综述(一)——联邦学习的背景、定义及价值
JUC (5): Problems caused by sharing
记录NLP各种资源网址