Bird
Raised Fist0
Fluttermobile~15 mins

Wrap widget in Flutter - Deep Dive

Choose your learning style10 modes available
Overview - Wrap widget
What is it?
The Wrap widget in Flutter arranges its child widgets in horizontal or vertical lines and automatically moves to the next line when there is no more space. It helps display items in a flexible grid-like layout without scrolling. This widget is useful when you want to show many items that can wrap onto new lines, like tags or buttons.
Why it matters
Without the Wrap widget, items that don't fit in one line might overflow or require complex manual layout. Wrap solves the problem of dynamic content that changes size or number, making apps look neat on different screen sizes. It improves user experience by preventing clipped or hidden content.
Where it fits
Before learning Wrap, you should understand basic Flutter layout widgets like Row, Column, and Container. After Wrap, you can explore more advanced layout widgets like GridView and CustomMultiChildLayout for complex designs.
Mental Model
Core Idea
Wrap automatically moves child widgets to the next line when space runs out, creating a flexible, multi-line layout.
Think of it like...
Imagine packing items in a suitcase row by row; when one row is full, you start a new row below. Wrap does the same with widgets on the screen.
Wrap Layout:
┌─────────────────────────────┐
│ Widget1 Widget2 Widget3      │
│ Widget4 Widget5 Widget6      │
│ Widget7 Widget8              │
└─────────────────────────────┘
Build-Up - 6 Steps
1
FoundationBasic Wrap widget usage
🤔
Concept: Wrap arranges children horizontally and wraps to new lines automatically.
Wrap( children: [ Text('One'), Text('Two'), Text('Three'), Text('Four'), Text('Five'), ], )
Result
The texts appear side by side and move to the next line when they don't fit horizontally.
Understanding that Wrap handles overflow by moving widgets to new lines simplifies layout for dynamic content.
2
FoundationWrap direction and spacing
🤔
Concept: Wrap can arrange children horizontally or vertically and control spacing between them.
Wrap( direction: Axis.vertical, spacing: 10.0, runSpacing: 20.0, children: [ Icon(Icons.star), Icon(Icons.star_border), Icon(Icons.star_half), ], )
Result
Icons are arranged vertically with 10 pixels between items and 20 pixels between lines.
Knowing how to adjust direction and spacing lets you customize the layout to fit your design needs.
3
IntermediateControlling alignment in Wrap
🤔Before reading on: do you think Wrap aligns children only horizontally or both horizontally and vertically? Commit to your answer.
Concept: Wrap allows alignment of children within a line and alignment of lines themselves.
Wrap( alignment: WrapAlignment.center, runAlignment: WrapAlignment.spaceBetween, children: [ Chip(label: Text('A')), Chip(label: Text('B')), Chip(label: Text('C')), ], )
Result
Children are centered in each line, and lines are spaced evenly vertically.
Understanding alignment options helps create visually balanced layouts with Wrap.
4
IntermediateUsing Wrap with dynamic content
🤔Before reading on: do you think Wrap automatically updates layout when children change size or number? Commit to your answer.
Concept: Wrap dynamically adapts to changes in its children, making it ideal for lists that grow or shrink.
List tags = ['Flutter', 'Dart', 'Widgets']; Wrap( children: tags.map((tag) => Chip(label: Text(tag))).toList(), )
Result
Chips appear wrapped neatly, and adding/removing tags updates the layout automatically.
Knowing Wrap handles dynamic children reduces the need for manual layout recalculations.
5
AdvancedPerformance considerations with Wrap
🤔Before reading on: do you think Wrap is efficient for very large lists or should you use other widgets? Commit to your answer.
Concept: Wrap lays out all children at once, which can be costly for large numbers of widgets; alternatives exist for such cases.
For many items, consider using GridView.builder instead of Wrap to improve performance by building widgets on demand.
Result
Using Wrap with many children may cause slow rendering; GridView.builder improves scrolling and memory use.
Understanding Wrap's layout behavior helps choose the right widget for performance-sensitive apps.
6
ExpertCustomizing Wrap with runSpacing and crossAxisAlignment
🤔Before reading on: do you think runSpacing affects space between lines or between items in a line? Commit to your answer.
Concept: runSpacing controls vertical space between lines, and crossAxisAlignment controls vertical alignment of children within a line.
Wrap( spacing: 8.0, runSpacing: 16.0, crossAxisAlignment: WrapCrossAlignment.end, children: [ Container(height: 30, width: 50, color: Colors.red), Container(height: 50, width: 50, color: Colors.green), Container(height: 40, width: 50, color: Colors.blue), ], )
Result
Children align at the bottom of each line with 8 pixels between items and 16 pixels between lines.
Mastering these properties allows precise control over complex multi-line layouts.
Under the Hood
Wrap measures each child widget's size and places them sequentially along the main axis until no more fit. Then it starts a new 'run' (line) along the cross axis. It calculates spacing and alignment for each run and child, laying out all children before painting.
Why designed this way?
Wrap was designed to solve the problem of overflow in Row and Column widgets by automatically wrapping content. It trades off some performance for flexibility and ease of use, avoiding manual line breaks or scroll views for small to medium content sets.
Wrap Layout Process:
┌───────────────┐
│ Start placing │
│ children in  │
│ main axis    │
├───────────────┤
│ If no space,  │
│ start new run │
├───────────────┤
│ Align runs &  │
│ children     │
└───────────────┘
Myth Busters - 4 Common Misconceptions
Quick: Does Wrap scroll automatically when content overflows? Commit yes or no.
Common Belief:Wrap automatically scrolls when there are too many children to fit.
Tap to reveal reality
Reality:Wrap does not scroll; it wraps children onto new lines but does not provide scrolling behavior.
Why it matters:Assuming Wrap scrolls can cause UI bugs where content is clipped and inaccessible.
Quick: Can Wrap replace GridView for large data sets efficiently? Commit yes or no.
Common Belief:Wrap is always the best choice for grid-like layouts, even with many items.
Tap to reveal reality
Reality:Wrap lays out all children at once, which can hurt performance with large lists; GridView.builder is better for large, scrollable grids.
Why it matters:Using Wrap for large data can cause slow app performance and poor user experience.
Quick: Does Wrap only work horizontally? Commit yes or no.
Common Belief:Wrap only arranges children horizontally and wraps to new rows.
Tap to reveal reality
Reality:Wrap can arrange children vertically and wrap to new columns by setting direction to Axis.vertical.
Why it matters:Knowing this expands design possibilities and prevents layout mistakes.
Quick: Does spacing property add space between lines or items? Commit your answer.
Common Belief:spacing adds space between lines of wrapped children.
Tap to reveal reality
Reality:spacing adds space between items in the same run; runSpacing adds space between lines.
Why it matters:Confusing these leads to unexpected gaps and misaligned layouts.
Expert Zone
1
Wrap's layout algorithm measures all children upfront, so it can be costly for many widgets; understanding this helps optimize performance.
2
The difference between alignment, runAlignment, and crossAxisAlignment is subtle but crucial for precise layout control.
3
Wrap does not support scrolling; combining it with SingleChildScrollView requires careful height constraints to avoid layout errors.
When NOT to use
Avoid Wrap for very large or infinite lists; use GridView.builder or ListView for better performance and scrolling support. Also, avoid Wrap when you need fixed grid cells with equal sizes; GridView is better suited.
Production Patterns
Wrap is commonly used for tag clouds, button groups, and chip lists where content size varies. In production, it is often combined with SingleChildScrollView for scrollable wrapped content or used inside constrained containers to prevent overflow.
Connections
Flexbox (CSS)
Wrap in Flutter is similar to CSS Flexbox's flex-wrap property.
Understanding CSS Flexbox helps grasp Wrap's behavior since both handle flexible, wrapping layouts in different platforms.
GridView widget
GridView builds fixed or dynamic grids with scrolling, while Wrap builds flexible wrapped layouts without scrolling.
Knowing when to use Wrap versus GridView improves app performance and user experience.
Packing problem (Computer Science)
Wrap solves a simplified packing problem by placing widgets sequentially until space runs out, then wrapping.
Recognizing this connection explains why Wrap cannot optimize space perfectly but prioritizes simplicity and speed.
Common Pitfalls
#1Content overflows because Wrap does not scroll.
Wrong approach:Wrap( children: List.generate(100, (i) => Text('Item $i')), )
Correct approach:SingleChildScrollView( child: Wrap( children: List.generate(100, (i) => Text('Item $i')), ), )
Root cause:Assuming Wrap provides scrolling leads to overflow when many children exceed screen space.
#2Using Wrap for very large lists causes slow rendering.
Wrong approach:Wrap( children: List.generate(1000, (i) => Text('Item $i')), )
Correct approach:GridView.builder( gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 3), itemBuilder: (context, index) => Text('Item $index'), itemCount: 1000, )
Root cause:Wrap builds all children at once, which is inefficient for large data sets.
#3Confusing spacing and runSpacing causes layout gaps.
Wrong approach:Wrap( spacing: 20.0, // expecting space between lines children: [...], )
Correct approach:Wrap( runSpacing: 20.0, // space between lines spacing: 8.0, // space between items children: [...], )
Root cause:Misunderstanding property roles leads to unexpected spacing.
Key Takeaways
Wrap widget arranges children in lines and automatically wraps them when space runs out, creating flexible layouts.
It is ideal for dynamic content like tags or buttons but does not provide scrolling by itself.
Understanding spacing, alignment, and direction properties allows precise control over the layout.
Wrap measures all children upfront, so it is not suitable for very large lists where GridView.builder is better.
Combining Wrap with scrolling widgets requires careful layout constraints to avoid overflow errors.

Practice

(1/5)
1. What is the main purpose of the Wrap widget in Flutter?
easy
A. To arrange child widgets in lines and wrap them when space runs out
B. To create a scrollable list of items vertically
C. To stack widgets on top of each other
D. To display a single widget centered on the screen

Solution

  1. 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.
  2. 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.
  3. Final Answer:

    To arrange child widgets in lines and wrap them when space runs out -> Option A
  4. Quick Check:

    Wrap = arrange and wrap children [OK]
Hint: Wrap arranges children and moves to next line when needed [OK]
Common Mistakes:
  • Confusing Wrap with ListView (scrollable list)
  • Thinking Wrap stacks widgets like Stack widget
  • Assuming Wrap centers a single widget
2. Which of the following is the correct syntax to create a Wrap widget with spacing of 10 pixels between children?
easy
A. Wrap(childSpacing: 10, children: [Text('A'), Text('B')])
B. Wrap(space: 10, children: [Text('A'), Text('B')])
C. Wrap(padding: 10, children: [Text('A'), Text('B')])
D. Wrap(spacing: 10, children: [Text('A'), Text('B')])

Solution

  1. Step 1: Identify the correct property for spacing

    The Wrap widget uses the property spacing to set horizontal gaps between children.
  2. Step 2: Check the options for correct property name

    Only Wrap(spacing: 10, children: [Text('A'), Text('B')]) uses spacing correctly. Options A, B, and D use incorrect property names.
  3. Final Answer:

    Wrap(spacing: 10, children: [Text('A'), Text('B')]) -> Option D
  4. Quick Check:

    Spacing property = spacing [OK]
Hint: Use 'spacing' property for gaps between children in Wrap [OK]
Common Mistakes:
  • Using 'space' or 'childSpacing' instead of 'spacing'
  • Confusing padding with spacing
  • Omitting the children list
3. What will be the visual output of this Flutter code snippet?
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.
medium
A. Two rows: first row with 2 containers, second row with 2 containers, with 8px horizontal and 12px vertical spacing
B. One row with all 4 containers side by side with 8px spacing
C. Four rows with one container each stacked vertically
D. Two rows: first row with 3 containers, second row with 1 container, no spacing

Solution

  1. 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.
  2. 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.
  3. Final Answer:

    Two rows: first row with 2 containers, second row with 2 containers, with 8px horizontal and 12px vertical spacing -> Option A
  4. Quick Check:

    Wrap fits 2 per row with spacing [OK]
Hint: Add widths plus spacing to check fit per row [OK]
Common Mistakes:
  • Assuming all containers fit in one row
  • Ignoring spacing when calculating width
  • Confusing runSpacing with spacing
4. Identify the error in this Flutter code using Wrap:
Wrap(
  spacing: 10,
  children: [
    Text('One'),
    Text('Two'),
    Text('Three'),
  ],
  runSpacing: 15
)
medium
A. The spacing property must be inside children
B. The children list is missing a closing bracket
C. There is no error; the code is correct
D. The runSpacing property is placed after children, causing a syntax error

Solution

  1. Step 1: Check property order in Wrap widget

    In Flutter, the order of named parameters does not matter. runSpacing can be placed after children without error.
  2. Step 2: Verify brackets and property usage

    The children list is properly closed, and spacing is correctly used as a property, not inside children.
  3. Final Answer:

    There is no error; the code is correct -> Option C
  4. Quick Check:

    Named parameters order doesn't cause errors [OK]
Hint: Named parameters order doesn't matter in Flutter [OK]
Common Mistakes:
  • Thinking property order causes syntax errors
  • Misreading brackets in children list
  • Confusing property placement inside children
5. You want to create a responsive tag list using Wrap that automatically moves tags to the next line when they don't fit horizontally. Which combination of properties should you set to control the horizontal space between tags and vertical space between lines?
hard
A. Use padding for horizontal gaps and margin for vertical gaps
B. Use spacing for horizontal gaps and runSpacing for vertical gaps
C. Use mainAxisSpacing for horizontal gaps and crossAxisSpacing for vertical gaps
D. Use horizontalSpacing for horizontal gaps and verticalSpacing for vertical gaps

Solution

  1. Step 1: Identify correct properties for Wrap spacing

    The Wrap widget uses spacing to set horizontal gaps between children and runSpacing to set vertical gaps between lines.
  2. Step 2: Eliminate incorrect property names

    Properties like padding, margin, mainAxisSpacing, crossAxisSpacing, horizontalSpacing, and verticalSpacing are not valid for Wrap.
  3. Final Answer:

    Use spacing for horizontal gaps and runSpacing for vertical gaps -> Option B
  4. Quick Check:

    Wrap spacing = spacing and runSpacing [OK]
Hint: Remember: spacing = horizontal, runSpacing = vertical [OK]
Common Mistakes:
  • Using padding or margin instead of spacing properties
  • Confusing Wrap with GridView spacing properties
  • Using incorrect property names like mainAxisSpacing
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard