flutter - 在 Flutter 中重播相同的 Flare 动画

标签 flutter flutter-layout flutter-animation flare

我正在尝试在 Flutter 中重新播放 Flare 动画。动画完成后不循环播放。我想要一个按需播放的动画,同样的动画。

当我在动画之间切换时,只要交换字符串并调用 setState 就可以正常工作。有没有一种简单的方法可以做到这一点。

这是我目前正在做的事情。

class _FlareDemoState extends State<FlareDemo> {

  String animationToPlay = 'activate';

  @override
  Widget build(BuildContext context) {

    print('Animation to play: $animationToPlay');

    return Scaffold(
      backgroundColor: Colors.purple,
      body: GestureDetector(
        onTap: () {
          setState(() {

          });
        },
        child: FlareActor('assets/button-animation.flr',
            animation: animationToPlay)));
   }
}

当我点击动画时我的日志结果

I/flutter (18959): Animation to play: activate
I/flutter (18959): Animation to play: activate
I/chatty  (18959): uid=10088(com.example.flare_tutorial) Thread-2 identical 2 lines
I/flutter (18959): Animation to play: activate
I/flutter (18959): Animation to play: activate
I/chatty  (18959): uid=10088(com.example.flare_tutorial) Thread-2 identical 7 lines
I/flutter (18959): Animation to play: activate
I/flutter (18959): Animation to play: activate
Reloaded 2 of 495 libraries in 672ms.
I/flutter (18959): Animation to play: activate

一切都被调用,它第一次播放,但之后动画不再重播。

最佳答案

更简洁的方法是使用自定义 FlareController。有一个具体的 FlareControls 实现非常适合这个用例。

class _MyHomePageState extends State<MyHomePage> {
  // Store a reference to some controls for the Flare widget
  final FlareControls controls = FlareControls();

  void _playSuccessAnimation() {
    // Use the controls to trigger an animation.
    controls.play("success");
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: FlareActor("assets/Teddy.flr",
          animation: "idle",
          fit: BoxFit.contain,
          alignment: Alignment.center,
          // Make sure you use the controls with the Flare Actor widget.
          controller: controls),
      floatingActionButton: FloatingActionButton(
        onPressed: _playSuccessAnimation,
        tooltip: 'Play',
        child: Icon(Icons.play_arrow),
      ),
    );
  }
}

请注意,此示例还会播放循环播放的闲置背景动画。对 FlareControls.play 的任何调用都会将传入的动画混合到这个背景空闲动画上。如果您不想/不需要背景动画,您只需省略 animation: "idle"参数即可。

完整示例在这里:https://github.com/luigi-rosso/flare_controls

关于flutter - 在 Flutter 中重播相同的 Flare 动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55301571/

相关文章:

flutter - 如何在 Sweep Gradient 中包含 Stroke Cap?

flutter - 我怎样才能使自定义粘性 header 中的 header 相互粘在下面而不是在 flutter 中重叠?

flutter - 在 flutter 中捏合缩放 ListView

flutter - 如何在 flutter for windows 中获取主机名?

flutter - 检查 ThemeMode.system 是深色还是浅色

keyboard - flutter 数字键盘没有完成按钮

android - 如何在一定时间后在 Flutter 中切换 Widgets?

dart - 重定向的构造函数具有不兼容的参数

flutter - body2 已弃用,不应使用。这是 2014 版 Material 设计中使用的术语。 - flutter 中的警告消息

flutter - AnimatedContainer 按钮类未设置动画