Expanded
Fill available space in a Row, Column, or Flex.
Overview
Expanded
makes its child widget fill the available space along the main axis of a Row
, Column
, or Flex
. When multiple Expanded
widgets are present, space is divided according to their flex
factors.
Basic Usage
Row {
Text("Fixed")
Expanded {
Text("This fills remaining space")
}
Text("Fixed")
}
The Expanded
child takes all remaining horizontal space after fixed-size children are laid out.
Expanded Examples
Flex Factor
Control space distribution with flex
:
Row {
Expanded(flex: 1) {
ColoredBox(color: Color(0xFF_FF6B6B))
}
Expanded(flex: 2) {
ColoredBox(color: Color(0xFF_4ECDC4))
}
Expanded(flex: 1) {
ColoredBox(color: Color(0xFF_FFE66D))
}
}
Space is divided: 1/(1+2+1) = 25%, 2/4 = 50%, 1/4 = 25%
Parameters
Parameter | Type | Description | Default |
---|---|---|---|
flex | Float | Flex factor for space distribution | 1 |
child | Widget | The widget to expand | required |
How It Works
- Layout inflexible children - Fixed-size widgets get their natural size
- Calculate remaining space - Subtract inflexible sizes from total
- Divide by flex sum - Each
Expanded
gets (flex / sum) of remaining - Force child size - Child must fill its allocated space (tight constraints)
Expanded vs Flexible
Expanded
is identical to Flexible
with fit: .tight
:
// These are equivalent
Expanded {
Widget()
}
Flexible(fit: .tight) {
Widget()
}
Use Flexible
with fit: .loose
when child can be smaller than allocated space.
See Also
- Flexible - Parent class with looser constraints
- Column & Row - Where Expanded is used
- SizedBox - Fixed-size alternative
- Layout Algorithm - How Expanded works internally