visibleFraction property

double visibleFraction

A fraction in the range [0, 1] that represents what proportion of the widget is visible (assuming rectangular bounding boxes).

0 means not visible; 1 means fully visible.

Implementation

double get visibleFraction {
  final visibleArea = _area(visibleBounds.size);
  final maxVisibleArea = _area(size);

  if (_floatNear(maxVisibleArea, 0)) {
    // Avoid division-by-zero.
    return 0;
  }

  var visibleFraction = visibleArea / maxVisibleArea;

  if (_floatNear(visibleFraction, 0)) {
    visibleFraction = 0;
  } else if (_floatNear(visibleFraction, 1)) {
    // The inexact nature of floating-point arithmetic means that sometimes
    // the visible area might never equal the maximum area (or could even
    // be slightly larger than the maximum).  Snap to the maximum.
    visibleFraction = 1;
  }

  assert(visibleFraction >= 0);
  assert(visibleFraction <= 1);
  return visibleFraction;
}