Home > Software design >  Postpone the start of the AnimationController animation in Flutter
Postpone the start of the AnimationController animation in Flutter

Time:04-01

I'm trying to delay the animation for 5 seconds from the beginning, here's my code snippet, but it doesn't work properly, as it would be better to delay for 5 seconds from the start of the startup screen. I tried to make a separate function that would be responsible for the delay but it also caused me to display errors on the emulator. I tried to use a ternary operator and delay it, but it also didn't work properly. I also want my animation to come in 7 seconds, I think it can also be done in a ternary operator. I don't yet know exactly how to stop the animation, so I used a long delay in the animation to make it invisible.

class BubbleBackground extends StatefulWidget {
  final Widget child;
  final double size;

  const BubbleBackground({
    Key key,
    @required this.child,
    @required this.size,
  }) : super(key: key);
  @override
  State<BubbleBackground> createState() => _BubbleBackgroundState();
}

class _BubbleBackgroundState extends State<BubbleBackground>
    with SingleTickerProviderStateMixin {
  bool timer = false;
  final longAnimationDuration = 100000;
  final shortAnimationDuration = 700;
  AnimationController _controller;
  @override
  void initState() {
    setState(() {
      // Future.delayed(Duration(seconds: 5), () {
      //   timer = true;
      // });
      timer = true;
    });

    _controller = AnimationController(
      lowerBound: 0.9,
      duration: Duration(
        milliseconds: timer
            ? Future.delayed(
                Duration(seconds: 5),
                () {
                  shortAnimationDuration;
                },
              )
            : longAnimationDuration,
      ),
      vsync: this,
    )..repeat(reverse: true);
    super.initState();
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return AnimatedBuilder(
      animation: _controller,
      builder: (_, __) {
        return Transform.scale(
          scale: _controller.value,
          child: Container(
            width: widget.size,
            height: widget.size,
            alignment: Alignment.center,
            decoration: BoxDecoration(
              color: Colors.white,
              shape: BoxShape.circle,
            ),
            child: widget.child,
          ),
        );
      },
    );
  }
}

CodePudding user response:

i figured out that I could just write initState normally, and I'll get desirable result for time delay

@override
  void initState() {
    super.initState();

    _controller = AnimationController(
      lowerBound: 0.9,
      duration: Duration(milliseconds: shortAnimationDuration),
      vsync: this,
    );
    Future.delayed(Duration(seconds: 5), () {
      _controller.repeat(reverse: true);
    });
  }

CodePudding user response:

try Timer like below code

late AnimationController animationController;

  @override
  void initState() {
    super.initState();
    animationController = AnimationController(
      vsync: this,
      duration: const Duration(milliseconds: 600),
    );

    Timer(
        const Duration(milliseconds: 1500),
        () => setState(() {
              isExpand = true;
              animationController.forward());
            }));
     }
  • Related