当前位置:网站首页>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 进行分离,这样就可以让动画效果进行复用了,非常不错的。
边栏推荐
- Hystrix 服务熔断
- JUC (four): five/six states of shorthand thread
- vscode 调试和远程
- 传输层详解
- Solve The problem of Google browser cross-domain has had been blocked by CORS policy: The request The client is not a secure context and The resou
- Excuse me, when datax is synchronized to the oceanbase database, it is written according to the primary key update method. How to fill in the content in the drop-down box?
- 联邦学习综述(一)——联邦学习的背景、定义及价值
- EasyNVR平台级联到EasyCVR,视频播放一会就无法播放是什么原因?
- 【C补充】整数与字符串的转换
- Overview of Federated Learning (2) - Classification, Framework and Future Research Directions of Federated Learning
猜你喜欢
随机推荐
星光不问赶路人!武汉校区小姐姐三个月成功转行软件测试,收获9k+13薪!
phpoffice 编辑excel文档
Stimulsoft ReportsJS and DashboardsJS. 2022.3.3
JIT VS AOT
Public chains challenging the "Impossible Triangle"
LoadBalancer 负载均衡
sublime text 3 设置
小程序毕设作品之微信积分商城小程序毕业设计成品(8)毕业设计论文模板
小程序毕设作品之微信积分商城小程序毕业设计成品(1)开发概要
微服务CAP原则
Nacos namespace
Nacos服务注册与发现
Open address method hash implementation - secondary detection method
Solve The problem of Google browser cross-domain has had been blocked by CORS policy: The request The client is not a secure context and The resou
gnss rtcm rtklib Ntrip...
小程序毕设作品之微信二手交易小程序毕业设计成品(7)中期检查报告
Testers, what else do you need to know besides testing?
Process priority nice
淘宝/天猫获取卖出的商品订单列表 API
Microservice CAP Principles









