1

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?

0

2 Answers 2

1

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()
    }
  }
Sign up to request clarification or add additional context in comments.

Comments

0

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();
  }
}

Comments

Your Answer

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