10

I was trying to add a button to this page that will (play or pause) the waves animation in the background. code link: https://github.com/apgapg/flutter_profile/blob/master/lib/page/intro_page.dart

I've tried many things but since I still bad with flutter animations I still without result.

Thanks in advance.

Eric Xue
  • 117
  • 1
  • 9
amine jafur
  • 123
  • 1
  • 1
  • 6

2 Answers2

23

Short answer:

AnimationController _controller = ...;

// To stop animation
_controller.stop();

// To start from beginning
_controller.forward();

// To start from a point other than the very beginning.
_controller.forward(from: 0.8);


Long answer:

I wasn't aware of that code, here is how I did. All you need is Controller.reset() to stop the animation and Controller.repeat() to start it.

However if you need to start the animation just once, use Controller.forward() and Controller.reverse().

enter image description here

void main() => runApp(MaterialApp(home: Scaffold(body: HomePage())));

class HomePage extends StatefulWidget {
  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> with SingleTickerProviderStateMixin {
  AnimationController _controller;
  bool _isPlaying = true;

  @override
  void initState() {
    super.initState();
    _controller = AnimationController(
      vsync: this,
      lowerBound: 0.3,
      duration: Duration(seconds: 3),
    )..repeat();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text("Animation")),
      body: Stack(
        alignment: Alignment.center,
        children: <Widget>[
          _buildCircularContainer(200),
          _buildCircularContainer(250),
          _buildCircularContainer(300),
          Align(child: CircleAvatar(backgroundImage: AssetImage("assets/images/profile.png"), radius: 72)),
          Align(
            alignment: Alignment(0, 0.5),
            child: RaisedButton(
              child: Text(_isPlaying ? "STOP" : "START"),
              onPressed: () {
                if (_isPlaying) _controller.reset();
                else _controller.repeat();
                setState(() => _isPlaying = !_isPlaying);
              },
            ),
          ),
        ],
      ),
    );
  }

  Widget _buildCircularContainer(double radius) {
    return AnimatedBuilder(
      animation: CurvedAnimation(parent: _controller, curve: Curves.fastLinearToSlowEaseIn),
      builder: (context, child) {
        return Container(
          width: _controller.value * radius,
          height: _controller.value * radius,
          decoration: BoxDecoration(color: Colors.black54.withOpacity(1 - _controller.value), shape: BoxShape.circle),
        );
      },
    );
  }
}
CopsOnRoad
  • 237,138
  • 77
  • 654
  • 440
8

When you directly have access to the AnimationController this snippet will start/stop the animation, wherever it left off.

animationController.isAnimating
    ? animationController.stop()
    : animationController.forward();

Here the .isAnimating property is of type bool and is true when the animationController is animating at the moment. Depending on the result .stop()/.forward() will stop/start the animation respectively.

Jakob Gerhardt
  • 145
  • 1
  • 7
  • But if you stop it and forward, when it value reach to target, forward will not work. Seems animation not support pause & resume , refer this https://github.com/flutter/flutter/issues/3299 – wjploop Dec 14 '21 at 09:59
  • 1
    call repeat() instead of forward() if you want it to continue looping. – Paul Sumpner Jan 27 '22 at 14:19