5

I have a SlideTransition with a container in my application, and it repeats forever, but i would like a delay after each repeat. so it would be like this:Visual Representation

Here's my code

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


   
2
  • Does this help you? Commented Aug 16, 2021 at 18:58
  • not really, i have seen that post before, but the problem is that i don't really know how to integrate the answer in my code. Commented Aug 16, 2021 at 19:28

2 Answers 2

6

Just change the duration you want => await Future.delayed(Duration(seconds: 3)); I test it with seconds: 3 to get better idea.

 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();
      }
    });
  }
Sign up to request clarification or add additional context in comments.

Comments

0

delay after each repeat :

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

1 Comment

this worked but it only added a delay once the repeat has also been done, it is my fault i didn't specify i wanted between reverse and forward aswell

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.