layoutMediaQueryData function
Rebuilds media for a full-screen surface laid out in a box of box size.
Apps that scale their whole tree — ResponsiveScaledBox, a root
FittedBox, a Transform.scale — lay every route and overlay entry out on
a canvas whose size differs from the screen media describes. Geometry
taken from MediaQuery.size then lands in the wrong place on that canvas:
a corner window stops short of the corner, a full-screen rect does not fill
it. Taking the size from the box, and scaling padding and insets by the same
factor, puts everything back in the canvas's own units.
The correction only applies when box has the screen's shape, which is the
signature of a uniformly scaled screen. A box of any other shape is a region
of the screen (a split pane, a sized slot), whose insets cannot be derived
by ratio, so media is returned unchanged — as it is when the sizes already
agree, which is every app that does not scale its tree.
Implementation
MediaQueryData layoutMediaQueryData(MediaQueryData media, Size box) {
final screen = media.size;
if (!box.isFinite || box.isEmpty || screen.isEmpty || box == screen) {
return media;
}
final scaleX = box.width / screen.width;
final scaleY = box.height / screen.height;
if ((scaleX / scaleY - 1).abs() > _uniformScaleTolerance) return media;
EdgeInsets scaled(EdgeInsets insets) => EdgeInsets.fromLTRB(
insets.left * scaleX,
insets.top * scaleY,
insets.right * scaleX,
insets.bottom * scaleY,
);
return media.copyWith(
size: box,
padding: scaled(media.padding),
viewPadding: scaled(media.viewPadding),
viewInsets: scaled(media.viewInsets),
systemGestureInsets: scaled(media.systemGestureInsets),
);
}