如何在Flutter中为每次重复动画添加延迟

4

我在我的应用程序中使用了一个带有容器的SlideTransition,它会不断重复,但我希望每次重复之后都有一段延迟时间。就像这样:Visual Representation

这是我的代码

  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,
      ),
    ),
  ),
);


   

这个对你有帮助吗?链接 - Peter Koltai
不是很确定,我之前看过那篇帖子,但问题在于我不知道如何将答案集成到我的代码中。 - Giuliano Condrache
2个回答

4

只需更改您想要的持续时间 => 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();
      }
    });
  }

0

每次重复后延迟:

  @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);
        });
      }
    });

这个代码是可以工作的,但是它只会在重复完成后添加延迟,这是我的错,我没有明确指定我也想要在反向和正向之间添加延迟。 - Giuliano Condrache

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接