Skip to content
Tutorials Center
My learning

Topics

Lesson 15 of 73 ยท Layout

Row, Column, Expanded and Flexible

Master Flutter's Row and Column: main and cross axis, MainAxisAlignment, flex factors, Expanded vs Flexible, Spacer, and how to fix overflow stripes.

Row and Column are the workhorses of Flutter layout. A Row puts its children side by side, a Column stacks them top to bottom, and almost every screen you build is a nest of both. They are really the same widget in two directions: both are built on the Flex widget, which is why they share the same properties and the same sizing algorithm.

This lesson covers the two axes, the alignment options, how Expanded, Flexible and Spacer share leftover space, and why you sometimes see yellow and black overflow stripes. It builds directly on the rule from Constraints go down, sizes go up.

Real vs stand-in. Row, Column, Expanded, Flexible and Spacer are real Flutter widgets and the Dart code below is read-only. The live example is a JavaScript flex solver that follows the same steps as Flutter's RenderFlex, simplified to the main axis.

Two axes in every Flex

Main axis Row goes across, Column goes down.
Cross axis The other direction.

MainAxisAlignment and CrossAxisAlignment control each.

1 / 2

Main axis and cross axis

For a Row, the main axis is horizontal and the cross axis is vertical. For a Column it is the other way round. Two enums decide how children sit:

DART
Row(
  mainAxisAlignment: MainAxisAlignment.spaceBetween,
  crossAxisAlignment: CrossAxisAlignment.center,
  children: [
    const Icon(Icons.menu),
    const Text('Inbox'),
    const Icon(Icons.search),
  ],
)

MainAxisAlignment distributes free space along the main axis:

Value Effect
start (default) Children packed at the start
end Packed at the end
center Packed in the middle
spaceBetween Free space between children, none at the ends
spaceAround Half-size space at the ends, full space between
spaceEvenly Equal space everywhere, including the ends

CrossAxisAlignment places each child across the other direction: start, end, center (default), stretch (children are forced to the full cross size) and baseline (for aligning text baselines in a Row).

By default a Flex takes all the main-axis space it is allowed (MainAxisSize.max). Set mainAxisSize: MainAxisSize.min to make it only as long as its children. This is the right choice for a Column inside a dialog or a card, where you want it to shrink-wrap its content.

Row and Column give their children loose constraints on the cross axis, and unbounded constraints along the main axis. The second part is why a Row inside a Row with long text can overflow, and why a ListView inside a Column fails, as you saw in the previous lesson.

How the space is shared

Flutter lays out a Row or Column in a fixed order:

  1. Lay out every non-flex child (no Expanded/Flexible) with unbounded main-axis space. They take their natural size.
  2. Compute the free space: the main-axis maximum minus the total size of non-flex children.
  3. Split the free space among the flex children, in proportion to their flex factor (default 1).
  4. An Expanded child is given exactly its share (a tight constraint). A Flexible child (fit: FlexFit.loose, the default for Flexible) is given its share as a maximum, so it can be smaller.
DART
Row(
  children: [
    Container(width: 80, color: Colors.orange),  // fixed: 80
    Expanded(flex: 1, child: Container(color: Colors.teal)),
    Expanded(flex: 2, child: Container(color: Colors.indigo)),
  ],
)
// On a 380 px wide row: free space = 300, so 100 and 200 px.

Expanded(child: x) is simply Flexible(fit: FlexFit.tight, child: x). Use Flexible when you want text or content to take its natural width if it is short, but never grow beyond its share when it is long. A very common use is a label next to an icon:

DART
Row(
  children: [
    const Icon(Icons.place),
    Flexible(child: Text(longAddress, overflow: TextOverflow.ellipsis)),
  ],
)

Spacer is an Expanded with an empty box inside. It pushes the widgets before and after it apart:

DART
Row(children: [const Text('Total'), const Spacer(), Text('\$42.00')])

A flex solver you can run

This stand-in follows the same four steps. Fixed children have a size; flex children have flex and a fit (tight for Expanded, loose for Flexible) and a natural size they would like to have. It also applies MainAxisAlignment to any space left over.

JS Solve a Row on a 380 px screen

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

No output.

Notice how the Expanded children split exactly 300 px as 100 and 200, how a Flexible shrinks to its natural size when short and is capped when long, and how the five alignment values distribute the leftover 200 px of the three 60 px boxes.

Overflow stripes

If the non-flex children are together larger than the available space, there is no free space to share. In a debug build Flutter paints a yellow and black striped bar at the edge and logs A RenderFlex overflowed by 120 pixels on the right. In a release build the content is simply clipped.

DART
// Overflows on a narrow phone: the Row cannot shrink its Text.
Row(children: [
  const Icon(Icons.mail),
  Text('A really long subject line that does not fit on one line'),
])

Pick the fix that matches your intent:

  • Wrap the text in Expanded or Flexible so it wraps or ellipsizes inside its share.
  • Use Wrap instead of Row when items may flow to a new line, like tags or chips.
  • Wrap the whole Column in a SingleChildScrollView if the content may be taller than the screen (the scroll view removes the height limit, so do not put an Expanded inside it).
  • Reduce fixed sizes, or let the layout adapt, see Responsive and adaptive layouts.

Useful patterns

DART
// A centered column that only takes the space it needs.
Column(mainAxisSize: MainAxisSize.min, children: [/* ... */])

// Two buttons sharing a row equally.
Row(children: [
  Expanded(child: OutlinedButton(onPressed: cancel, child: const Text('Cancel'))),
  const SizedBox(width: 12),
  Expanded(child: FilledButton(onPressed: save, child: const Text('Save'))),
])

// Header at the top, scrolling list taking the rest.
Column(children: [
  const Header(),
  Expanded(child: ListView(children: items)),
])

For gaps between children, a SizedBox is the simplest tool, and newer Flutter versions also offer a spacing parameter on Row and Column. You will see SizedBox and friends in Padding, SizedBox, Container and ConstrainedBox.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. In a Column, which axis is the main axis?

    A Column lays children out vertically, so vertical is its main axis. A Row's main axis is horizontal.
  2. 2. A Row has a fixed 80 px icon and two Expanded children with flex 1 and flex 2 on a 380 px wide screen. How wide are the Expanded children?

    Free space is 380 - 80 = 300, split in the ratio 1:2, giving 100 and 200.
  3. 3. What is the difference between Expanded and Flexible?

    Expanded is Flexible with FlexFit.tight, so the child fills the share. Flexible's default FlexFit.loose allows a smaller child.
  4. 4. You see yellow and black stripes and 'A RenderFlex overflowed by 120 pixels'. What is the most likely cause?

    When fixed-size children exceed the space, there is nothing to share and the Flex overflows. Wrap flexible content in Expanded, or make the content scroll.

Summary

  • Row and Column are Flex widgets; the main axis follows the direction, the cross axis is the other one.
  • MainAxisAlignment spreads free space; CrossAxisAlignment aligns across; mainAxisSize: min shrink-wraps.
  • Fixed children are measured first; leftover space is split by flex among Expanded (tight) and Flexible (loose) children; Spacer is an empty Expanded.
  • Overflow stripes mean fixed content is bigger than the space: use Expanded, Flexible, Wrap or a scroll view.

Next: Stack, Align and Positioned