Overflow Fix: Wrap
Part of the Layout That Fits section of Coddy's Flutter journey. Lesson 8 of 30.
Some rows hold items that should flow like words in a sentence: tags, filters, chips. When there are too many for one line, a Row overflows on the right.
Wrap takes the same children: list, and when the next child does not fit on the line, it starts a new line under it:
Wrap(
children: [
for (final tag in tags) Text(tag),
],
)That is the third fix for an overflow. Use Expanded when one child should shrink, a scroll view when the page is longer than the screen, and Wrap when the children should move to the next line.
A Wrap keeps each child at the size it asks for. It never squeezes them; it only moves them down.
Challenge
EasyThe tags overflow on the right. Make them flow onto as many lines as they need, like words in a paragraph.
Try it yourself
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
final tags = ['Dart', 'Flutter', 'Mobile', 'Design', 'Games', 'Web'];
return MaterialApp(
debugShowCheckedModeBanner: false,
home: Scaffold(
appBar: AppBar(title: const Text('Interests')),
body: Padding(
padding: const EdgeInsets.all(16),
child: Row(
children: [
for (final tag in tags)
Container(
width: 110,
height: 40,
decoration: BoxDecoration(
color: Colors.purple.shade100,
borderRadius: BorderRadius.circular(20),
),
child: Center(child: Text(tag)),
),
],
),
),
),
);
}
}
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Layout That Fits
1Sharing a Row
Expanded Fills the RowSharing Space With flexFlexible Hugs Its ChildSpacer Pushes Apart2The Overflow Error
The Overflow ErrorOverflow Fix: ExpandedOverflow Fix: ScrollingOverflow Fix: WrapSpacing Inside a WrapPractice on your own: Online Flutter compiler