Flutter animation 原生的StatefullWidget
添加 SingleTickerProviderStateMixin,
使用GetX的话 我个人是把animateionController写在GetXController,
GetX有提供SingleGetTickerProviderMixin.
下面直接看code
GetxController部分
animationController 设定整个动画在0.5秒.
接下来 设定了两个animation
class ExtensionPageController extends GetxController
with SingleGetTickerProviderMixin {
late AnimationController animationController;
late Animation<double> scaleAnimation;
late Animation<double> flipAnimation;
@override
void onInit() {
animationController =
AnimationController(vsync: this, duration: Duration(milliseconds: 500));
//小->大->小 变化的过程 (1 -> 2 > 3 -> 2 -> 1)
scaleAnimation = TweenSequence(<TweenSequenceItem<double>>[
TweenSequenceItem(tween: Tween(begin: 1.0, end: 2.0), weight: 25),
TweenSequenceItem(tween: Tween(begin: 2.0, end: 3.0), weight: 25),
TweenSequenceItem(tween: Tween(begin: 3.0, end: 2.0), weight: 25),
TweenSequenceItem(tween: Tween(begin: 2.0, end: 1.0), weight: 25),
]).animate(animationController);
flipAnimation = Tween(begin: 0.0, end: 2.0).animate(animationController);
super.onInit();
}
animationStart() {
animationController.forward(from: 0.0);
}
}
Widget部分
这边借用前篇多国语系切换下面还有的空间.
使用GetBuilder和AnimatedBuilder,
IconButton按下触发动画 呼叫controller的 animationStart()
AnimatedBuilder 回传 Transform,
scale 大小(带入上面写好的scaleAnimation),
rotateY水平翻转(flipAnimation),
Expanded(
flex: 4,
child: GetBuilder<ExtensionPageController>(
init: controller,
initState: (_) {},
tag: "imageFlip",
builder: (_) {
return AnimatedBuilder(
animation: controller.animationController,
builder: (context, child) {
return Transform(
child: child,
alignment: Alignment.center,
transform: Matrix4.identity()
..scale(controller.scaleAnimation.value,
controller.scaleAnimation.value, 1)
..rotateY(pi * controller.flipAnimation.value),
);
},
child: IconButton(
icon: Icon(Icons.ac_unit, size: 50),
onPressed: () => controller.animationStart(),
),
);
},
),
),
实际效果
下一篇将为大家介绍flutter Dio
>>: Day. 27 Binary Tree Level Order Traversal
前言 昨天有说到在储存使用者的密码时,不管是用 AES 把他们加密起来,或是经过 SHA1 杂凑之後...
自从上次在街边吃了一碗不怎麽地「杂碎面」,咖哩鱼蛋没鱼味,咖哩又不入味,失败!猪皮煮得太烂,没咬头,...
预设 先要有一个开发板,可以接各种sensor。 可以先跟电脑有实体连接,这样就有指定的port可以...
您最亲爱的老肝哥又又上线了! 至於为什麽是又又呢?因为老肝哥刚刚文章打文很开心呢,结果 忘记送出文章...
⚠行前通知 先前已经学过Python但想学爬虫的人可以回来罗~ 从今天起就开始大家最期待的网页爬虫的...