我有一些动画代码可以无限改变背景颜色
但是问题是我想在没有按钮触发的情况下完成
并且我希望它在第一次(首次加载时)是自动化的
下面是代码
我被困住了,谁能帮忙
目的是无需触发即可进行动画处理,它会自动运行更改背景的动画
import 'package:flutter/material.dart';
class ScreenTime extends StatefulWidget {
@override
_ScreenTimeState createState() => _ScreenTimeState();
}
class _ScreenTimeState extends State<ScreenTime> {
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.black54;
Color topColor = Colors.white10;
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;
});
},
),
)
],
));
}
}
答案 0 :(得分:0)
这个问题的目的基本上是“如何在没有触发的情况下为AnimatedContainer设置动画”
我想我找到了解决方法
我所做的是
只需创建一个新函数,然后在build方法中调用该方法
和方法代码如下
import docker
def checkDockerContainerStatus( container):
client = docker.from_env()
#cli = docker.APIClient()
if client.containers.list(filters={'name': container}):
response = client.containers.list(filters={'name': container})
return str(response[0].id)[:12]
else:
return None
running_id = checkDockerContainerStatus('app-container')
if running_id is not None:
print(f"Found! {running_id}")
else:
print("Container app-container is not found")
我的第二个解决方案是这个,我认为它可行
但是我担心它是否有副作用
setState(() {
index = index + 1;
bottomColor = Colors.blue;
});
答案 1 :(得分:0)
要在解决问题时尽可能多地保留原始实现,
WidgetsBinding.instance.addPostFrameCallback((_) => setState(() {bottomColor = Colors.blue;}));
initState
中的不会造成问题。只要在initState
中完成即可。在build
中调用它会导致不希望的额外无限循环。
但是,在repeat
上使用AnimationController
几乎可以肯定是一种更简洁的实现。