Flutter实现心动的动画特效
作者:艾维码 发布时间:2021-09-03 18:08:01
标签:Flutter,动画,特效
为了追求更好的用户体验,有时候我们需要一个类似心跳一样跳动着的控件来吸引用户的注意力,这是一个小小的优化需求,但是在 Flutter 里动画两件套就像裹脚布一样臭长,所以需要像封装一个 AnimatedWidget,解放生产力。
实现动画
混入 SingleTickerProviderStateMixin
当创建一个 AnimationController 时,需要传递一个vsync
参数,存在vsync
时会防止动画的UI不在当前屏幕时消耗不必要的资源。 通过混入 SingleTickerProviderStateMixin 。
class _MyHomePageState extends State<MyHomePage> with SingleTickerProviderStateMixin{}
创建动画
创建一个间隔将近一秒钟的动画控制器:
late final AnimationController animController;
@override
void initState() {
super.initState();
animController = AnimationController(
duration: const Duration(milliseconds: 800),
vsync: this,
);
}
心跳动画是从小变大,再变小,所以需要一个值大小变化的动画:
late final Animation<double> animation;
@override
void initState() {
super.initState();
animController = AnimationController(
duration: const Duration(milliseconds: 800),
vsync: this,
);
animation = Tween<double>(
begin: 0.9,
end: 1.05,
);
}
心跳是不间断的,所以需要监听动画完成时恢复动画,再继续开始动画:
animation = Tween<double>(
begin: 0.9,
end: 1.05,
).animate(animController)
..addListener(() {
setState(() {});
})
..addStatusListener((status) {
if (status == AnimationStatus.completed) {
animController.reverse();
} else if (status == AnimationStatus.dismissed) {
animController.forward();
}
});
使用缩放控件:
Transform.scale(
scale: animation.value,
child: const FlutterLogo(
size: 80,
),
),
为了跳动效果,突出跳动动画,把缩回去的时间改短:
animController = AnimationController(
reverseDuration: const Duration(milliseconds: 700),
duration: const Duration(milliseconds: 800),
vsync: this,
);
最后别忘了释放资源:
@override
void dispose() {
animController.dispose();
super.dispose();
}
抽离成小组件
为了每次用到类似的动画只需引入即可,需要分离动画和显示的组件。新建一个BounceWidget
,包含动画,然后可以传入UI组件:
class BounceWidget extends StatefulWidget {
final Widget child;
const BounceWidget({
Key? key,
required this.child,
}) : super(key: key);
@override
State<BounceWidget> createState() => _BounceWidgetState();
}
继续实现动画:
class _BounceWidgetState extends State<BounceWidget>
with SingleTickerProviderStateMixin {
late Animation<double> animation;
late AnimationController animController;
@override
void initState() {
super.initState();
animController = AnimationController(
reverseDuration: const Duration(milliseconds: 700),
duration: const Duration(milliseconds: 800),
vsync: this,
);
animation = Tween<double>(
begin: 0.9,
end: 1.05,
).animate(animController)
..addListener(() {
setState(() {});
})
..addStatusListener((status) {
if (status == AnimationStatus.completed) {
animController.reverse();
} else if (status == AnimationStatus.dismissed) {
animController.forward();
}
});
animController.forward();
}
@override
Widget build(BuildContext context) {
return Transform.scale(
scale: animation.value,
child: widget.child,
);
}
@override
void dispose() {
animController.dispose();
super.dispose();
}
}
去引入动画:
Center(
child: BounceWidget(
child: FlutterLogo(
size: 80,
),
),
完整代码
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key, required this.title}) : super(key: key);
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Padding(
padding: const EdgeInsets.only(top: 80, left: 16),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: const <Widget>[
Text(
"心动的",
style: TextStyle(
fontSize: 28,
color: Colors.black,
),
),
Text(
"感觉",
style: TextStyle(
fontSize: 48,
color: Colors.black,
),
),
Center(
child: BounceWidget(
child: FlutterLogo(
size: 80,
),
),
),
],
),
),
);
}
}
来源:https://juejin.cn/post/7083418319159722020


猜你喜欢
- Java 方法执行时的动态分派和静态分派是 Java 实现多态的本质背景Java 的动态分派和静态分派也是 Java 方法的执行原理。 Ja
- 通过主菜单对各级子菜单进行控制,并实现添加记录,查找记录,删除记录,修改记录,排序记录,以及退出系统功能的实现。一共六部分的功能模块。 上面
- 本文实例为大家分享了C#绘制饼状图和柱状图的具体代码,供大家参考,具体内容如下#代码如下:using System;using System
- vue3新增effectScope相关的API其官方的描述是创建一个 effect 作用域,可以捕获其中所创建的响应式副作用 (即计算属性和
- 之前碰到个问题,使用webview的时候无法定位,最近19大没法墙,只能去百度逛逛,发现有人说要这么做 WebSe
- 一.使用场景一次请求需要往数据库插入多条数据时,可以节省大量时间,mysql操作在连接和断开时的开销超过本次操作总开销的40%。二.实现方法
- 系列目录 【已更新最新开发文章,点击查看详细】类似于以下场景,将表单中的用户信息(包含附件)上传到服务器并保存到数据库中,<form
- 前言本文的多租户是基于多数据库进行实现的,数据是通过不同数据库进行隔离。下面话不多说,来看看详细的介绍:MyCat 基本配置首先针对多租户配
- 本文实例为大家分享了tryAcquire()、addWaiter()、acquireQueued()的用法 ,供大家参考,具体内容如下try
- 服务器端我们用软件模拟,是一个很小巧的软件,下载软件NetAssist:http://xiazai.jb51.net/201403/tool
- 父类空间优先于子类对象产生在每次创建子类对象时,先初始化父类空间,再创建其子类对象本身。目的在于子类对象中包含了其对应的父类空间,便可以包含
- C++中的动态数组(Dynamic Array)是指动态分配的、可以根据需求动态增长占用内存的数组。为了实现一个动态数组类的封装,我们需要考
- 作者:冰封一夏出处:http://www.cnblogs.com/bfyx/HZHControls官网:http://www.hzhcont
- 实践过程效果代码public partial class Frm_Libretto : Form{ public
- 如图所示为程序效果动画图地图滚动的原理在本人之前博客的文章中介绍过人物在屏幕中的移动方式,因为之前拼的游戏地图是完全填充整个手机屏幕的,所以
- selenium 中如何处理弹出窗口阅读目录原理测试页面的HTMLJava 代码原理在代码里, 通过 &n
- 1. 是否需要整合 ?不需要 : 单独使用Springmvc. 需要将原先Spring中的内容通通迁移到Springmvc中. 例如:数据源
- 本文实例为大家分享了C#实现飞行棋的具体代码,供大家参考,具体内容如下基于Winform框架写的不足之处请大佬指教using System;
- SpringAOP获取方法参数上的注解一、示例① 如下代码,自定义一个参数注解@Test,并将其使用到方法参数上,用于标注需要检验的参数/*
- 一、前端搭建1、前端用到js:uploadify(下载地址:http://www.uploadify.com/download/)、laye