如何在颤振中的每次重复动画之间添加延迟

How to add delay between each repeat of animation in flutter

我的应用程序中有一个带有容器的 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();
      }
    });
  }

每次重复后延迟:

  @override
  void initState() {// <-- add initstate
    super.initState();
    _animationController.forward(from: 0.0);
  }

  late final AnimationController _animationController = AnimationController(
      vsync: this,
      duration: const Duration(seconds: 1))
     //..repeat(reverse: true); // <-- comment this line

    ..addStatusListener((AnimationStatus status) { // <-- add listener
      if (status == AnimationStatus.completed) {
        Future.delayed(
            Duration(milliseconds: _animationController.value == 0 ? 500 : 0),
            () {
          _animationController
              .animateTo(_animationController.value == 0 ? 1 : 0);
        });
      }
    });