如何在flutter中制作多种颜色的ColorTween动画
How to make ColorTween animation with multiple colors in flutter
我制作了这张自定义卡片(来自 UNO 游戏),它看起来像
我已经使用 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'),
),
);
}
}
现在,有没有办法在一组值之间设置动画或随机排列颜色?我想要一些与以下伪代码相关的功能
ColorTween(values: <Color>[Colors.orange , Colors.red , Colors.blue , ........]
你可以说我想把颜色串在一起
我尝试查找以下 post,但没有找到我需要的内容。
How could I change ColorTween colors in Flutter
感谢您的宝贵时间!
查看 https://flutter.dev/docs/cookbook/animation/animated-container 处的 AnimatedContainer。这是处理动画的一种更简单的方法,我认为它正是您要找的东西。页面上的测试代码将让您立即进行测试。只需放置您的装饰并稍微整合随机值即可看到魔法。祝你好运!
试试 RainbowColor,它允许 interpolation/tweening 几种颜色。它完全按照您的描述提供了 ColorTween 的多色变体:
RainbowColorTween([Colors.orange, Colors.red, Colors.blue])
它也可以在补间上下文之外使用,以在整个光谱中插入颜色,例如
var rb = Rainbow(spectrum: [Colors.orange, Colors.red, Colors.blue]);
Color warmColor = rb[0.25];
Color coldColor = rb[0.83];
顺便说一句,时机不错。我昨天才发布 RainbowColor,老实说,我几天前做的时候真的不知道你问了这个问题。
我制作了这张自定义卡片(来自 UNO 游戏),它看起来像
我已经使用 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'),
),
);
}
}
现在,有没有办法在一组值之间设置动画或随机排列颜色?我想要一些与以下伪代码相关的功能
ColorTween(values: <Color>[Colors.orange , Colors.red , Colors.blue , ........]
你可以说我想把颜色串在一起
我尝试查找以下 post,但没有找到我需要的内容。 How could I change ColorTween colors in Flutter
感谢您的宝贵时间!
查看 https://flutter.dev/docs/cookbook/animation/animated-container 处的 AnimatedContainer。这是处理动画的一种更简单的方法,我认为它正是您要找的东西。页面上的测试代码将让您立即进行测试。只需放置您的装饰并稍微整合随机值即可看到魔法。祝你好运!
试试 RainbowColor,它允许 interpolation/tweening 几种颜色。它完全按照您的描述提供了 ColorTween 的多色变体:
RainbowColorTween([Colors.orange, Colors.red, Colors.blue])
它也可以在补间上下文之外使用,以在整个光谱中插入颜色,例如
var rb = Rainbow(spectrum: [Colors.orange, Colors.red, Colors.blue]);
Color warmColor = rb[0.25];
Color coldColor = rb[0.83];
顺便说一句,时机不错。我昨天才发布 RainbowColor,老实说,我几天前做的时候真的不知道你问了这个问题。