gpt4 book ai didi

flutter - 如何在flutter中连续改变背景

转载 作者:行者123 更新时间:2023-12-03 18:28:31 26 4
gpt4 key购买 nike

我知道如何更改背景,但我需要不断进行更改。
我该如何实现?

最佳答案

您可以使用 AnimatedContainer 并在动画结束时重置目标颜色。
为了平滑过渡,我建议您可以循环使用颜色列表,您可以对对齐进行相同的操作。

例子:

enter image description here

示例的源代码:

import 'package:flutter/material.dart';

class AnimatedGradient extends StatefulWidget {
@override
_AnimatedGradientState createState() => _AnimatedGradientState();
}

class _AnimatedGradientState extends State<AnimatedGradient> {
List<Color> colorList = [
Colors.red,
Colors.blue,
Colors.green,
Colors.yellow
];
List<Alignment> alignmentList = [
Alignment.bottomLeft,
Alignment.bottomRight,
Alignment.topRight,
Alignment.topLeft,
];
int index = 0;
Color bottomColor = Colors.red;
Color topColor = Colors.yellow;
Alignment begin = Alignment.bottomLeft;
Alignment end = Alignment.topRight;

@override
Widget build(BuildContext context) {
return Scaffold(
body: Stack(
children: [
AnimatedContainer(
duration: Duration(seconds: 2),
onEnd: () {
setState(() {
index = index + 1;
// animate the color
bottomColor = colorList[index % colorList.length];
topColor = colorList[(index + 1) % colorList.length];

//// animate the alignment
// begin = alignmentList[index % alignmentList.length];
// end = alignmentList[(index + 2) % alignmentList.length];
});
},
decoration: BoxDecoration(
gradient: LinearGradient(
begin: begin, end: end, colors: [bottomColor, topColor])),
),
Positioned.fill(
child: IconButton(
icon: Icon(Icons.play_arrow),
onPressed: () {
setState(() {
bottomColor = Colors.blue;
});
},
),
)
],
));
}
}

关于flutter - 如何在flutter中连续改变背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62370055/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com