Flutter 如何使用颤振设置progressbar颜色的动画

Flutter 如何使用颤振设置progressbar颜色的动画,flutter,flutter-animation,Flutter,Flutter Animation,我尝试创建一个动画进度条,其中进度以动画方式填充,并且根据进度的最终值更改其颜色。 第一部分工作正常,但我不知道彩色动画有什么问题。我错过了什么 我用来显示进度条 import 'package:flutter/material.dart'; import 'package:flutter_rounded_progress_bar/flutter_rounded_progress_bar.dart'; import 'package:flutter_rounded_progress_bar/ro

我尝试创建一个动画进度条,其中进度以动画方式填充,并且根据进度的最终值更改其颜色。 第一部分工作正常,但我不知道彩色动画有什么问题。我错过了什么

我用来显示进度条

import 'package:flutter/material.dart';
import 'package:flutter_rounded_progress_bar/flutter_rounded_progress_bar.dart';
import 'package:flutter_rounded_progress_bar/rounded_progress_bar_style.dart';

class MyLinearProgressIndicator extends StatefulWidget {
  final double currentProgress;
  final double height;
  final Color foregroundColor;
  final int duration = 500;

  MyLinearProgressIndicator({
    this.currentProgress,
    this.height = 5,
    this.foregroundColor = const Color(0xFFde8405)});

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

class _LinearProgressIndicatorState extends State<MyLinearProgressIndicator> with 
SingleTickerProviderStateMixin {
  AnimationController progressController;
  Animation<double> animation;
  Animation<Color> colorAnimation;
  CurvedAnimation curve;

  @override
  void initState() {
    super.initState();
    progressController = AnimationController(vsync: this, duration: Duration(milliseconds: widget.duration));

  curve = CurvedAnimation(parent: progressController, curve: Curves.ease);

  animation =
    Tween<double>(begin: 0, end: widget.currentProgress).animate(curve)
      ..addListener(() {
        setState(() {});
      });
  Color endColor;
  if (widget.currentProgress <= 30) {
    endColor = const Color(0xFFFF0000);
  } else if (widget.currentProgress <= 50) {
    endColor = const Color(0xFF00FF00);
  } else {
    endColor =const Color(0xFF0000FF);
  }
  colorAnimation =
    ColorTween(begin: widget.foregroundColor, end: endColor).animate(curve)
      ..addListener(() {
        setState(() {});
      });

  WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
    progressController.forward();
  });
}

@override
Widget build(BuildContext context) {
  return Row(
    children: <Widget>[
      Padding(
        padding: const EdgeInsets.only(right: 16.0),
        child: Text("${widget.currentProgress.floor()}%",
            style: TextStyle(
              fontWeight: FontWeight.bold,
              fontSize: 12,
              color: Color(0xFF333333))),
      ),
      Container(
        height: widget.height,
        width: 100,
        child: RoundedProgressBar(
          milliseconds: widget.duration,
          height: widget.height,
          style: RoundedProgressBarStyle(
              borderWidth: 0,
              widthShadow: 0,
              colorProgress: colorAnimation.value,
              backgroundProgress: Color(0xFFEBEBEB)),
          percent: animation.value,
        )),
      ],
    );
  }

  @override
  void dispose() {
    progressController.dispose();
    super.dispose();
  }
}
导入“包装:颤振/材料.省道”;
导入“包:颤振_圆形_进度_条/颤振_圆形_进度_条.省道”;
导入“包:颤振_圆形_进度_条/圆形_进度_条_风格.省道”;
类MyLinearProgressIndicator扩展StatefulWidget{
最终进度;
最终双倍高度;
最终颜色前底色;
最终积分持续时间=500;
糜线推进指示器({
这是一种进步,
这个高度=5,
this.foregroundColor=const Color(0xFFde8405)};
@凌驾
_LinearProgressIndicatorState createState()=>
_LinearProgressingIndicatorState();
}
类_LinearProgressIndicatorState使用扩展状态
SingleTickerProviderStateMixin{
动画控制器;
动画;
动画色彩动画;
曲线化曲线;
@凌驾
void initState(){
super.initState();
progressController=AnimationController(vsync:this,duration:duration(毫秒:widget.duration));
曲线=曲线动画(父级:progressController,曲线:Curves.ease);
动画=
Tween(开始:0,结束:widget.currentProgress)。动画(曲线)
…addListener(){
setState((){});
});
颜色端点颜色;

如果(widget.currentProgress当前的行为是什么?进度条从0到widget.currentProgress的动画设置正确,但颜色没有改变它显示给您的颜色是什么?原始的widget.foreGroundColor基于上面的代码,我尝试在DartPad中创建一个示例,但由于我无法在那里使用progressbar库,我使用简单的小部件重新创建了一些类似的东西,并且使用了这些小部件。问题可能在于我使用的库?当前的行为是什么?进度条从0到widget.currentProgress的动画设置正确,但颜色没有改变它显示给您的颜色是什么?原始的widget.foreGroundColor基于代码上面,我尝试在DartPad中创建一个示例,但由于我无法使用progressbar库,因此我使用简单的小部件重新创建了一些Similar,并使用了这些小部件。问题可能在于我使用的库?