如何在 Flutter 中更改 ColorTween 颜色

2024-03-29

当我在 Flutter 中调用 setState() 时,我想更改 ColorTween 中的颜色

这是我的动画图像

import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';

class FingerprintImageWidget extends StatefulWidget {
  FingerprintImageWidget(
      {Key key, this.width, this.height, this.beginColor, this.endColor})
      : super(key: key);

  final double width;
  final double height;
  Color beginColor;
  Color endColor;

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

class FingerprintImageWidgetState extends State<FingerprintImageWidget>
    with SingleTickerProviderStateMixin {
  AnimationController _animationController;
  Animation<Color> _colorTween;
  Color beginColor;
  Color endColor;

  @override
  void initState() {
    beginColor = widget.beginColor;
    endColor = widget.endColor;
    _animationController =
        AnimationController(vsync: this, duration: const Duration(seconds: 1));
    _colorTween = ColorTween(begin: beginColor, end: endColor)
        .animate(_animationController);
    changeColors();
    super.initState();
  }


  Future<void> changeColorController;
  @override
  void dispose() {
    _animationController.dispose();
    disposed = true;
    super.dispose();
  }

  bool disposed = false;

  Future<void> changeColors() async {
    while (!disposed) {
      if (disposed) return;
      await Future<void>.delayed(const Duration(milliseconds: 1300), () {
        if (_animationController.status == AnimationStatus.completed) {
          _animationController.reverse();
        } else {
          _animationController.forward();
        }
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    return AnimatedBuilder(
        animation: _colorTween,
        builder: (BuildContext context, Widget child) => AppImage(
              IMAGE_FINGERPRINT,
              width: widget.width,
              height: widget.height,
              color: _colorTween.value,
              fit: BoxFit.contain,
            ));
  }
}

我通过分离颜色补间对象的动画对象解决了我的问题

  void redraw(Color beginColor, Color endColor) {
    setState(() {
      _colorTween = ColorTween(begin: beginColor, end: endColor);

      _colorTweenAnimation = _colorTween.animate(_animationController);
    });
  }

全班:

import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';

class FingerprintImageWidget extends StatefulWidget {
  FingerprintImageWidget(
      {Key key, this.width, this.height, this.beginColor, this.endColor})
      : super(key: key);

  final double width;
  final double height;
  Color beginColor;
  Color endColor;

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

class FingerprintImageWidgetState extends State<FingerprintImageWidget>
    with SingleTickerProviderStateMixin {
  AnimationController _animationController;
  ColorTween _colorTween;
  Animation<Color> _colorTweenAnimation;
  Color beginColor;
  Color endColor;

  @override
  void initState() {
    beginColor = widget.beginColor;
    endColor = widget.endColor;
    _animationController =
        AnimationController(vsync: this, duration: const Duration(seconds: 1));
    _colorTween = ColorTween(begin: beginColor, end: endColor);
    _colorTweenAnimation = _colorTween.animate(_animationController);
    changeColors();
    super.initState();
  }

  void redraw(Color beginColor, Color endColor) {
    setState(() {
      _colorTween = ColorTween(begin: beginColor, end: endColor);

      _colorTweenAnimation = _colorTween.animate(_animationController);
    });
  }

  Future<void> changeColorController;
  @override
  void dispose() {
    _animationController.dispose();
    disposed = true;
    super.dispose();
  }

  bool disposed = false;

  Future<void> changeColors() async {
    while (!disposed) {
      if (disposed) return;
      await Future<void>.delayed(const Duration(milliseconds: 1300), () {

        if (_animationController.status == AnimationStatus.completed) {
          _animationController.reverse();
        } else {
          _animationController.forward();
        }
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    return AnimatedBuilder(
        animation: _colorTweenAnimation,
        builder: (BuildContext context, Widget child) => AppImage(
              IMAGE_FINGERPRINT,
              width: widget.width,
              height: widget.height,
              color: _colorTweenAnimation.value,
              fit: BoxFit.contain,
            ));
  }
}

然后我使用了全局键,这样我就可以调用重绘

  final GlobalKey<FingerprintImageWidgetState> _fingerprintImageKey =
      GlobalKey();
    FingerprintImageWidget(
                key: _fingerprintImageKey,
                width: 70,
                height: 100,
                beginColor: beginFingerColor,
                endColor: endFingerColor,
              ),
      _fingerprintImageKey.currentState.redraw(beginFingerColor,endFingerColor);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在 Flutter 中更改 ColorTween 颜色 的相关文章

随机推荐

  • UICollectionView 委托方法 cellForItemAtIndexPath:indexPath 未从 sizeForItemAtIndexPath 调用

    当我打电话时 collectionView cellForItemAtIndexPath indexPath 从内部 collectionView layout sizeForItemAtIndexPath 那么委托方法不会被触发 知道为什
  • 从另一个项目将外部资源添加到gradle中的jar中

    我有一个项目 有一个 资源 库 不是基于java的 它仅包含多个 java 项目使用的文件层次结构 library files binaries etc 在我的一个java项目中 我想将这些文件包含在组装的jar文件中 current de
  • LOH 碎片 - 2015 年更新

    有大量有关 NET LOH 的信息 并且已在各种文章中进行了解释 不过 有些文章似乎缺乏一点精确性 过时的信息 In Brian Rasmussen 的回答 2009 Microsoft 项目经理 https stackoverflow c
  • 如何增加 swift int 枚举

    我有一个快速枚举 enum MainState Int case NotStarted case Init case AskWhatToText case RecordWhatToText var state MainState NotSt
  • ActiveRecord查询别名字段名输出

    假设我有一张桌子World 我有一个名为foo表内 我想查询的是World表并选择foo 但我想将其别名为bar在后续转换为 JSON 输出时 有什么方法可以为这一 ActiveRecord 查询设置字段名称的别名吗 不希望在整个应用程序中
  • 从文本文件中读取矩阵并将其存储在二维数组中

    我一直在尝试将矩阵输入存储在数组中的文本文件中 但它显示了特殊的输出 这是代码 include
  • WinRT 组件能否在 Windows 7 中工作[重复]

    这个问题在这里已经有答案了 我读到Windows有来自Windows 8的新API 它被称为WinRT 我打算使用它 但我担心Windows 7 My Goal 我计划构建一种新的编程语言并使用它 但为了使其可用 它必须具有 Gui 编程
  • 学说:两个数据库中两个实体之间的关系

    我正在使用 MySQL 和 Doctrine 2 与 Symfony 3 我想在两个独立数据库中的两个实体之间建立多对多关系 我认为 Doctrine 无法处理这个问题 至少不能以原生方式处理 无论如何 为了执行此操作 我正在使用schem
  • 使用模板:首先解决运算符还是首先解决转换?

    我昨天看到了一些有趣的编译器行为 我想我明白为什么会发生这种情况 但我想确定一下 所以 我不会写我的推理 只写事实 请注意 这不是我包含的拼写错误vector代替string 我是故意这样做的 这样编译器就无法理解 std string 是
  • 如何使用计时器对图像应用淡入淡出过渡效果?

    我正在尝试在两个 PictureBox 控件之间进行淡入淡出过渡 我使用计时器来更改两个 PictureBox 的不透明度GetPixel and SetPixel每当时间流逝 在这个阶段 问题是这段代码引发了异常 System Inval
  • for循环的优化

    我正在编写一些 C 代码 目前应该尽可能快地运行 通常以 100 的速度占用单个核心约 25 分钟 我需要代码保持单核 因为跨多个核运行此代码的好处不会像同时多次运行此项目那样大 有问题的代码如下 public Double UpdateS
  • System.Reflection.TargetInitationException 未被捕获

    解决方案后添加的注释 在反射调用的方法中抛出了 AccessViolationException 这就是无法捕获 TargetInitationException 的原因 注意 这是在 IDE 外部 引用的问题不相同 https stack
  • 如何在 JFileChooser 中指定默认的新目录名称?

    在 Java 程序中 我想显示一个 JFileChooser 用户只需选择一个将写入多个输出文件的目录名 所以 mychooser setFileSelectionMode JFileChooser DIRECTORIES ONLY 这很容
  • AssertionError:unstack() 数据帧时 blk ref_locs 中存在间隙

    我正在尝试 unstack Pandas 数据框中的数据 但我不断收到此错误 我不知道为什么 这是到目前为止我的代码和我的数据示例 我尝试修复它是删除 voteId 不是数字的所有行 这不适用于我的实际数据集 当我部署代码时 在 Anaco
  • 如何在android中使用Room Persistence Library查询嵌套的嵌入式对象?

    考虑我有 3 个类用户 地址 位置 class Address public String street public String state public String city ColumnInfo name post code pu
  • 在 Coq 中查找 ++ 等定义和符号

    我们如何获得这些符号的定义 类型 例如 or of List 我努力了 Search Search Search SearchAbout and Check Check Check 然而它们都不起作用 SearchAbout 确实显示了一些
  • JNI JVM 调用类路径

    我正在使用 Cygwin 编写一个小型 C 程序 该程序启动 Java 虚拟机 我使用的库需要 POSIX 环境 到目前为止 只要将所有类放在与可执行文件相同的文件夹中 我就可以让它工作 但是 我想指定一个实际的 JAR 文件 其中包含我要
  • 如何使用单独调用函数的值快速填充 numpy 数组

    我想用生成的值填充 numpy 数组 这些值由生成器函数生成 数组长度不太长 通常 到目前为止 我已经可以使用 vanilla python 做到这一点 def generate return generated data array np
  • lein Figwheel 与 lein cljsbuild auto

    lein Figwheel 和 lein cljsbuild auto 有什么区别 因为我相信它们都是用来编译 clojurescript 的 另外 使用其中一种比另一种有什么好处吗 Figwheel 它们都是 lein 插件 但 Figw
  • 如何在 Flutter 中更改 ColorTween 颜色

    当我在 Flutter 中调用 setState 时 我想更改 ColorTween 中的颜色 这是我的动画图像 import dart async import package flutter material dart import p