如何在每次点击时播放相同的Flare动画?

时间:2019-04-06 20:39:58

标签: flutter flare

仅当以这种方式点击对象时,我才能运行动画吗?

class _MyHomePageState extends State<MyHomePage> {
  bool isOpen = false;

  @override
  Widget build(BuildContext context) {
    return Center(
      child: GestureDetector(
        onTap: () {
          setState(() {
            isOpen = !isOpen;
          });
        },
        child: FlareActor('assets/heart.flr',
            animation: isOpen ? 'run' : 'x'),
      ),
    );
   }
  }

但是我需要的是在每次点击时都运行相同的动画,这是行不通的:

GestureDetector(
        onTap: () {
          setState(() {
          });
        },
        child: FlareActor('assets/heart.flr',
            animation: 'run'),
      )

我也尝试过:

GestureDetector(
        onTap: () {
          setState(() {
            isOpen = !isOpen;
          });
        },
        child: FlareActor('assets/heart.flr',
            animation: isOpen ? 'run' : 'run'),
      )

1 个答案:

答案 0 :(得分:0)

我最近在实施某些东西时遇到了同样的问题。我的问题得到了解答,您可以使用FlareControls并只调用play而不是使用键来播放动画。 Original post

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("run");
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: FlareActor("assets/heart.flr",
          animation: "run",
          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),
      ),
    );
  }
}

我也有a video解释了如何整合这些动画并重播相同的动画。