forSurface method

OverlaySafeArea forSurface(
  1. BuildContext surface
)

Re-expresses these insets relative to surface.

The insets are measured against the screen, but an OverlayEntry is positioned against the Overlay it is inserted into — and that overlay does not have to start at the top of the screen. A nested Navigator inside a Scaffold body, for example, owns an overlay that already begins below the app bar:

Scaffold(
  appBar: AppBar(...),   // 156 tall
  body: Navigator(...),  // its overlay starts at y = 156
)

Applying a screen-measured inset of 156 there would clear the app bar a second time and leave a 156px gap. Subtracting how far the surface already sits down the screen keeps both rulers aligned.

Implementation

OverlaySafeArea forSurface(BuildContext surface) {
  final box = surface.findRenderObject() as RenderBox?;
  if (box == null || !box.hasSize) return this;

  final origin = box.localToGlobal(Offset.zero);
  final view = View.maybeOf(surface);
  if (view == null) return this;

  final screen = view.physicalSize / view.devicePixelRatio;
  final bottomGap = screen.height - (origin.dy + box.size.height);
  final rightGap = screen.width - (origin.dx + box.size.width);

  return OverlaySafeArea(
    top: _shrink(top, origin.dy),
    bottom: _shrink(bottom, bottomGap),
    left: _shrink(left, origin.dx),
    right: _shrink(right, rightGap),
  );
}