Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Widgets  ›  Capstone Project

Responsive Widgets

Responsive Widgets 15 minResponsive Flutter layouts read the space available with MediaQuery or LayoutBuilder and share it out with flexible widgets such as Expanded and Wrap.

Responsive Widgets

Sprout looks right on your phone. Open it on a tablet and the plant list stretches into long, thin rows with a sea of empty space. A responsive layout reads how much room it has and arranges itself to fit, whether that is a small phone, a tablet in landscape, or a resizable desktop window.

The rule

Measure the space you are given, then share it out.

Measuring

// The whole window
final width = MediaQuery.sizeOf(context).width;

// Only the space this widget's parent gives it
LayoutBuilder(
  builder: (context, constraints) {
    if (constraints.maxWidth < 600) {
      return const PlantListView();   // one column
    }
    return const PlantGridView();     // cards in a grid
  },
);

MediaQuery answers "how big is the window?". LayoutBuilder answers "how much room do I have?", which is usually the better question for a component.

Sharing out

  • Expanded and Flexible, inside a Row or Column, split the leftover space by flex ratio.
  • Wrap lays children in a line and moves to the next line when they no longer fit.
  • GridView with a maximum tile width lets the column count follow the width.
  • SafeArea keeps content clear of notches and system bars.
Row(
  children: [
    Expanded(flex: 2, child: PlantDetails(plant)),
    Expanded(flex: 1, child: WateringHistory(plant)),
  ],
);

Here the details get two thirds of the width and the history one third, on any screen.

The shortcut

Think in breakpoints and ratios, never in fixed pixel widths.

Laying out Sprout's plant screenStep 1 of 4
  1. Measure

    Wrap the area in LayoutBuilder and read constraints.maxWidth.

The trap is a fixed width like SizedBox(width: 400) in a Row. On a narrow phone the row runs out of room and Flutter paints the yellow-and-black overflow stripes.

Drills

Answer fast — trust the rule.

quick drill

Which widget reports the space its parent gives it?

quick drill

In a Row, Expanded(flex: 3) and Expanded(flex: 1) share 800 pixels. How wide is the first?

quick drill

Tag chips overflow the right edge of a phone screen. Which widget moves them onto a new line?

quick drill

Sprout's plant panel sits beside a 400-pixel menu in a 1000-pixel window. What does MediaQuery.sizeOf(context).width return inside the panel?

Remember

  • Measure the space you are given, then share it out.
  • Think in breakpoints and ratios, not fixed pixel widths.
  • Trap: MediaQuery measures the window, not your widget, and fixed widths overflow.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainOne list, two layouts

Sprout should show plants in a single column on a phone and a grid of cards on a wide tablet. Describe how you would build this, name the widget that tells you the available width, and explain why you chose it over reading the whole screen size.