Skip to the workspace
Pebellslearn by building
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.

flutter / Responsive Widgets

Explore each scene at your own pace.

01 · The rule

The rule

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.

Scene 1 of 5
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.