Flutter Wrap Widget
Automatically wrapping widgets to new lines
đ What is Wrap?
Wrap is a Flutter widget that displays children in multiple horizontal or vertical runs. When there's no more room, it automatically wraps to the next line, perfect for tags, chips, and flexible layouts.
// Simple Wrap example
Wrap(
children: [
Chip(label: Text('Tag 1')),
Chip(label: Text('Tag 2')),
Chip(label: Text('Tag 3')),
],
)
Output:
Key Wrap Concepts
Auto Wrap
Automatically wraps to next line
Wrap(
children: widgets,
)
Spacing
Control space between items
Wrap(
spacing: 8.0,
runSpacing: 4.0,
)
Direction
Horizontal or vertical layout
Wrap(
direction: Axis.vertical,
)
Alignment
Align wrapped content
Wrap(
alignment: WrapAlignment.center,
)
đš Basic Wrap Usage
Create a simple wrap layout with chips:
import 'package:flutter/material.dart';
class WrapExample extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('Wrap Example')),
body: Padding(
padding: EdgeInsets.all(16.0),
child: Wrap(
spacing: 8.0,
runSpacing: 8.0,
children: [
Chip(label: Text('Flutter')),
Chip(label: Text('Dart')),
Chip(label: Text('Mobile')),
Chip(label: Text('Development')),
Chip(label: Text('UI')),
],
),
),
);
}
}
Output:
đš Wrap with Spacing
Control horizontal and vertical spacing:
Wrap(
spacing: 16.0, // Space between items
runSpacing: 12.0, // Space between lines
children: [
Container(
width: 80,
height: 80,
color: Colors.red,
child: Center(child: Text('1')),
),
Container(
width: 80,
height: 80,
color: Colors.green,
child: Center(child: Text('2')),
),
Container(
width: 80,
height: 80,
color: Colors.blue,
child: Center(child: Text('3')),
),
],
)
Output:
đš Wrap Alignment
Align wrapped content using alignment properties:
Wrap(
alignment: WrapAlignment.center,
spacing: 10.0,
children: [
ElevatedButton(
onPressed: () {},
child: Text('Button 1'),
),
ElevatedButton(
onPressed: () {},
child: Text('Button 2'),
),
ElevatedButton(
onPressed: () {},
child: Text('Button 3'),
),
],
)
Output:
đš Vertical Wrap
Change direction to vertical for column-based wrapping:
Wrap(
direction: Axis.vertical,
spacing: 8.0,
runSpacing: 8.0,
children: [
Chip(label: Text('Item 1')),
Chip(label: Text('Item 2')),
Chip(label: Text('Item 3')),
Chip(label: Text('Item 4')),
],
)
Output:
đš Practical Example: Tag List
Create a tag list that wraps automatically:
Wrap(
spacing: 8.0,
runSpacing: 8.0,
children: List.generate(
10,
(index) => ActionChip(
label: Text('Tag ${index + 1}'),
onPressed: () {
print('Tag ${index + 1} pressed');
},
),
),
)