2

So I created a custom NavBar, when you click on the icons it runs the animations for the icon.

Problem: When starting the application the animation of a specific icon should be executed. For that i looked for a method that runs on Widget build complete.

Code I found:

void initState() {
super.initState();
WidgetsBinding.instance
    .addPostFrameCallback((_) => yourFunction(context));}

My code:

class NavBar extends StatefulWidget {
  NavBar({Key? key}) : super(key: key);

  @override
  _NavBarState createState() => _NavBarState();
}

class _NavBarState extends State<NavBar> {
  List<IconSpecs> iconSpecsLst = [
    IconSpecs(name: 'list', height: 69, width: 69),
    IconSpecs(name: 'checkmark', height: 60, width: 60),
    IconSpecs(name: 'settings', height: 60, width: 60),
  ];

  late List<RiveIcon> icons;

  @override
  void initState() {
    super.initState();
    icons = iconSpecsLst
        .map(
          (object) => RiveIcon(
            iconSpecs: object,
          ),
        )
        .toList();
    WidgetsBinding.instance!
        .addPostFrameCallback((_) => setAnimation(icons, 2));
  }

  setAnimation(List<RiveIcon> icons, int active) {
    icons[active].activeInput?.value = true;

    for (int i = 0; i < icons.length; i++) {
      if (i != active) {
        icons[i].activeInput?.value = false;
      }
    }
  }
   

I added it to my code but it doesn't get executed. I don't have any error message.

1 Answer 1

2

The callback isn't called if there's no next frame drawn.

To force draw the next frame, call ensureVisualUpdate:

WidgetsBinding.instance!.addPostFrameCallback((_) {
  setAnimation(icons, 2);
});

WidgetsBinding.instance!.ensureVisualUpdate();
Sign up to request clarification or add additional context in comments.

1 Comment

ensureVisualUpdate interesting

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.