track method

bool track(
  1. Element element, {
  2. OnElementEvent? onEnterViewport,
  3. OnElementEvent? onLeaveViewport,
  4. bool periodicTracking = false,
})

Tracks element if it's visible in viewport.

Useful to track when an element is visible for the 1st time, usually due scrolling.

element The element to track onEnterViewport Callback to call when element shows up in viewport. onLeaveViewport Callback to call when element leaves viewport. periodicTracking If true this tracking will continue after first event.

Implementation

bool track(Element element,
    {OnElementEvent? onEnterViewport,
    OnElementEvent? onLeaveViewport,
    bool periodicTracking = false}) {
  if ((onEnterViewport == null && onLeaveViewport == null)) {
    return false;
  }

  var initValue = _trackElementValue
      .track<bool>(element, (elem) => isInViewport(elem), (elem, show) {
    if (show!) {
      _trackElementValue.setProperty(element, 'viewport', true);
      if (onEnterViewport != null) onEnterViewport(element);
      return periodicTracking || onLeaveViewport != null;
    } else {
      var alreadyViewed =
          _trackElementValue.getProperty(element, 'viewport') ?? false;
      if (onLeaveViewport != null) onLeaveViewport(element);
      return !(alreadyViewed as bool) || periodicTracking;
    }
  });

  return initValue == true;
}