Sub topic 6
Controlling how much space a Row or Column takes
This chapter assumes you know how box constraints work ("constraints go down, sizes go up") and how the main axis and cross axis of a Row or Column are defined. It also assumes you have used mainAxisAlignment and crossAxisAlignment. Here you learn the property that decides how long a Row or Column is along its own main axis: mainAxisSize.
What mainAxisSize decides
Row and Column both build on the same render object, RenderFlex, and it measures its children along the main axis. After that, it must pick its own size on that axis. mainAxisSize is the rule for that choice. It accepts two values:
MainAxisSize.max(the default): take as much main-axis space as the parent allows.MainAxisSize.min: take only as much as the children need.
For a Row, the main axis is horizontal, so the property controls width. For a Column, it controls height.
import 'package:flutter/material.dart';
void main() => runApp(const MaterialApp(home: Scaffold(body: SizeDemo())));
class SizeDemo extends StatelessWidget {
const SizeDemo({super.key});
@override
Widget build(BuildContext context) {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Container(
color: Colors.amber.shade200,
child: const Row(
children: [Icon(Icons.star), Text('max (default)')],
),
),
const SizedBox(height: 16),
Container(
color: Colors.lightBlue.shade200,
child: const Row(
mainAxisSize: MainAxisSize.min,
children: [Icon(Icons.star), Text('min')],
),
),
],
);
}
}Run this and you will see two colored bars. The amber one spans the whole screen width. The blue one hugs its icon and text. The outer Column uses CrossAxisAlignment.start, which passes loose width constraints (minimum 0, maximum the screen width) to each child. Because the constraints are loose, each Container can shrink to its child, and the Row decides how wide that is.
How the decision is made
The rule works in terms of constraints, which explains the edge cases later in the chapter.
- The parent hands the
RowaBoxConstraintswith a minimum and maximum width. - The
Rowlays out its non-flexible children and adds up their widths. - If any children are
ExpandedorFlexible, they divide the remaining space. This only works when the maximum width is finite. - With
max, theRowpicks the maximum width. Withmin, it picks the sum of its children's widths. - The result is clamped to the incoming constraints. It is never smaller than the minimum or larger than the maximum.
The clamp in the last step matters. MainAxisSize.min is a request to shrink, not a guarantee. If the parent imposes tight constraints, such as a SizedBox(width: 300), the Row is 300 wide regardless of its mainAxisSize.
The practical cases for min
Use MainAxisSize.min when the Row or Column should be only as large as its content. Three situations come up often.
A Column inside a Center, Card, or dialog
Center loosens constraints, so a default Column inside it would still grow to the full available height. With min, the card wraps its content:
import 'package:flutter/material.dart';
void main() => runApp(const MaterialApp(home: Scaffold(body: ProfileCard())));
class ProfileCard extends StatelessWidget {
const ProfileCard({super.key});
@override
Widget build(BuildContext context) {
return const Center(
child: Card(
child: Padding(
padding: EdgeInsets.all(16),
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
CircleAvatar(radius: 32, child: Icon(Icons.person)),
SizedBox(height: 12),
Text('Ada Lovelace'),
],
),
),
),
);
}
}Change MainAxisSize.min to MainAxisSize.max and the card stretches to the full height of the screen. The Padding and Card pass the constraints through, so the Column is the only widget that decides the height.
A Row used as a button or badge label
An icon next to a label, placed inside a Container with a background or border, should wrap its content. A default Row would fill the width of whatever loose space the parent offers.
Bottom sheets and menus
showModalBottomSheet gives its child loose height constraints, up to a fraction of the screen. A Column(mainAxisSize: MainAxisSize.min) makes the sheet only as tall as its content.
Interaction with MainAxisAlignment
mainAxisAlignment distributes free space along the main axis. With MainAxisSize.min there is normally no free space, because the Row is exactly as wide as its children. So spaceBetween, spaceEvenly, center and the rest appear to do nothing.
| Situation | Free space on main axis? | Does mainAxisAlignment show? |
|---|---|---|
| `max`, bounded constraints | Yes, max width minus children | Yes |
| `min`, loose constraints | No | No visible effect |
| `min`, tight constraints wider than children | Yes, from the clamp | Yes |
| Any size, with an `Expanded` child | No, `Expanded` consumes it | No visible effect |
The third row is the least obvious. Suppose a SizedBox(width: 300) wraps a Row(mainAxisSize: MainAxisSize.min, mainAxisAlignment: MainAxisAlignment.spaceBetween). The SizedBox forces a minimum width of 300, so the Row ends up 300 wide even though it asked to shrink. The extra space then exists, and spaceBetween spreads the children across it.
What mainAxisSize does not control
mainAxisSize only affects the main axis. The cross axis follows a different rule: a Row is as tall as its tallest child, and a Column is as wide as its widest child, both clamped to the incoming constraints.
To widen the cross axis you have two options:
CrossAxisAlignment.stretchforces every child to the maximum cross-axis constraint. AColumnusing it fills the available width. This requires a bounded cross axis, otherwise you get an error about infinite size.- Wrap the
RoworColumnin aSizedBoxorConstrainedBoxwith explicit dimensions.
import 'package:flutter/material.dart';
void main() => runApp(const MaterialApp(home: Scaffold(body: StretchDemo())));
class StretchDemo extends StatelessWidget {
const StretchDemo({super.key});
@override
Widget build(BuildContext context) {
return Center(
child: SizedBox(
width: 240,
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
ElevatedButton(onPressed: () {}, child: const Text('Save')),
ElevatedButton(onPressed: () {}, child: const Text('Cancel')),
],
),
),
);
}
}The column is as short as its two buttons (min on the main axis) and as wide as the SizedBox (stretch on the cross axis).
Edge cases and common errors
Unbounded main axis
A Row inside a horizontal ListView, a SingleChildScrollView(scrollDirection: Axis.horizontal), or another Row receives an infinite maximum width. In that case the Row cannot fill the maximum, so it wraps its children, whatever mainAxisSize says. The same applies to a Column inside a vertical scrollable.
The problem appears when such a Row has Expanded or Flexible children. There is no finite space to divide, and Flutter throws a RenderFlex error saying that children have non-zero flex but the incoming constraints are unbounded. Setting MainAxisSize.min does not fix this. The usual fix is to give the Row a finite width, or to remove the Expanded and give the child a fixed size.
Expanded inside a min Row
If the constraints are bounded, an Expanded child still takes all the remaining space even when the Row is min. The Row then ends up at its maximum width anyway, because its children now occupy it. min only helps when no child asks for the leftover space.
Reading size from a parent
A common mistake is to set mainAxisSize: MainAxisSize.min on a Column to fix an overflow. It does not remove the overflow. It only makes the Column shorter when the content fits. If the content is taller than the screen, the Column is still too tall, and you need a scrollable or a Flexible child instead.
Choosing between max and min
| Goal | Use |
|---|---|
| Fill the screen or parent, then align children inside it | `max` (default) |
| Size a card, chip, dialog or sheet to its content | `min` |
| Share leftover space with `Expanded` | Either, because `Expanded` fills it |
| Place a Row inside a horizontal scroller | `min`, with fixed-size children |
Exercise
A Column with mainAxisSize: MainAxisSize.min and mainAxisAlignment: MainAxisAlignment.center is placed directly inside Center. Is the Column centered vertically by its own mainAxisAlignment? Explain.
Show answer
No. Center loosens the constraints, and with min the Column is exactly as tall as its children, so it has no free vertical space to distribute. Its mainAxisAlignment has no visible effect. The Center widget is what places the Column in the middle of the screen.
Exercise
You write SizedBox(width: 300, child: Row(mainAxisSize: MainAxisSize.min, mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [Text('A'), Text('B')])). How wide is the Row, and where do the two Text widgets appear?
Show answer
The Row is 300 wide. The SizedBox passes tight constraints, so the Row's size is clamped to 300 even though it asked for min. That leaves free space, and spaceBetween puts 'A' at the left edge and 'B' at the right edge.