Skip to content
Tutorials Center
My learning

Topics

Lesson 19 of 73 ยท Layout

Responsive and Adaptive Layouts

Make one Flutter app fit phones, tablets, foldables, desktop and web: MediaQuery, LayoutBuilder, breakpoints, OrientationBuilder, SafeArea and adaptive widgets.

A Flutter app can run on a 5-inch phone, a tablet in landscape, a foldable, a desktop window you can drag to any size, and a browser tab. If you hard-code width: 360 somewhere, it will break on at least one of them. Good layouts respond to the space they have.

Two words are often mixed up. A responsive layout changes with the size of the space available (more columns when wider). An adaptive layout changes with the platform or input method (a different dialog style on iOS, hover effects with a mouse, a navigation rail instead of a bottom bar). Most real apps need both. This lesson covers the tools: MediaQuery, LayoutBuilder, breakpoints, OrientationBuilder, SafeArea, adaptive widgets and foldables. It builds on Constraints go down, sizes go up and on BuildContext as a position in the tree (BuildContext and InheritedWidget).

Real vs stand-in. MediaQuery, LayoutBuilder and the others are real Flutter and the Dart code is read-only. The live examples are JavaScript stand-ins that pick a layout from a window size and from parent constraints, the same decisions your build methods make.

Responsive or adaptive?

Responsive Reacts to the available size.
Adaptive Reacts to platform and input.
1 / 2

MediaQuery: the whole window

MediaQuery is an InheritedWidget provided by MaterialApp (more precisely by WidgetsApp). It describes the window the app runs in:

DART
final size = MediaQuery.sizeOf(context);                  // Size(390, 844), logical pixels
final isLandscape = MediaQuery.orientationOf(context) == Orientation.landscape;
final topInset = MediaQuery.paddingOf(context).top;       // status bar / notch
final keyboard = MediaQuery.viewInsetsOf(context).bottom; // on-screen keyboard height
final scale = MediaQuery.textScalerOf(context);           // user's font scaling
final dpr = MediaQuery.devicePixelRatioOf(context);

Prefer the specific accessors (sizeOf, paddingOf, ...) over MediaQuery.of(context).size. of subscribes the widget to every change in the media data, including the keyboard opening, so the widget rebuilds far more often than necessary. The specific methods subscribe to just one property.

Sizes are in logical pixels, a device-independent unit that is about the same physical size everywhere. The device pixel ratio converts them to physical pixels.

MediaQuery tells you about the window, not about the space your own widget got. A small card in a side panel on a wide desktop window gets a large MediaQuery width but a narrow parent. Sizing a component from the screen width is a common mistake, and it is the reason LayoutBuilder exists.

LayoutBuilder: my parent's space

LayoutBuilder hands you the constraints from its parent, so a widget can adapt to the room it was really given:

DART
LayoutBuilder(
  builder: (context, constraints) {
    if (constraints.maxWidth >= 600) {
      return Row(children: [
        const SizedBox(width: 240, child: Sidebar()),
        const Expanded(child: Content()),
      ]);
    }
    return const Content();
  },
)

The builder re-runs whenever the incoming constraints change, for example when the user resizes a desktop window or rotates a phone. Because the decision uses the parent's constraints, the same widget works on a full page, in a split view or in a small dialog. Two cautions: do not return a widget that needs unbounded space unless you handled it, and remember that the builder runs during layout, so avoid expensive work or setState inside it.

Breakpoints

A breakpoint is a width at which the layout changes. Material 3 defines window size classes you can adopt as constants:

Class Width (logical px) Typical devices Typical navigation
compact under 600 most phones in portrait bottom NavigationBar
medium 600 to 839 tablets, unfolded foldables NavigationRail
expanded 840 and up large tablets, desktop permanent drawer or rail plus a detail pane

Keep the breakpoints in one place, and return a layout rather than sprinkling width > 600 checks through your widgets:

DART
enum WindowSize { compact, medium, expanded }

WindowSize windowSizeFor(double width) {
  if (width < 600) return WindowSize.compact;
  if (width < 840) return WindowSize.medium;
  return WindowSize.expanded;
}

A typical list-detail screen shows only the list on a phone (tapping an item navigates to the detail page, see Navigator and Routes), and the list and detail side by side on a tablet. The same widgets are reused; only their arrangement changes.

JS Choose a layout from the width

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Note that a phone in landscape (844 wide) lands in the expanded class, even though the screen is short. That is one reason many teams combine the width class with a height check, or design landscape phones as their own case.

Component-level decisions

The stand-in below makes the same kind of decision as a LayoutBuilder: a card that is laid out horizontally when its own parent is wide, regardless of the window size.

JS LayoutBuilder reasons about the parent, not the window

