如何在 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 颜色 的相关文章

  • 未为“AudioCache”类型定义“play”方法

    问题 未为 AudioCache 类型定义 play 方法 导入 包 flutter material dart 导入 包 audioplayers src audio cache dart void main runApp Xylopho
  • Firebase云函数在Flutter中返回null,因为它仍在运行

    更新 我从我的 firebase 控制台得到这个 函数执行花费了 2906 毫秒 完成状态代码 200 15 秒后 我得到 console log DOC 确实存在 我正在运行这个云火库功能 它 有效 但我不断得到这样的回报 FLUTTER
  • Dart 是单线程的,但为什么它使用 Future 对象并执行异步操作

    在文档中 Dart 是单线程的 但为了一次执行两个操作 我们使用与线程相同工作的 future 对象 使用Future对象 futures 来执行异步操作 如果 Dart 是单线程的 那么为什么它允许执行异步操作 Note 异步操作是并行操
  • 如何在 flutter 搜索页面小部件中消除搜索建议?

    我需要使用默认 flutter 的 Google 地方信息搜索建议搜索页 每当用户开始输入时 我都需要提供自动完成建议 并且我使用异步方式实现了这一点FutureBuilder 现在的问题是我需要将搜索请求的调度去抖 500 毫秒或更长时间
  • CustomPainter 使用 Listenable 重绘

    CustomPainter 类似乎有几种触发重绘的方法 我让我的画家使用 shouldRepaint 方法 但是 我希望我的画家对可监听中的更改做出反应 而不是轮询更改 Flutter 文档指出 触发重绘的最有效方法是 扩展此类并向 Cus
  • Flutter 命令删除 .pub-cache 文件夹中的包

    如何删除flutter包 pub cache文件夹 当我们给予flutter clean 它将删除当前目录中的build文件夹 我们可以手动删除 但是我的要求是删除里面的包 pub cache使用命令的文件夹 要清除全局 PUB CACHE
  • 父数据小部件使用不正确。扩展的小部件必须放置在弹性小部件内

    我收到以下错误 即 抛出了另一个异常 ParentDataWidget 的使用不正确 在移动屏幕上显示错误 override Widget build BuildContext context return MaterialApp titl
  • 快捷栏持续时间和高度

    我正在尝试展示一个小吃店 当我点击手势检测器后 这个小吃有两个按钮 问题是小吃栏出现几秒钟然后消失 所以我有两个问题 如何阻止小吃栏消失 直到用户采取行动并单击按钮 此外 小吃栏具有整个屏幕的高度 如何使其在屏幕底部具有特定高度 您可以使用
  • MappedListIterable 不是子类型

    我是 flutter 和 dart 的新手 并尝试从 firestore 作为流获取数据并将其提供给我的 ListView 但我不断收到此错误 type MappedListIterable
  • 使用 Visual Studio Code 在 Flutter 上运行 Gradle 时出错

    我正在使用 Windows 10 Visual Studio Code 我尝试在编写代码后运行我的代码 这就是它之后向我展示的内容 在调试模式下在 TECNO Camon CX 上启动 lib main dart 运行 Gradle 时出错
  • 带操作按钮的颤动本地通知

    我在我的 flutter 项目中尝试了 flutter 本地通知插件 它在简单通知上工作正常 但我需要带有操作按钮的通知功能 请帮助我或建议我实现此功能 不幸的是 flutter local notifications 插件尚不支持操作按钮
  • Android Studio Flutter 项目错误:内存不足

    我在 Android Studio 上运行任何 flutter 项目 都会抛出内存不足错误 控制台中显示的消息如下所示 e b build slave windows engine build src third party dart ru
  • Flutter - 名称为 [DEFAULT] 的 FirebaseApp 不存在

    我正在使用 firebase 身份验证系统和 Firestore 开发一个 flutter 应用程序 我工作了3个月 之前没有遇到过这个错误 现在我在 Play 商店上发布了我的应用程序 并且我发现使用模拟器的调试版本也出现此错误 我认为已
  • Flutter WillPopScope 与 AlertDialog 迁移到空安全

    我最近将我的 Flutter 应用程序迁移到空安全 但 WillPopScope 与 AlertDialog 结合使用会导致问题 WillPopScope期望Future
  • 未安装 Visual Studio;这对于 Windows 开发是必要的

    My 颤振医生 https docs flutter dev get started install windows run flutter doctor是说 Visual Studio develop for Windows X Visu
  • 使用不包含 Bloc 的上下文调用 BlocProvider.of() - 即使它包含

    首先 我确实知道 BLoC 是如何运作的 它背后的想法 我知道两者之间的区别BlocProvider and BlocProvider value 构造函数 为简单起见 我的应用程序有 3 个页面 其中有一个小部件树 如下所示 App gt
  • Flutter无法从url加载图像

    图片资源服务捕获异常 解析图像编解码器时抛出以下 ImageCodecException 加载网络图像失败 图片网址 https cdn sportmonks com images soccer leagues 5 png https cd
  • 如何将 Flutter 应用连接到 tcp 套接字服务器?

    我很难将 Flutter 应用程序连接到服务器上的网络 tcp 套接字 我知道我必须使用某种中间选项 以便在 tcp 套接字到 flutter 以及 Flutter 到 tcp 套接字之间转换数据 任何想法 信息如何实现这一目标 问题是如何
  • 使用 EditableText 进行 Flutter

    我正在尝试弄清楚如何在 Flutter 中使用 TextEditor 我有 卡片编辑器 基本上我希望能够处理相当于一段文本的内容 new EditableText autofocus true maxLines null backgroun
  • 条件必须具有 bool 静态类型

    有什么方法可以在这种情况下使用未来的布尔值 或者有更好的方法吗 Widget buildRow String pair final Future

随机推荐

  • 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