rive-app / rive-flutter

Flutter runtime for Rive
https://rive.app
MIT License
1.16k stars 180 forks source link

Events don't fire when animation loaded from file #402

Closed FilledStacks closed 1 week ago

FilledStacks commented 1 week ago

Description

Event listeners do not fire when loading a rive animation from a file directly.

Steps To Reproduce

Clone this repo and run it.

Tapping on the button in the middle of the screen should print out an event but it doesn't.

To test that the event is set up properly you can comment out line 42 -> 47 in main.dart then uncomment line 49 -> 64.

When tapping on the button in the middle of the screen you'll see the event printing out.

Source .riv/.rev file

You can download the .riv file here

Expected behavior

Event listeners should fire regardless of how a rive animation is loaded.

Device & Versions (please complete the following information)

HayesGordon commented 1 week ago

Hey!

You're using two separate instances of the Artboard. Basically adding the event listener on an instance that is never passed to the widget. When manually creating the artboard like this, always call instance().

benefitOneArtboard = riveFile.artboardByName('benefit-1-view')!.instance(); // add .instance()

And remove it from the widget creation:

body: Rive(
  artboard: benefitOneArtboard, // remove .instance()
  useArtboardSize: true,
  fit: BoxFit.cover,
  enablePointerEvents: true,
),

You'll also note that the event is triggered twice in the "working" example because you've added it twice: once to the "main" loaded artboard and again on the instanced version.

Closing this issue but let me know if you run into problems.

FilledStacks commented 1 week ago

I knew I was overlooking something silly.

Thanks Gordon!