What is Expanded in Flutter?
Expanded is a widget that expands its child to fill the available space along the main axis of a Row, Column, or Flex. It's a shorthand for using Flexible with fit: FlexFit.tight. In simple terms, if you have a Row and you want one child to take all remaining space, wrap that child with Expanded.
Basic Usage
To use Expanded, place it inside a Row, Column, or Flex. Give it a child, and optionally a flex factor to control how much space it takes relative to other Expanded children.
Here, the red and blue containers have fixed widths, while the green one (wrapped in Expanded) fills all the remaining horizontal space.
How Expanded Works
Expanded is only useful inside a Row, Column, or Flex (collectively called flex parents). It tells the parent that this child should be forced to fill the available space along the main axis. The parent then divides the remaining space among all Expanded (and Flexible) children according to their flex values.
- flex (default 1): An integer that determines the proportion of the remaining space this child should take. If you have two Expanded widgets with flex 1 and 2, the second one gets twice as much space.
- child: The widget that will be forced to expand. It can be any widget (Container, Text, etc.) and will be constrained to fill the allocated area.
Expanded vs Flexible
Expanded is actually a specialized version of Flexible. The difference lies in how they treat the child's sizing:
- Expanded uses
FlexFit.tight, meaning the child is forced to fill the available space. If the child has its own size constraints, they are overridden. - Flexible uses
FlexFit.looseby default, meaning the child can be smaller than the available space. It's allowed to size itself based on its own needs.
Using flex Factor
The flex property allows you to distribute space proportionally. This is essential for creating flexible layouts that adapt to different screen sizes.
Common Use Cases
- Making a widget fill remaining space in a Row/Column – The most common use.
- Creating flexible navigation bars – e.g., a row of buttons with one taking all extra space to push others apart.
- Building responsive forms – Input fields that stretch to fill available width.
- Centering content while allowing it to grow – Combine Expanded with Center for dynamic sizing.
Common Mistakes Beginners Make
- Using Expanded outside a Row/Column/Flex – Expanded must be a direct child of a flex widget. Placing it elsewhere (e.g., inside a Container) will cause a layout exception.
- Putting multiple Expanded widgets with no constraints – If the parent Row/Column itself is unbounded (e.g., inside a ListView without setting shrinkWrap), Expanded won't know how much space to take and will throw an error.
- Assuming Expanded respects the child's intrinsic size – Expanded forces its child to fill the space; if you need the child to be smaller, use Flexible with
FlexFit.loose. - Confusing main axis direction – Remember that Expanded works along the main axis of the parent. For a Row, it expands horizontally; for a Column, vertically.
- Forgetting that flex values are relative – They only matter in relation to other flex children. A single Expanded with flex 2 behaves exactly like flex 1.
Key Points to Remember
- Expanded must be a direct child of a Row, Column, or Flex.
- It forces its child to fill the available space along the main axis.
- Use
flexto distribute space proportionally among multiple Expanded widgets. - Expanded is a shorthand for
Flexible(fit: FlexFit.tight). - For children that should not be forced to fill, use
FlexiblewithFlexFit.loose.
Common Interview Questions
- What is the difference between Expanded and Flexible?
- Can you use Expanded inside a Container? Why or why not?
- How does the
flexproperty work? If you have three Expanded widgets with flex 1, 2, and 3, how much space does each get? - What happens if you put an Expanded widget inside a Column that is itself inside another Expanded? Explain the constraints.
- How would you create a layout where two widgets share the available space in a 3:1 ratio?
- Why might you get a "RenderFlex children have non-zero flex but incoming height constraints are unbounded" error? How do you fix it?