首页 > 其他分享 >一统天下 flutter - 动画: 自定义 Tween - ColorTween, CurveTween, SizeTween, RectTween, AlignmentTween, BorderT

一统天下 flutter - 动画: 自定义 Tween - ColorTween, CurveTween, SizeTween, RectTween, AlignmentTween, BorderT

时间:2023-03-22 11:33:12浏览次数:45  
标签:SizeTween 自定义 动画 CurveTween Tween ColorTween flutter

一统天下 flutter https://github.com/webabcd/flutter_demo
作者 webabcd

一统天下 flutter - 动画: 自定义 Tween - ColorTween, CurveTween, SizeTween, RectTween, AlignmentTween, BorderTween, BoxConstraintsTween, DecorationTween, EdgeInsetsTween, TextStyleTween, IntTween 等

示例如下:

lib\animation\tween3.dart

/*
 * 自定义 Tween - ColorTween, CurveTween, SizeTween, RectTween, AlignmentTween, BorderTween, BoxConstraintsTween, DecorationTween, EdgeInsetsTween, TextStyleTween, IntTween 等
 *
 * 本例以 ColorTween 为例,其他的用法都差不多
 *
 * 当你想对 Color 做动画的时候,会发现直接用 Tween<Color> 是不行的,需要使用 ColorTween(其他的也类似,比如你要对 Rect 做动画就用 RectTween)
 * 像类似 ColorTween 这样的自定义 Tween 是如何实现的呢?其实就是重写 Tween<T> 的 lerp() 方法,自己看源码就能了解了
 */

import 'package:flutter/material.dart';
import 'package:flutter_demo/helper.dart';

class Tween3Demo extends StatefulWidget {
  const Tween3Demo({Key? key}) : super(key: key);

  @override
  _Tween3DemoState createState() => _Tween3DemoState();
}

class _Tween3DemoState extends State<Tween3Demo> with SingleTickerProviderStateMixin {

  late Animation<Color?> _animation;
  late AnimationController _controller;

  @override
  void initState() {
    super.initState();

    _controller = AnimationController(vsync: this, duration: const Duration(seconds: 3),);

    /// 通过 ColorTween 实现在指定的颜色之间做动画
    /// 注:通过 .chain(CurveTween) 可以为动画关联一个缓动类型
    _animation = ColorTween(begin: Colors.red, end: Colors.green).chain(CurveTween(curve: Curves.bounceInOut)).animate(_controller)
      ..addListener(() {
        log("value: ${_animation.value}");
        setState(() {

        });
      });
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      color: Colors.orange,
      child: Column(
        children: [
          Flexible(
            child: Center(
              child: Container(
                color: _animation.value,
                height: 300,
                width: 300,
              ),
            ),
          ),
          Container(
            height: 40,
            color: Colors.red,
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceEvenly,
              children: [
                ElevatedButton(
                  child: const Text('repeat'),
                  onPressed: () {
                    _controller.repeat(reverse: true);
                  },
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }

  @override
  void dispose() {
    /// 清理 AnimationController
    _controller.dispose();
    super.dispose();
  }
}

一统天下 flutter https://github.com/webabcd/flutter_demo
作者 webabcd

标签:SizeTween,自定义,动画,CurveTween,Tween,ColorTween,flutter
From: https://www.cnblogs.com/webabcd/p/flutter_lib_animation_tween3.html

相关文章

  • 自定义类型详解
    一、结构体在C语言中有int,char,float等等类型,可以用来形容某些数据,但是有些数据仅靠一种类型无法描述出来,比如说一个人,我们不仅要描述他的名字,还要描述他的身高、体重、性别......
  • fastadmin 添加自定义按钮
    更新1.index.html2.JS文件3.require-table.js文件......
  • Fastadmin 列表自定义按钮
    Fastadmin列表自定义按钮FastAdmin是一款基于ThinkPHP+Bootstrap的极速后台开发框架。文章目录前言一、单纯的调用接口按钮二、打开新的弹窗页面总结前言Fastadmin列表......
  • .net 自定义转换器JsonConverter的使用
    参考官方文档场景描述例如api返回了以下json串(infcode的值有可能时string也可能时number有时候返回时这个{ "infcode":-1, "detail_msg":null}有时后也可能时这个......
  • 【SpringBoot】自定义注解+拦截
     创建一个注解,用来校验身份@Target({ElementType.TYPE,ElementType.METHOD})@Retention(RetentionPolicy.RUNTIME)public@interfaceAuthUser{//intuser();......
  • js创建自定义对话框
    一:这是只是一个很简单的例子createDialog:function(flag){vardialogMark=document.getElementById("dialogMark");va......
  • css自定义简约滚动条
    应用场景日常开发中,浏览器默认的滚动条样式通常与我们开发项目风格样式不够统一。我们可以通过自定义滚动条样式实现风格统一。css样式<style>/*滚动条*/......
  • k8s实践之自定义控制器crd编写
    本篇文章我们实践用k8s编写一个自定义控制器,文章参考自极客时间张磊老师的课程:深入剖析Kubernetes。1.自定义控制器项目首先我们在GOPATH下,创建一个结构如下的项目:$......
  • tp6自定义变量代替静态资源路径
    tp6在视图页面想使用一个变量直接代替public目录下的一些静态资源目录,可以定义 使用方式: ......
  • 自定义权限控制
    1业务场景在程序中某些选项不能让某些用户使用,需要进行权限控制,并且没有标准的权限对象,就需要自己自定义权限对象进行控制。例如:在选择配置容差时,不允许某些用户使用。......