Sub topic 4
Reading Window Information with MediaQuery
Assume you know basic Dart, widget trees, Scaffold, Row, Column, and ListView. This chapter uses Dart 3 syntax and assumes you can run a Flutter app on a device, emulator, desktop target, or the web.
A Flutter app can receive different amounts of space as the device rotates, a desktop window is resized, or a software keyboard appears. MediaQuery is an inherited widget that exposes information about the current application window and display environment. You can use it to make decisions that depend on the window rather than on one widget's local constraints.
Reading the window size
Use MediaQuery.sizeOf(context) when a layout decision depends on the window's logical size. Logical pixels are Flutter's device-independent measurement unit.
import 'package:flutter/material.dart';
class WindowSummary extends StatelessWidget {
const WindowSummary({super.key});
@override
Widget build(BuildContext context) {
final size = MediaQuery.sizeOf(context);
final isWide = size.width >= 600;
return Scaffold(
body: Center(
child: Text(
isWide
? 'Wide window: ${size.width} × ${size.height}'
: 'Narrow window: ${size.width} × ${size.height}',
),
),
);
}
}size.width and size.height describe the window available to the app. They do not describe the size of the current widget. For example, a card inside a wide window might be only 300 logical pixels wide. Use LayoutBuilder when the decision must depend on that card's local width.
Prefer MediaQuery.sizeOf(context) for size-only decisions. Older code often uses MediaQuery.of(context).size. The newer method communicates that the widget only depends on the size, which can reduce unnecessary rebuilds when other window properties change.
Insets, keyboards, and text scaling
MediaQuery also reports areas that your content should avoid.
paddingcommonly represents system areas such as a status bar, notch, or home indicator.viewInsetsrepresents areas covered by temporary system UI, especially the on-screen keyboard.textScalerdescribes the user's preferred text scale.
SafeArea uses the relevant padding automatically:
class ProfilePage extends StatelessWidget {
const ProfilePage({super.key});
@override
Widget build(BuildContext context) {
final keyboardBottom = MediaQuery.viewInsetsOf(context).bottom;
return Scaffold(
body: SafeArea(
child: Padding(
padding: EdgeInsets.only(bottom: keyboardBottom),
child: const TextField(
decoration: InputDecoration(labelText: 'Name'),
),
),
),
);
}
}Use insets deliberately. A Scaffold and SafeArea may already account for some system areas, so adding the same padding again can create excessive gaps. Test with a keyboard open, a notch, and a desktop window.
Exercise
A tablet window is 800 logical pixels wide, but a panel inside it receives only 360 logical pixels. Which tool should decide whether the panel changes layout?
Show answer
Use LayoutBuilder because the decision depends on the panel's local constraints. Use MediaQuery when the decision depends on the whole window.