Back to Articles

Intermediate

Responsive Design in Flutter: LayoutBuilder and MediaQuery

· 7 min read

Responsive Design in Flutter: LayoutBuilder and MediaQuery

Why Responsive Design?

One of Flutter's biggest strengths is running from a single codebase on phones, tablets, desktop and the web. But reusing the exact same layout on every screen usually makes for a poor experience: a single-column list that looks great on a phone turns into a narrow strip surrounded by huge empty margins on desktop. Responsive design means deciding which widget tree to build based on the available width.

Cards switching from a Column layout to a Row layout as the width changes
LayoutBuilder switching from Column to Row once the breakpoint is crossed.

Reading Screen Size with MediaQuery.sizeOf

To read the full screen size, use MediaQuery.sizeOf(context). Prefer it over the older MediaQuery.of(context).size, because sizeOf only rebuilds the widget when the size actually changes; of(context) also triggers unnecessary rebuilds whenever any other MediaQuery value changes, such as theme, text scale factor or padding.

Widget build(BuildContext context) {
  final width = MediaQuery.sizeOf(context).width;
  final isWide = width >= 900;
  return isWide ? const _WideLayout() : const _NarrowLayout();
}

LayoutBuilder: The Space the Parent Gives You

MediaQuery's size gives you the whole screen. But if a widget sits inside a Row, a side panel or a fixed-width Container, what matters isn't the screen but the space allotted to that widget. LayoutBuilder exposes the parent's minimum/maximum width and height directly through constraints, which makes it more reliable than MediaQuery for nested components.

LayoutBuilder(
  builder: (context, constraints) {
    if (constraints.maxWidth >= 600) {
      return const _DetailGrid(crossAxisCount: 3);
    }
    return const _DetailGrid(crossAxisCount: 1);
  },
)

OrientationBuilder

Sometimes what matters is not width but the device's orientation — for example showing two columns on a tablet in landscape and one column in portrait. OrientationBuilder hands Orientation.portrait and Orientation.landscape straight to the builder and manages the dependency on MediaQuery's orientation field for you.

A Real Example: Adaptive Page Layout

class ProductPage extends StatelessWidget {
  const ProductPage({super.key});

  @override
  Widget build(BuildContext context) {
    return LayoutBuilder(
      builder: (context, constraints) {
        final isWide = constraints.maxWidth >= 840;
        const image = _ProductImage();
        const details = _ProductDetails();
        if (isWide) {
          return const Row(
            children: [
              Expanded(child: image),
              Expanded(child: details),
            ],
          );
        }
        return const Column(
          children: [image, details],
        );
      },
    );
  }
}

Things to Watch Out For

• Use MediaQuery.sizeOf(context) instead of MediaQuery.of(context).size, or you'll trigger rebuilds on unrelated changes too. • LayoutBuilder's constraints come directly from the parent's allotted space, not the screen size — don't use the two interchangeably. • Define breakpoint thresholds (e.g. 600, 900) in a single place (an enum or a constants class) instead of scattering magic numbers across the project. • Nesting too many LayoutBuilders adds build cost; where possible, keep the decision point at the top of the page.

Conclusion

Responsive design isn't a single trick — it's about using the right tool in the right place: MediaQuery.sizeOf for the screen itself, LayoutBuilder for the space given to a widget, and OrientationBuilder for device orientation. Combine the three correctly and the same codebase feels at home on both a phone and a desktop.