FlutterBasic 2 min read

Layout starts with constraints

Understand how parent constraints, child sizes and Expanded work together in a Flutter layout.

On this page

Flutter layout is a conversation between parents and children. A parent gives constraints, a child chooses a size within them, and the parent positions the child. A layout problem is often a constraint problem rather than a missing width.

Share a row deliberately

Without a flexible child, a long text label can demand more horizontal space than a row has available. Expanded gives it a defined share of the remaining width.

main.dart
import 'package:flutter/material.dart';
void main() => runApp(const MaterialApp(home: Scaffold(
  body: Center(child: Padding(
    padding: EdgeInsets.all(24),
    child: Row(children: [
      Icon(Icons.lightbulb_outline, size: 36),
      SizedBox(width: 16),
      Expanded(child: Column(mainAxisSize: MainAxisSize.min,
        crossAxisAlignment: CrossAxisAlignment.start, children: [
          Text('A useful idea', style: TextStyle(fontSize: 24)),
          Text('Leave enough room for a label that may wrap onto another line.'),
        ])),
    ]),
  )),
)));

Main and cross axes

For Row, the main axis is horizontal. For Column, it is vertical. Alignment settings are easier to understand when you identify the axis before changing a property.

Watch for unbounded space

An Expanded needs a finite amount of space to divide along its flex axis. Putting one inside a vertically scrolling column can produce an unbounded-height error. Decide whether the content should scroll, fill a bounded area, or size itself to its children.

Calculate the row before changing it

Start with the parent’s available width. Subtract the icon and spacing, then ask which child may use the remainder. Expanded gives the text region a tight share of that bounded space, allowing the paragraph to wrap. Without an appropriate bound, the text can demand more width than the row can fit.

For a Column, repeat the reasoning along the vertical axis. The terms main and cross refer to the selected flex direction, not permanently to horizontal and vertical. A setting that fixes horizontal distribution in a Row therefore has a different visual effect in a Column.

Do not apply Expanded blindly inside a scrolling region. If the main-axis extent is unbounded, there is no finite remainder to divide. Compare scrollable layouts with the overflow diagnostic and choose whether the content should fill, wrap or scroll.

Try it yourself

Replace the description with a much longer sentence. Resize the preview and observe wrapping. Then remove Expanded and compare the behavior. Explain why the icon still fits but the label can overflow.

How this topic connects

Understand first

Apply it next

Lesson progressMark complete after checking the example and exercise.

Find your next idea

Search lessons, code examples and projects.

Type a few words to start exploring.