我的应用程序中有一个带有容器的 SlideTransition,它会永远重复,但我希望在每次重复后都有延迟。所以它会是这样的:
这是我的代码
late final AnimationController _animationController = AnimationController(
vsync: this,
duration: const Duration(seconds: 1)
)..repeat(reverse: true); // Here there should be the 500 millisecond delay
late final Animation<Offset> _animation = Tween<Offset>(
begin: Offset.zero,
end: Offset(0, 1),
).animate(_animationController);
. . .
return Scaffold(
body: Center(
child: SlideTransition(
position: _animation,
child: Container(
height: 100,
width: 100,
color: Colors.red,
),
),
),
);
最佳答案
只需更改您想要的持续时间 => await Future.delayed(Duration(seconds: 3));
我用 seconds: 3
测试它以获得更好的想法。
late final AnimationController _animationController =
AnimationController(vsync: this, duration: const Duration(seconds: 3))
..forward();
@override
void initState() {
super.initState();
_animationController.addListener(() async {
print(_animationController.status);
if (_animationController.isCompleted) {
await Future.delayed(Duration(seconds: 3));
_animationController.reverse();
} else if (_animationController.isDismissed) {
await Future.delayed(Duration(seconds: 3));
_animationController.forward();
}
});
}
关于flutter - 如何在 flutter 中的每次重复动画之间添加延迟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68807827/