What is the Spacer Widget?
Spacer is a widget that creates an adjustable, empty space that expands to fill the available space in a Row, Column, or Flex. It is a convenient alternative to using Expanded with an empty Container. Spacer distributes the remaining space among multiple spacers based on their flex factor, similar to how Expanded works. It's ideal for pushing content to the edges of a row or column, or for creating balanced spacing between widgets.
Basic Usage
In this example, the Spacer expands to take up all remaining horizontal space, pushing the right text to the end of the row.
Flex Factor
Spacer has a flex property (default 1) that determines how much of the remaining space it takes relative to other spacers and Expanded widgets in the same parent.
Here, the first spacer gets twice the amount of free space compared to the second spacer, because it has flex: 2 vs flex: 1.
Using Spacer in Column
Spacer works the same in a vertical Column. It can push content to the bottom or create evenly distributed spacing between items.
Spacer vs Expanded
Spaceris an empty widget that takes up space. It's ideal when you just need a gap.
Expandedis used to make a child fill the remaining space. If you need a child to expand (like a button or container), useExpanded. If you just need an empty gap, useSpacer.
Common Use Cases
- Push a button to the right end of an AppBar: Wrap the
AppBar'sactionswith aRowand useSpacer()between the title and actions.
- Push a button to the right end of an AppBar: Wrap the
- Bottom‑aligned content in a Column: Place a
Spacer()above the last widget to push it to the bottom.
- Bottom‑aligned content in a Column: Place a
- Evenly distributed items: Combine multiple spacers between items to create equal gaps.
- Responsive layout: Use spacers to automatically adjust spacing on different screen sizes.
Important Constraints
For Spacer to work, its parent must be a Row, Column, or Flex. The parent must also have a finite size; if the parent is unbounded (e.g., inside a ListView without a specified height), the spacer may not expand as expected. Always ensure the parent has a constrained size.
Common Mistakes
- Using
Spacerin a row that has no size constraint: If the row is inside a column that doesn't constrain its width, the spacer may not expand. Ensure the parent has a bounded width.
- Using
- Forgetting to set
flexcorrectly: If you want proportional spacing, adjust theflexvalues.
- Forgetting to set
- Using
Spacerwhen you need a fixed gap:Spaceris flexible; for a fixed gap, useSizedBoxinstead.
- Using
- Nesting
SpacerinsideExpanded:Spaceralready expands; wrapping it withExpandedis redundant.
- Nesting
Key Takeaways
Spacercreates flexible, expandable space inRow,Column, orFlex.
- Use the
flexproperty to control how much space it takes relative to others.
- Use the
- Perfect for pushing content to edges or distributing space evenly.
- It's a lighter alternative to
Expandedwith an empty child.
- It's a lighter alternative to
- Ensure the parent has a bounded size for the spacer to work.