onScroll method
The listener that is called when the list view is scrolled.
Implementation
void onScroll(ScrollNotification notification) {
// Iterate through each item to check
// whether it is in the viewport
_contexts.forEach((WidgetData item) {
// Retrieve the RenderObject, linked to a specific item
final RenderObject? renderObject = item.context!.findRenderObject();
// If none was to be found, or if not attached, leave by now
if (renderObject == null || !renderObject.attached) {
return;
}
//Retrieve the viewport related to the scroll area
final RenderAbstractViewport viewport =
RenderAbstractViewport.of(renderObject)!;
final double vpHeight = notification.metrics.viewportDimension;
final RevealedOffset vpOffset =
viewport.getOffsetToReveal(renderObject, 0.0);
// Retrieve the dimensions of the item
final Size size = renderObject.semanticBounds.size;
//distance from top of the widget to top of the viewport
final double deltaTop = vpOffset.offset - notification.metrics.pixels;
//distance from bottom of the widget to top of the viewport
final double deltaBottom = deltaTop + size.height;
bool isInViewport = false;
//Check if the item is in the viewport by evaluating the provided widget's isInViewPortCondition condition.
isInViewport = _isInViewCondition!(deltaTop, deltaBottom, vpHeight);
if (isInViewport) {
//prevent changing the value on every scroll if its already the same
if (!_currentInViewIds.contains(item.id)) {
_currentInViewIds.add(item.id);
notifyListeners();
}
} else {
if (_currentInViewIds.contains(item.id)) {
_currentInViewIds.remove(item.id);
notifyListeners();
}
}
});
}