How could I change ColorTween colors in Flutter

吃可爱长大的小学妹 提交于 2021-02-20 02:55:09

问题


I want to change the colors in the ColorTween when I call setState() in Flutter

Here is my animated image

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,
            ));
  }
}

回答1:


I solved my issue by separating the animation object of the color tween object

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

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

Full class:

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,
            ));
  }
}

Then I used a global key so I can call redraw

  final GlobalKey<FingerprintImageWidgetState> _fingerprintImageKey =
      GlobalKey();
    FingerprintImageWidget(
                key: _fingerprintImageKey,
                width: 70,
                height: 100,
                beginColor: beginFingerColor,
                endColor: endFingerColor,
              ),
      _fingerprintImageKey.currentState.redraw(beginFingerColor,endFingerColor);


来源:https://stackoverflow.com/questions/57545973/how-could-i-change-colortween-colors-in-flutter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!