Sub topic 8
Sharing space with Expanded, Flexible, and Spacer
This chapter assumes you know how a Row or Column uses its main axis and cross axis. It also assumes you know the rule "constraints go down, sizes go up". Here you will see how the free space left over after fixed-size children is divided among flexible ones.
The two-pass flex algorithm
A Row or Column is a Flex widget. Both lay out their children in two passes.
- Fixed children. Each child that is not flexible is laid out with an unbounded main-axis constraint. It picks its own size along the main axis. In a
Row, that means its own width. - Flexible children. The free space is the parent's maximum main-axis size minus the sizes of the fixed children. Each flexible child gets a share of it:
share = freeSpace * child.flex / totalFlex
totalFlex is the sum of the flex factors of all flexible children.
Fixed children therefore take their space first, and flexible children split what is left. Flex factors are relative to each other, not percentages.
Expanded
Expanded marks a child as flexible and forces it to fill its share exactly. Here is a complete app. The Row is 380 logical pixels wide, and the fixed icon box takes 80.
import 'package:flutter/material.dart';
void main() => runApp(const MaterialApp(home: Scaffold(body: Demo())));
class Demo extends StatelessWidget {
const Demo({super.key});
@override
Widget build(BuildContext context) {
return Center(
child: SizedBox(
width: 380,
child: Row(
children: [
Container(width: 80, height: 50, color: Colors.green),
Expanded(
flex: 2,
child: Container(height: 50, color: Colors.blue),
),
Expanded(
child: Container(height: 50, color: Colors.red),
),
],
),
),
);
}
}The free space is 380 − 80 = 300. totalFlex is 2 + 1 = 3. The blue box gets 300 × 2/3 = 200 and the red box gets 300 × 1/3 = 100. The default flex is 1.
Because Expanded forces an exact size, the blue and red boxes need no width of their own. Giving a child inside Expanded a different width has no effect, since the incoming constraint is tight along the main axis.
Flexible and FlexFit
Expanded is a shortcut. This:
Expanded(flex: 2, child: child)is the same as this:
Flexible(flex: 2, fit: FlexFit.tight, child: child)Flexible defaults to FlexFit.loose. The child gets a constraint from 0 up to its share, so it may be smaller than the share.
| Widget | Fit | Child's main-axis constraint | Child can be smaller than its share? |
|---|---|---|---|
| `Expanded` | tight | exactly the share | No |
| `Flexible` | loose (default) | 0 up to the share | Yes |
| `Flexible(fit: FlexFit.tight)` | tight | exactly the share | No |
Loose fit matters when the content size varies. A chat bubble that should hug short text but wrap long text is a typical case.
Row(
children: [
Flexible(
child: Container(
padding: const EdgeInsets.all(8),
color: Colors.amber,
child: const Text('Short text hugs its content, long text wraps.'),
),
),
const Icon(Icons.done_all),
],
)If you used Expanded here, the amber box would always stretch across the whole row. With Flexible, a short message keeps a narrow box. A long message stops growing at the available width and wraps.
One consequence concerns mainAxisSize. A Row with MainAxisSize.min and only loose Flexible children can shrink to fit them. A Row containing an Expanded child fills all the available width regardless, because the tight child consumes all the free space.
Spacer
Spacer is an Expanded that holds an empty box. It exists to push siblings apart without drawing anything.
Row(
children: [
const Text('Back'),
const Spacer(),
const Text('Next'),
],
)Spacer takes a flex argument and takes part in the same division as everything else. Two spacers with different flex values place a widget at a proportional position.
Row(
children: [
const Spacer(flex: 1),
const Text('Left third'),
const Spacer(flex: 2),
],
)Here the text sits one third of the way across, as long as the text is narrow compared to the row.
Using MainAxisAlignment.spaceBetween gives a similar result for simple cases. Choose Spacer when you need unequal gaps or want to push only one group of children apart.
Rules and errors
Expanded must be a direct child of a Flex
Expanded and Flexible are ParentDataWidgets. They write flex information onto their child's parent data, and the enclosing Flex reads it. They only work when the next render object up is a RenderFlex. If you wrap one in Padding or Align, Flutter throws:
Incorrect use of ParentDataWidget.Put the Padding inside the Expanded instead:
Row(
children: [
Expanded(
child: Padding(
padding: const EdgeInsets.all(8),
child: const Text('Padded and expanded'),
),
),
],
)Flex children in an unbounded main axis
Free space is only defined if the main axis has a maximum. If a Row sits inside a horizontal ListView or SingleChildScrollView, its width is unbounded. An Expanded child inside it fails with a message such as:
RenderFlex children have non-zero flex but incoming width constraints are unbounded.The same happens to a Column with an Expanded child inside a vertical scrollable, or inside another Column's non-flex slot. There are three ways to fix it:
- Give the
RoworColumna bounded size, for example with aSizedBox. - Remove the
Expandedand size the child explicitly. - Put the scrolling widget itself inside an
Expanded, as in the next example.
Nested scrollables need Expanded
The reverse problem is a scrolling child that needs a bounded height. A ListView asks for as much height as it can get. A Column passes down an unbounded height to its fixed children, so the ListView has no limit. Expanded supplies that limit.
Column(
children: [
const Text('Header'),
Expanded(
child: ListView(
children: const [
ListTile(title: Text('One')),
ListTile(title: Text('Two')),
ListTile(title: Text('Three')),
],
),
),
],
)The header takes its natural height. The list gets the rest.
Overflow is not fixed by flex alone
Fixed children are measured first. If they are already wider than the row, no free space is left and the row overflows. Expanded only helps for the children it wraps. Wrap the one that can shrink, usually a Text.
Row(
children: [
const Icon(Icons.info),
const SizedBox(width: 8),
Expanded(
child: Text(
'A very long message that would otherwise overflow the Row',
overflow: TextOverflow.ellipsis,
),
),
],
)The Text now receives a bounded width, so it can truncate or wrap.
Choosing between them
- Use
Expandedwhen a child should fill its share exactly. - Use
Flexiblewhen the child should be at most its share but may be smaller. - Use
Spacerwhen you need empty, adjustable space and nothing to draw. - Use
SizedBoxwhen you need a fixed gap.
Exercise
A Row is 500 logical pixels wide. It contains a 100-pixel-wide Container, then Expanded(flex: 3), then Expanded(flex: 2). What width does each Expanded child get?
Show answer
Free space = 500 − 100 = 400. totalFlex = 3 + 2 = 5. The first Expanded gets 400 × 3/5 = 240. The second gets 400 × 2/5 = 160.
Exercise
You wrap a Text in Padding and then wrap the Padding in Expanded, all inside a Row. A teammate moves Expanded to wrap the Row's child through an Align widget, and Flutter reports "Incorrect use of ParentDataWidget". Why, and how do you fix it?
Show answer
Expanded is a ParentDataWidget, so it only works when it is a direct child of a Row, Column, or Flex. Align sits between Expanded and the Row, so the Row never sees the flex data. Move Expanded back to be the direct child of the Row, and put Align (or Padding) inside the Expanded.
Exercise
How does Flexible with its default fit differ from Expanded, and when would a chat bubble use Flexible?
Show answer
Flexible defaults to FlexFit.loose, so its child gets a constraint from 0 up to its share and may be smaller. Expanded is tight, so its child must fill its share exactly. A chat bubble uses Flexible so that short text keeps a narrow bubble, while long text grows only to the available width and then wraps.