我正试着给这个页面添加一个按钮,它将在背景中播放或暂停海浪动画. 编码链接:https://github.com/apgapg/flutter_profile/blob/master/lib/page/intro_page.dart

我试过很多方法,但是我还是不擅长拍打动画,所以还是没有结果.

提前谢谢.

推荐答案

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:

我没有意识到这个代码,以下是我是如何做到的.您只需要Controller.reset()来停止动画,Controller.repeat()来开始动画.

但是,如果只需要启动一次动画,请使用Controller.forward()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),
        );
      },
    );
  }
}

Dart相关问答推荐

使用arm64v8/DART Docker映像进行编译时不支持DART镜像

StreamBuilder 会在无状态小部件中自动关闭流吗?

Flutter:将照片或图像显示为模态弹出窗口

如何限制TextSpan小部件的文本长度

在 Flutter 中保持响应的同时制作持久的背景图像

Flutter Null 安全迁移说Package doesn't exist

如何在控件边框(border()/阴影(shadow)中添加霓虹灯效果?

如何在Dart中按布尔对列表进行排序

Flutter ToggleButton 类 - Flutter 1.9.1

如何从类中访问元数据注释?

Flutter 在 main 中读取共享首选项,然后决定哪个启动页面?

如何将 Stream 转换为 List

Dart null 安全性不适用于类字段

VS Code 首选项的位置

在 Dart 中,将动态转换为给定类型或返回 null 的语法好方法?

具有 Maps 的 Null 感知运算符

如何在 Dart 中从外部查询 shadow DOM 中的元素?

如何在 Dart 中扁平化map列表?

Dart 中的外部是什么意思?

Dart 中的错误与异常