I have a scroll listener for hiding and showing the bottom navigation bar, It works perfectly but I discover it will fire the function almost 50 times and more when it meets the condition if I scroll too fast. Which means it will rebuild the widget so many times right? How can I prevent this from happening.
updateBottomBar
is a callback function to setstate the parent widget.
bottomBarVisible
is the variable to show and hide the bottom nav bar.
@override
void initState() {
scrollController.addListener(() {
//listener
if (scrollController.position.userScrollDirection ==
ScrollDirection.reverse &&
widget.bottomBarVisible == true) {
print("it is hide");
widget.updateBottomBar(false);
} else if (scrollController.position.userScrollDirection ==
ScrollDirection.forward &&
widget.bottomBarVisible == false) {
print("it is show");
widget.updateBottomBar(true);
}
});
2
Answers
I see you are already checking
widget.bottomBarVisible == true
before setting it tofalse
by callingwidget.updateBottomBar(false)
. So that means, it would only call your function once, right?If that’s not the case, maybe you are not updating the
bottomBarVisible
variable quickly enough. I suggest you update the variable as the first thing to do in yourupdateBottomBar()
function, to prevent rapid firing.addListener
work multiple time while scrolling is not finished. You can handle usingatEdge
function that will returntrue
when scrolling finished.sController.position.atEdge