screenshotFittedScreenRect function

Rect screenshotFittedScreenRect({
  1. required Size imageSize,
  2. required Rect screenRect,
})

Destination rect for a capture inside the device-frame screen hole.

BoxFit.contain keeps the capture's aspect ratio. Mapping a default 800×600 widget-test surface into an iPhone hole with BoxFit.fill stretches the UI.

Implementation

Rect screenshotFittedScreenRect({
  required Size imageSize,
  required Rect screenRect,
}) {
  if (imageSize.isEmpty || screenRect.isEmpty) return screenRect;
  final fitted = applyBoxFit(BoxFit.contain, imageSize, screenRect.size);
  return Alignment.center.inscribe(fitted.destination, screenRect);
}