当前位置:网站首页>Flutter record learning different animation (2)
Flutter record learning different animation (2)
2022-07-30 03:44:00 【Mr-dream】
一、AnimatedWidget 简介
AnimatedWidget Base class can distinguish core from the animation code widget 代码. AnimatedWidget 不需要保持 State 对象来 hold 动画.AnimatedWidget 帮助类(代替 addListener() 和 setState())创建动画 widget.
利用 AnimatedWidget Create an animation can run the reuse widget.
Flutter API 中的 AnimatedWidget:AnimatedBuilder, RotationTransition, ScaleTransition, SizeTransition, SlideTransition.
二、AnimatedWidget 动画示例
From a rectangular wide high shrinkage
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 Know how to render the transition effect
但 AnimatedBuilder 不会渲染 widget,Will not control the animation object.
使用 AnimatedBuilder Describe an animation is other widget Part of the build method.If only need to use reusable animation defines a widget.
Flutter API 中 AnimatedBuilders:BottomSheet, ExpansionTile, PopupMenu, ProgressIndicator, RefreshIndicator, Scaffold, SnackBar, TabBar, TextField.
AnimatedWidget The sample code there is a problem,Is changed animation need to render logo 的widget.Is a better solution,Task is divided into different classes:
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,
);
}
}
四、总结
从上面代码可以看出widgetAnd animation effects apart,所以AnimatedBuilder进一步将AnimatedWidget 进行分离,It can make the animation effect for reuse,非常不错的.
边栏推荐
- LeetCode 114. 二叉树展开为链表(一题三吃)
- 智能答题功能,CRMEB知识付费系统必须有!
- 数据湖:数据集成工具DataX
- Starlight does not ask passers-by!The young lady on the Wuhan campus successfully switched to software testing in three months and received a salary of 9k+13!
- 【无标题】
- Nacos 安装与部署
- Hystrix service circuit breaker
- Answer these 3 interview questions correctly, and the salary will go up by 20K
- 淘宝/天猫获得淘宝店铺详情 API
- HCIP OSPF
猜你喜欢

小程序毕设作品之微信二手交易小程序毕业设计成品(5)任务书

Gateway routing gateway

Problems caused by List getting the difference

What is the difference between mission, vision and values?

CDH/CDP 是什么?

Software testing interview questions and answer analysis, the strongest version in 2022

【GPU并行计算】利用OpenCL&OpenCLUtilty进行GPU并行计算

【无标题】

Awesome, Tencent technical experts handed Redis technical notes, and the download volume has exceeded 30W

小程序毕设作品之微信积分商城小程序毕业设计成品(3)后台功能
随机推荐
Open address method hash implementation - linear detection method
【SQL】按某个关联列用一张表的数据更新另一张表
Process priority nice
OpenFeign实现负载均衡
小程序毕设作品之微信积分商城小程序毕业设计成品(5)任务书
小程序毕设作品之微信二手交易小程序毕业设计成品(5)任务书
Nacos配置中心
Overview of Federated Learning (2) - Classification, Framework and Future Research Directions of Federated Learning
淘宝/天猫获得淘宝店铺详情 API
sublime text 3 设置
Nacos命名空间
【无标题】
Nacos实现高可用
星光不问赶路人!武汉校区小姐姐三个月成功转行软件测试,收获9k+13薪!
Testers, what else do you need to know besides testing?
Organizations Going Online: A New Trend in Organizational Digital Transformation
如何有效进行回顾会议(上)?
Is the snowflake the same question?
Sentinel 流量防卫兵
TCP congestion control technology and acceleration principle of BBR