Sub topic 14
When Row and Column Are Not Enough: Wrap and Scrolling
This chapter assumes basic Dart syntax, StatelessWidget, the build method, and a working Flutter project. It also assumes you understand that a parent sends constraints to its child and the child returns a size.
Why a single Row or Column fails
A Row keeps its children on one horizontal line. If their total width exceeds the available width, Flutter reports a RenderFlex overflow error. A Column has the same problem vertically when its content is taller than the available height.
Choose the layout based on the content:
| Content | Suitable widget | Behavior |
|---|---|---|
| A short, fixed horizontal group | `Row` | Keeps all children on one line |
| A variable group of small items | `Wrap` | Moves items to new lines |
| A long vertical list | `ListView` | Scrolls in one direction |
| A grid or two-dimensional collection | `GridView` | Scrolls rows and columns |
Use Wrap for variable-width items
Wrap lays out children along its horizontal direction. When the next child does not fit, it starts a new run, which is a line of children. spacing adds space between items in the same run. runSpacing adds space between runs.
This is useful for tags, filter chips, buttons, and other items whose widths vary.
import 'package:flutter/material.dart';
void main() {
runApp(const MaterialApp(home: TagPage()));
}
class TagPage extends StatelessWidget {
const TagPage({super.key});
@override
Widget build(BuildContext context) {
const tags = ['Dart', 'Flutter', 'Layouts', 'Responsive UI', 'Testing'];
return Scaffold(
appBar: AppBar(title: const Text('Topics')),
body: Padding(
padding: const EdgeInsets.all(16),
child: Wrap(
spacing: 8,
runSpacing: 8,
children: [
for (final tag in tags) Chip(label: Text(tag)),
],
),
),
);
}
}The Wrap itself receives the width of the padded body. It measures each Chip and places as many as fit. Do not use Wrap as a substitute for a very long list. It creates all its children at once and does not provide scrolling.
Scroll content with ListView
A ListView is a scrollable list. It is usually the right choice when a Column would become taller than the screen. Use ListView.builder for many or dynamic items because it builds visible children as needed.
ListView.builder(
padding: const EdgeInsets.all(16),
itemCount: 100,
itemBuilder: (context, index) {
return ListTile(
leading: CircleAvatar(child: Text('${index + 1}')),
title: Text('Item ${index + 1}'),
);
},
)For a two-dimensional collection, use GridView.builder:
GridView.builder(
padding: const EdgeInsets.all(16),
gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 2,
crossAxisSpacing: 12,
mainAxisSpacing: 12,
),
itemCount: 20,
itemBuilder: (context, index) {
return Card(child: Center(child: Text('Card $index')));
},
)The common unbounded-height error
A scrollable widget needs a bounded size in its scroll direction. A ListView inside a Column often fails because the Column gives it unbounded height. Wrap the list in Expanded so it receives the remaining screen height.
Column(
children: [
const Padding(
padding: EdgeInsets.all(16),
child: Text('Recent items'),
),
Expanded(
child: ListView(
children: const [
ListTile(title: Text('First')),
ListTile(title: Text('Second')),
],
),
),
],
)Use shrinkWrap: true only when a short scrollable section must measure itself inside another layout. It can require more work because the list measures its contents. Avoid nesting scrollables in the same direction unless you deliberately configure their scrolling behavior.
Exercise
A row of filter chips overflows on a narrow phone. Should you use ListView or Wrap?
Show answer
Use Wrap when the chips may move onto multiple lines. Use a horizontal ListView only when the chips must remain on one line and should scroll sideways.