'"WidgetsBinding.instance!.addPostFrameCallback((_) => yourFunction(context));" does not execute
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.
Solution 1:[1]
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();
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |