[Day21] Flutter GetX with animation

Flutter animation 原生的StatefullWidget
添加 SingleTickerProviderStateMixin,
使用GetX的话 我个人是把animateionController写在GetXController,
GetX有提供SingleGetTickerProviderMixin.

下面直接看code
GetxController部分
animationController 设定整个动画在0.5秒.
接下来 设定了两个animation

  1. flipAnimation:
    Tween 0.0 -> 2.0 用在图片旋转.
  2. scaleAnimation:
    这边用的是TweenSequence 可以设定时间的占比(weight)和变化量.
    想做一个sacel的 小->大->小 变化的过程(1 -> 2 > 3 -> 2 -> 1)
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(),
      ),
    );
  },
),
),

实际效果
https://cdn-images-1.medium.com/max/800/1*hvBSrLziUF5mqwFFoJiVKw.gif

本篇的GitHub source code

下一篇将为大家介绍flutter Dio


<<:  Terraform

>>:  Day. 27 Binary Tree Level Order Traversal

Day20-不能说的秘密(二)

前言 昨天有说到在储存使用者的密码时,不管是用 AES 把他们加密起来,或是经过 SHA1 杂凑之後...

[Day 19] tinyML开发好帮手─云端一站式平台Edge Impulse简介

自从上次在街边吃了一碗不怎麽地「杂碎面」,咖哩鱼蛋没鱼味,咖哩又不入味,失败!猪皮煮得太烂,没咬头,...

硬体的讯号怎麽丢给软件?

预设 先要有一个开发板,可以接各种sensor。 可以先跟电脑有实体连接,这样就有指定的port可以...

老肝哥-菜鸟Java的LeetCode历程,第九题:Palindrome Number,朝远大目标前进!

您最亲爱的老肝哥又又上线了! 至於为什麽是又又呢?因为老肝哥刚刚文章打文很开心呢,结果 忘记送出文章...

Day 14:专案02 - PTT C_chat版爬虫01 | 爬虫简介、request和response、Requests

⚠行前通知 先前已经学过Python但想学爬虫的人可以回来罗~ 从今天起就开始大家最期待的网页爬虫的...