Reputation:
I have a scroll controller which checks if the scroll position passed the threshold and then call the function:
final _scrollController = ScrollController();
final _scrollThreshold = 200.0;
_scrollController.addListener(_onScroll);
void _onScroll() {
final maxScroll = _scrollController.position.maxScrollExtent;
final currentScroll = _scrollController.position.pixels;
if (maxScroll - currentScroll <= _scrollThreshold) {
dummyFunction();
}
}
Currently, dummyFunction() is called multiple times, How to make sure it is going to be called once?
Upvotes: 1
Views: 1607
Reputation: 2717
I am not sure what you are trying to achieve exactly, but I think you could use controller.position
, which is the currently visible segment of the scrollable. This variable contains info about its position inside the scrollable, such as extentBefore
and extentAfter
. extentAfter
represents the remaining scroll space available, so you could do something like this to trigger your function:
void _onScroll() {
if (controller.position.extentAfter < someThreshold) {
_dummyFunction()
}
}
Upvotes: 1
Reputation: 7799
Use a boolean
storing wheather the function was called or not :
final _scrollController = ScrollController();
final _scrollThreshold = 200.0;
_scrollController.addListener(_onScroll);
final _hasReachedPoint = false;
void _onScroll() {
final maxScroll = _scrollController.position.maxScrollExtent;
final currentScroll = _scrollController.position.pixels;
if (maxScroll - currentScroll <= _scrollThreshold && !_hasReachedPoint) {
_hasReachedPoint = true;
dummyFunction();
}
}
Upvotes: 0