Press Run (or Ctrl + Enter) to see the console output here.

No output.

OrientationBuilder

OrientationBuilder rebuilds when the parent is wider than tall (landscape) or the reverse (portrait). It is a quick way to switch a Column to a Row or change the number of grid columns:

DART
OrientationBuilder(
  builder: (context, orientation) => GridView.count(
    crossAxisCount: orientation == Orientation.portrait ? 2 : 4,
    children: tiles,
  ),
)

Note that this is based on the parent's shape, not on the device rotation. On desktop and in split-screen modes, "landscape" simply means "wider than tall". If your app must lock its orientation, use SystemChrome.setPreferredOrientations, but think twice: forcing portrait on large screens and foldables is considered a poor experience and some app stores warn about it.

SafeArea and system UI

Phones have notches, camera cut-outs, rounded corners, status bars and gesture bars. SafeArea adds padding so your content is not hidden by them, using the values from MediaQuery.padding:

DART
SafeArea(
  child: Column(children: [/* ... */]),
)

SafeArea(top: false, child: bottomButtons)   // only protect the bottom edge

Scaffold with an AppBar already keeps the body below the bar, so you mostly need SafeArea for screens without one or for content that goes edge to edge. A good pattern for images or backgrounds is to let them extend behind the system bars while keeping text and controls inside a SafeArea. For the keyboard, MediaQuery.viewInsetsOf(context).bottom is the height it occupies; a Scaffold resizes its body automatically, and a SingleChildScrollView keeps a form reachable.

Adaptive widgets and platform differences

Adaptive means respecting what the user's platform does. Flutter offers several constructors that pick the native style:

DART
Switch.adaptive(value: on, onChanged: setOn)          // iOS-style switch on Apple platforms
CircularProgressIndicator.adaptive()
Slider.adaptive(value: v, onChanged: setV)
showAdaptiveDialog(context: context, builder: (_) => const AlertDialog(title: Text('Delete?')))

You can branch on Theme.of(context).platform (it respects theme overrides and works in tests), or on defaultTargetPlatform. Avoid dart:io's Platform.isAndroid in shared code: it does not exist on the web. Beyond looks, think about input: on desktop and web add hover and focus states, keyboard shortcuts (Shortcuts, Actions) and scrollbars; on touch devices make tap targets at least 48 by 48 logical pixels. Flutter on Web and Desktop goes further.

Other things that scale with the user, not the device: text scale (people set bigger fonts, so avoid fixed-height text containers, see Accessibility and Internationalization) and display zoom.

Foldables and display features

Foldable phones can show your app across a hinge or in two panels. MediaQuery.displayFeaturesOf(context) returns a list of DisplayFeatures: the bounds of a fold or hinge, its type (fold, hinge, cutout) and its state (postureFlat, postureHalfOpened). Two practical rules:

  1. Do not place important controls under a hinge; wrap content in DisplayFeatureSubScreen to keep it on one side.
  2. Use a two-pane layout (list on one half, detail on the other) when the window is in the expanded class. The two_pane package from Microsoft does this split for you.

Also remember that a window can change size at any time without the app restarting, on foldables, in multi-window mode and on desktop. Never read the size once in initState and keep it; read it in build, where Flutter re-runs your code whenever it changes.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. A product card should lay out horizontally when its own container is wide, even on a phone-sized window. Which tool fits best?

    LayoutBuilder provides the constraints from the parent, i.e. the space the component really has. MediaQuery describes the whole window.
  2. 2. Why prefer MediaQuery.sizeOf(context) over MediaQuery.of(context).size?

    MediaQuery.of subscribes to all media data (keyboard, padding, ...). The specific accessors rebuild only when their property changes.
  3. 3. Which is a good use of SafeArea?

    SafeArea adds padding equal to the system UI insets.
  4. 4. On a Material 3 window of 700 logical pixels wide, which size class applies and what navigation is typical?

    600 to 839 px is the medium class, usually a NavigationRail.
  5. 5. Why should you avoid Platform.isAndroid from dart:io in shared widget code?

    dart:io is unavailable in web builds. Theme.of(context).platform and defaultTargetPlatform work everywhere.

Summary

  • Responsive layouts react to available size, adaptive ones to platform and input; use both.
  • MediaQuery describes the window (use sizeOf, paddingOf, ...); LayoutBuilder describes the parent's constraints and is best for components.
  • Keep breakpoints (compact under 600, medium 600 to 839, expanded 840 and up) in one function and choose a layout, such as bottom bar, rail or two panes.
  • OrientationBuilder, SafeArea and adaptive constructors handle shape, system UI and native feel; displayFeatures helps on foldables.
  • Never read the size once and cache it: windows resize while the app runs.

Next: Material 3 Widgets Tour