What if your app's layout could magically adjust itself perfectly on any screen without extra code?
Why Wrap widget in Flutter? - Purpose & Use Cases
Imagine you want to show a list of colorful tags or buttons on a small phone screen. You try to put them all in a row, but they don't fit and get cut off or overflow outside the screen.
Manually adjusting each item's size or adding complicated scrolling makes your code messy and hard to maintain. It's slow to fix and often breaks on different screen sizes or orientations.
The Wrap widget automatically moves items to the next line when they don't fit in one row. It keeps your layout neat and responsive without extra work.
Row(children: [Tag1, Tag2, Tag3, Tag4, Tag5])
Wrap(children: [Tag1, Tag2, Tag3, Tag4, Tag5])
You can create flexible, neat layouts that adapt smoothly to any screen size or orientation.
Think of a photo gallery app showing thumbnails that wrap to new lines as you rotate your phone or use different devices.
Wrap widget arranges items automatically in multiple lines.
It saves you from manual size and overflow fixes.
It makes your app look good on all screen sizes.
Practice
Wrap widget in Flutter?Solution
Step 1: Understand Wrap widget behavior
The Wrap widget arranges children in horizontal or vertical lines and wraps them to the next line when there is no more space.Step 2: Compare options with Wrap's purpose
Only To arrange child widgets in lines and wrap them when space runs out describes this behavior correctly. Options A, B, and C describe other widgets or layouts.Final Answer:
To arrange child widgets in lines and wrap them when space runs out -> Option AQuick Check:
Wrap = arrange and wrap children [OK]
- Confusing Wrap with ListView (scrollable list)
- Thinking Wrap stacks widgets like Stack widget
- Assuming Wrap centers a single widget
Solution
Step 1: Identify the correct property for spacing
The Wrap widget uses the propertyspacingto set horizontal gaps between children.Step 2: Check the options for correct property name
Only Wrap(spacing: 10, children: [Text('A'), Text('B')]) usesspacingcorrectly. Options A, B, and D use incorrect property names.Final Answer:
Wrap(spacing: 10, children: [Text('A'), Text('B')]) -> Option DQuick Check:
Spacing property = spacing [OK]
- Using 'space' or 'childSpacing' instead of 'spacing'
- Confusing padding with spacing
- Omitting the children list
Wrap(
spacing: 8,
runSpacing: 12,
children: [
Container(width: 100, height: 50, color: Colors.red),
Container(width: 100, height: 50, color: Colors.green),
Container(width: 100, height: 50, color: Colors.blue),
Container(width: 100, height: 50, color: Colors.yellow),
],
)
Assuming the screen width is 220 pixels.Solution
Step 1: Calculate how many containers fit per row
Each container is 100px wide, spacing between is 8px. Two containers plus spacing: 100 + 8 + 100 = 208px, which fits in 220px width. Three containers would be 100 + 8 + 100 + 8 + 100 = 316px, too wide.Step 2: Determine rows and spacing
So, Wrap places 2 containers in first row, then wraps to second row for next 2 containers. Horizontal spacing is 8px, vertical runSpacing is 12px.Final Answer:
Two rows: first row with 2 containers, second row with 2 containers, with 8px horizontal and 12px vertical spacing -> Option AQuick Check:
Wrap fits 2 per row with spacing [OK]
- Assuming all containers fit in one row
- Ignoring spacing when calculating width
- Confusing runSpacing with spacing
Wrap(
spacing: 10,
children: [
Text('One'),
Text('Two'),
Text('Three'),
],
runSpacing: 15
)Solution
Step 1: Check property order in Wrap widget
In Flutter, the order of named parameters does not matter.runSpacingcan be placed afterchildrenwithout error.Step 2: Verify brackets and property usage
Thechildrenlist is properly closed, andspacingis correctly used as a property, not insidechildren.Final Answer:
There is no error; the code is correct -> Option CQuick Check:
Named parameters order doesn't cause errors [OK]
- Thinking property order causes syntax errors
- Misreading brackets in children list
- Confusing property placement inside children
Solution
Step 1: Identify correct properties for Wrap spacing
The Wrap widget usesspacingto set horizontal gaps between children andrunSpacingto set vertical gaps between lines.Step 2: Eliminate incorrect property names
Properties likepadding,margin,mainAxisSpacing,crossAxisSpacing,horizontalSpacing, andverticalSpacingare not valid for Wrap.Final Answer:
Usespacingfor horizontal gaps andrunSpacingfor vertical gaps -> Option BQuick Check:
Wrap spacing = spacing and runSpacing [OK]
- Using padding or margin instead of spacing properties
- Confusing Wrap with GridView spacing properties
- Using incorrect property names like mainAxisSpacing
Ready to prove it?
Pass the test and this pattern moves unknown
