如何在flutter中制作多种颜色的ColorTween动画

Lak*_*tta 3 dart flutter flutter-animation

我制作了这张自定义卡(来自 UNO 游戏),看起来像 UNO 4+游戏卡

我使用 ColorTween 在 1 秒内更改容器的 boxshadow 属性,代码如下

class SpecialUnoCard extends StatefulWidget {
  final String _value;
  SpecialUnoCard(this._value);

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

class _SpecialUnoCardState extends State<SpecialUnoCard>
    with SingleTickerProviderStateMixin {
  AnimationController _controller;
  Animation _animation;
  int index = 0;

  final _listOfTweens = [
    ColorTween(begin: red, end: blue),
    ColorTween(begin: red, end: green),
    ColorTween(begin: red, end: orange),
    ColorTween(begin: blue, end: red),
    ColorTween(begin: blue, end: green),
    ColorTween(begin: blue, end: orange),
    ColorTween(begin: green, end: red),
    ColorTween(begin: green, end: blue),
    ColorTween(begin: green, end: orange),
    ColorTween(begin: orange, end: red),
    ColorTween(begin: orange, end: green),
    ColorTween(begin: orange, end: blue),
  ];

  ColorTween tween() =>
      _listOfTweens[Random().nextInt(_listOfTweens.length - 1)];

  @override
  void initState() {
    _controller =
        AnimationController(vsync: this, duration: Duration(seconds: 1));
    _animation = tween()
        .chain(CurveTween(curve: Curves.easeInOutCubic))
        .animate(_controller);

    _controller.addListener(() {
      setState(() {});
    });

    _controller.addStatusListener((status) {
      if (status == AnimationStatus.completed) {
        _controller.reverse();
      } else if (status == AnimationStatus.dismissed) {
        _controller.forward();
      }
    });

    _controller.forward();

    super.initState();
  }

  @override
  void deactivate() {
    _controller.dispose();
    super.deactivate();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      margin: EdgeInsets.symmetric(
          vertical: _cardMarginVer, horizontal: _cardMarginHor),
      padding: EdgeInsets.all(15),
      decoration: BoxDecoration(
        borderRadius: BorderRadius.circular(_cardCornerRadii),
        border: Border.all(
            color: _animation.value, width: 4, style: BorderStyle.solid),
        boxShadow: [
          BoxShadow(color: _animation.value, spreadRadius: 12, blurRadius: 25)
        ],
        color: Colors.black,
      ),
      child: Container(
        height: _cardHeight,
        width: _cardWidth,
        decoration: BoxDecoration(
          borderRadius: BorderRadius.circular(30),
          color: Colors.black,
        ),
        child: (widget._value == plus4)
            ? SvgPicture.asset('assets/plus4.svg')
            : SvgPicture.asset('assets/wild.svg'),
      ),
    );
  }
}
Run Code Online (Sandbox Code Playgroud)

现在,有没有一种方法可以在一堆值之间设置动画或随机排列颜色?我想要一些与以下伪代码相关的功能

ColorTween(values: <Color>[Colors.orange , Colors.red , Colors.blue , ........]
Run Code Online (Sandbox Code Playgroud)

你可以说我想将颜色链接在一起

我尝试查找以下帖子,但没有找到我需要的内容。 如何在 Flutter 中更改 ColorTween 颜色

谢谢你的时间!

ili*_*ots 6

尝试RainbowColor,它允许在多种颜色之间进行插值/补间。它提供了 ColorTween 的多色变体,正如您所描述的:

RainbowColorTween([Colors.orange, Colors.red, Colors.blue])
Run Code Online (Sandbox Code Playgroud)

它也可以在补间上下文之外使用,以跨光谱插入颜色,例如

var rb = Rainbow(spectrum: [Colors.orange, Colors.red, Colors.blue]);
Color warmColor = rb[0.25];
Color coldColor = rb[0.83];
Run Code Online (Sandbox Code Playgroud)

顺便说一句,时机很好。我昨天刚刚发布了RainbowColor,老实说我不知道​​几天前我制作它时你问了这个问题。