当前位置:网站首页>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,非常不错的.
边栏推荐
- Mini Program Graduation Works WeChat Second-hand Trading Mini Program Graduation Design Finished Work (2) Mini Program Function
- 微服务进阶 Cloud Alibaba
- SQLSERVER将子查询数据合并拼接成一个字段
- 第51篇-知乎请求头参数分析【2022-07-28】
- 北京bgp机房和普通机房的区别
- phpoffice 编辑excel文档
- 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!
- flutter 记录学习不一样的动画(二)
- 小程序毕设作品之微信二手交易小程序毕业设计成品(7)中期检查报告
- List获取差集产生的问题
猜你喜欢

小程序毕设作品之微信积分商城小程序毕业设计成品(3)后台功能

WPF引入 ttf 图标文件使用记录

论坛管理系统

【Use of scientific research tools】A

小程序毕设作品之微信积分商城小程序毕业设计成品(6)开题答辩PPT

Record NLP various resource URLs

CMake的安装和测试

Mini Program Graduation Works WeChat Second-hand Trading Mini Program Graduation Design Finished Work (2) Mini Program Function

【Node访问MongoDB数据库】

基于全志D1-H和XR806的名贵植物监控装置
随机推荐
护网行动基本介绍
OpenFeign realize load balance
MySQL之数据查询(分类汇总与排序)
AI智能安防视频平台EasyCVR如何配置音视频同时传输?
Nacos cluster partition
JIT VS AOT
sql中 exists的用法
小程序毕设作品之微信积分商城小程序毕业设计成品(7)中期检查报告
[Flink] How to determine the cluster planning size from development to production launch?
NLP自然语言处理(二)
Nacos配置中心
Monitor page deployment
Ansible introduction (detailed) features + advantages + design concept + application field + system architecture + working principle + task execution process
运行时间监控:如何确保网络设备运行时间
Overview of Federated Learning (1) - Background, Definition and Value of Federated Learning
【SQL】按某个关联列用一张表的数据更新另一张表
论坛管理系统
spicy(一)基本定义
对均匀采样信号进行重采样
List获取差集产生的问题