Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

onInit called every setState on parent widget #277

Closed
ekasetiawans opened this issue Dec 22, 2022 · 2 comments
Closed

onInit called every setState on parent widget #277

ekasetiawans opened this issue Dec 22, 2022 · 2 comments
Labels
bug Something isn't working

Comments

@ekasetiawans
Copy link

ekasetiawans commented Dec 22, 2022

<-- Thanks for using Rive!

Description

<-- A clear and concise description of what the problem is. -->
onInit called every setState on parent widget

Steps To Reproduce

Steps to reproduce the behavior:

  1. Create a custom widget that returns RiveAnimation.asset
@override
  Widget build(BuildContext context) {
    return RiveAnimation.asset(
      widget.icon,
      stateMachines: const ['mode'],
      fit: BoxFit.contain,
      onInit: (artboard){
        print('test');
      },
    );
  }
  1. Put the widget in any StatefulWidget
class HomePage extends StatefulWidget {
  ...
}

class HomePageState extends State<HomePage> {
...
@override
Widget build(BuildContext context) {
  return Column(
    children: [
      ElevatedButton(
        onPressed: (){
          setState((){});
        },
        child: Text('Set State'),
      ),
      YourRiveAnimationWidget(),
    ]
  );
}
}
  1. Every we tap the Set State button will print test on the debug console. That's mean onInit method is called every setState called on it's parent widget.

Expected behavior

<-- A clear and concise description of what you expected to happen. -->
onInit method called once

Device & Versions (please complete the following information)

  • Device: [e.g. Android Emulator, iOS Simulator]
  • OS: [e.g. Android SDK API Level 30, iOS 14.5]
  • Flutter Version: Copy-paste the output of flutter --version
    e.g.:
Flutter 3.3.10 • channel stable • https://github.com/flutter/flutter.git
Framework • revision 135454af32 (7 days ago) • 2022-12-15 07:36:55 -0800
Engine • revision 3316dd8728
Tools • Dart 2.18.6 • DevTools 2.15.0

Additional context

<-- Add any other context about the problem here. -->
I have workaround, that the custom widget must be StatefulWidget, and declare onInit method as member of the state class.

@override
  Widget build(BuildContext context) {
    return RiveAnimation.asset(
      widget.icon,
      stateMachines: const ['mode'],
      fit: BoxFit.contain,
      onInit: _onInit,
    );
  }

void _onInit(artboard) {
  print('this will be printed once');
}
@ekasetiawans ekasetiawans added the bug Something isn't working label Dec 22, 2022
@bdezso
Copy link

bdezso commented Dec 28, 2022

There is a serious bug that is causing poor performance in a frequently rebuild context with a rive animation. On an iPhone 13 Pro, the frame rate has dropped from 60 fps to around 20 fps, which is a significant decrease. This is a serious issue that needs to be addressed.

@luigi-rosso
Copy link
Contributor

Thanks for reporting, we're looking into it!

rive-engineering pushed a commit that referenced this issue Jan 10, 2023
This resolves a performance and usability issue where certain conditions would result in Rive needlessly reconfiguring/initializing the Rive artboard (as well as downloading/loading Rive files). If `onInit`, `animations`, `controllers`, or `stateMachines` were passed in as an argument to `RiveAnimation` the above issue is triggered on each widget rebuild. Under certain conditions this could result in an animation constantly restarting, bad performance, or Flutter ending up in a `setState` callback loop.

This PR also clears the list of local controllers each time init is called.

Resolves: #277

This also fixes bugs in our example app
- Play/Pause not working
- One shot animation behaving oddly

Diffs=
9af05d044 docs: update changelog
cb7fd6d14 test: add rive animation onInit tests
107ae16bc refactor: naming and call logic
6857aa691 docs: add additional code docs
f3ba4f015 perf: fix didUpdateWidget configure loop
7d0aaaff3 Adjust RiveAnimation didUpdateWidget condition (#278)
d4c6dd4ab Add more helper functions
6a8f9e249 Fix tess for C++11 and add to github action (#4571)
c8b5fdadd More SIMD features
87f079a10 RawPath::Iter improvements
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants