A layout that looks great on a phone can look stretched and empty on a tablet or in a browser. Flutter runs on all of them from one codebase, so your UI needs to adapt. In this post you will build a layout that shows one column on mobile, two boxes side by side on tablet, and three on desktop and web, using MediaQuery.
What MediaQuery gives you
MediaQuery describes the screen your app is drawn on. The values you will use most:
| Method | Returns |
|---|---|
MediaQuery.sizeOf(context) | Screen width and height in logical pixels |
MediaQuery.orientationOf(context) | Orientation.portrait or Orientation.landscape |
MediaQuery.paddingOf(context) | Safe-area insets (notch, status bar) |
MediaQuery.viewInsetsOf(context) | Space covered by the keyboard |
Step 1: Define your breakpoints
Breakpoints are the widths where your layout switches. Put them in one place so every screen uses the same rules:
import 'package:flutter/material.dart';
enum DeviceType { mobile, tablet, desktop }
class Breakpoints {
static const double tablet = 600;
static const double desktop = 1024;
static DeviceType of(BuildContext context) {
final width = MediaQuery.sizeOf(context).width;
if (width >= desktop) return DeviceType.desktop;
if (width >= tablet) return DeviceType.tablet;
return DeviceType.mobile;
}
}600 and 1024 are common starting points, and 600 matches Material Design's compact/medium split. Adjust them to your design.
Step 2: Build a reusable ResponsiveLayout widget
Instead of writing if (width > ...) in every screen, wrap the decision in a widget:
class ResponsiveLayout extends StatelessWidget {
const ResponsiveLayout({
super.key,
required this.mobile,
this.tablet,
this.desktop,
});
final Widget mobile;
final Widget? tablet;
final Widget? desktop;
@override
Widget build(BuildContext context) {
switch (Breakpoints.of(context)) {
case DeviceType.desktop:
return desktop ?? tablet ?? mobile;
case DeviceType.tablet:
return tablet ?? mobile;
case DeviceType.mobile:
return mobile;
}
}
}Only mobile is required. If you skip desktop, the tablet layout is used, and if you skip both, mobile is used everywhere. That lets you add bigger layouts one screen at a time.
Step 3: Use it in a screen
Here is the demo from the video: three coloured boxes stacked on mobile, two in a row on tablet, and three in a row on desktop.
class HomeScreen extends StatelessWidget {
const HomeScreen({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Responsive Using MediaQuery')),
body: const ResponsiveLayout(
mobile: _BoxColumn(),
tablet: _BoxRow(count: 2),
desktop: _BoxRow(count: 3),
),
);
}
}
const _colors = [Colors.red, Colors.blue, Colors.orange];
class _Box extends StatelessWidget {
const _Box(this.index);
final int index;
@override
Widget build(BuildContext context) {
return Container(
margin: const EdgeInsets.all(8),
color: _colors[index],
alignment: Alignment.center,
child: Text('Box ${index + 1}', style: const TextStyle(color: Colors.white)),
);
}
}
class _BoxColumn extends StatelessWidget {
const _BoxColumn();
@override
Widget build(BuildContext context) {
return Column(
children: [
for (var i = 0; i < 3; i++) Expanded(child: _Box(i)),
],
);
}
}
class _BoxRow extends StatelessWidget {
const _BoxRow({required this.count});
final int count;
@override
Widget build(BuildContext context) {
return Row(
children: [
for (var i = 0; i < count; i++) Expanded(child: _Box(i)),
],
);
}
}Run it on the web (flutter run -d chrome) and resize the browser window. The layout switches as you cross 600 and 1024 pixels, with no restart needed.
Sizing relative to the screen
Sometimes you want a widget to be a fraction of the screen, for example a banner that is 30% of the height in portrait and 60% in landscape:
final size = MediaQuery.sizeOf(context);
final isLandscape =
MediaQuery.orientationOf(context) == Orientation.landscape;
return SizedBox(
width: size.width * 0.9,
height: isLandscape ? size.height * 0.6 : size.height * 0.3,
child: const BannerImage(),
);Use this sparingly. Most of the time Expanded, Flexible and padding give better results than percentages, because they respect the space the parent actually has.
Handling the keyboard and the notch
MediaQuery also tells you about things that cover your UI:
final keyboardHeight = MediaQuery.viewInsetsOf(context).bottom;
final safeTop = MediaQuery.paddingOf(context).top;viewInsetsOf(context).bottom is greater than zero while the keyboard is open, which helps you move a bottom button above it. For the notch and status bar, wrapping content in SafeArea is usually simpler than reading paddingOf yourself.
MediaQuery vs LayoutBuilder
MediaQuery gives you the size of the whole screen. LayoutBuilder gives you the size of the space your widget was given by its parent.
LayoutBuilder(
builder: (context, constraints) {
if (constraints.maxWidth > 400) {
return const ProductCardWide();
}
return const ProductCardNarrow();
},
)Use MediaQuery for page-level decisions, like showing a side navigation rail on desktop. Use LayoutBuilder for reusable components, like a card that can sit in a narrow sidebar or a wide main area, because the screen width tells you nothing about how much room that card actually has.
Bonus: keep web content readable
On a wide monitor, text that runs the full width of the screen is hard to read. Cap the content width:
Center(
child: ConstrainedBox(
constraints: const BoxConstraints(maxWidth: 1200),
child: child,
),
)Common mistakes
- Calling MediaQuery in
initState. The context is not ready yet, and the value will not update when the window resizes. Read it inbuild(ordidChangeDependencies). - Checking platform instead of size.
Platform.isAndroidsays nothing about screen size. Android runs on tablets and foldables too, anddart:io'sPlatformis not available on web. Decide layouts by width. - Using screen width inside a component. A card inside a 300px sidebar on a 1400px screen will think it has 1400px. Use
LayoutBuilderthere. - Hard-coding pixel sizes everywhere.
width: 360overflows on small phones. LetExpanded,FlexibleandWrapshare the space. - Using
MediaQuery.of(context).size. It works, but causes extra rebuilds. Switch tosizeOf.
Wrapping up
- Define breakpoints once and read the width with
MediaQuery.sizeOf(context). - Wrap the decision in a
ResponsiveLayoutwidget so each screen just passes its mobile, tablet and desktop versions. - Use
LayoutBuilderfor components that need to fit the space they are given.
Watch the full walkthrough in the video above, and see more Flutter tutorials.


