What is Wrap in Flutter?
Wrap is a layout widget that displays its children in multiple horizontal or vertical runs. When there's not enough space in the current run, it creates a new run. It's perfect for displaying chips, tags, or any collection of items that need to flow like text.
Basic Usage
DARTRead-only
1
Key Properties
- direction: Axis.horizontal (default) or Axis.vertical.
- alignment: How to align children within a run (e.g.,
WrapAlignment.start,center,end,spaceBetween). - spacing: Space between children along the main axis.
- runAlignment: How to align runs along the cross axis.
- runSpacing: Space between runs.
- crossAxisAlignment: How to align children within a run in the cross axis (e.g.,
start,end,center). - textDirection, verticalDirection: Control the order.
Wrap vs Row/Column
Use Row/Column when you have a fixed number of items that should stay in one line. Use Wrap when the number of items can vary and you want them to automatically wrap to the next line.
Common Use Cases
- Tags or chips in a blog post.
- Keyword lists.
- Photo galleries that wrap.
- Filter buttons that adapt to screen width.
Key Points to Remember
- Wrap automatically moves children to the next line when they don't fit.
- Control gaps with
spacingandrunSpacing. - Align runs with
runAlignment. - Wrap is more flexible than Row/Column for dynamic content.