【问题标题】:Play & pause a Flutter animation播放和暂停 Flutter 动画
【发布时间】:2019-08-28 11:23:29
【问题描述】:

我试图向此页面添加一个按钮,该按钮将(播放或暂停)背景中的波浪动画。 代码链接:https://github.com/apgapg/flutter_profile/blob/master/lib/page/intro_page.dart

我尝试了很多东西,但由于我对颤动动画仍然很糟糕,所以我仍然没有结果。

提前致谢。

【问题讨论】:

    标签: android flutter dart animation flutter-animation


    【解决方案1】:

    简答:

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

    长答案:

    我不知道该代码,这就是我所做的。您只需要Controller.reset() 停止动画和Controller.repeat() 启动它。

    但是,如果您只需要启动一次动画,请使用 Controller.forward()Controller.reverse()

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

    【讨论】:

      【解决方案2】:

      当您直接访问 AnimationController 时,此 sn-p 将启动/停止动画,无论它从哪里停止。

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

      这里的.isAnimating 属性是bool 类型,并且当animationController 正在制作动画时为真。根据结果​​.stop()/.forward() 将分别停止/启动动画。

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-09-13
      • 1970-01-01
      • 1970-01-01
      • 2019-01-26
      • 2012-02-09
      • 2013-02-28
      • 2021-12-07
      相关资源
      最近更新 更多