Sub topic 4
Fixing unbounded height: Expanded, shrinkWrap and nesting
This chapter assumes you know Row, Column and Expanded, and that you have built a basic ListView. It explains why a scrollable inside a Column throws an error, and it covers three fixes with their costs.
Why the error happens
Flutter lays out widgets by passing constraints down. A parent tells each child the minimum and maximum width and height it may use. The child picks a size within those limits and reports it back.
A Column lays out its children along the vertical axis. It gives each child an unbounded maximum height, because it does not yet know how much room the other children need. Most widgets handle this, since a Text or an Icon has a natural height. A vertical ListView does not. It tries to fill all the height it is offered, and an infinite height cannot be filled.
This code throws:
import 'package:flutter/material.dart';
void main() => runApp(const MaterialApp(home: BrokenScreen()));
class BrokenScreen extends StatelessWidget {
const BrokenScreen({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
body: Column(
children: [
const Text('Inbox'),
ListView.builder(
itemCount: 100,
itemBuilder: (context, index) => ListTile(title: Text('Row $index')),
),
],
),
);
}
}The console reports Vertical viewport was given unbounded height. A viewport is the window a scrollable uses to show part of its content. The same error appears for a horizontal ListView inside a Row, with "width" in place of "height".
Fix 1: Expanded
Expanded is the usual fix. It makes the Column measure its non-flexible children first, then hand the leftover height to the Expanded child as a firm, bounded constraint.
import 'package:flutter/material.dart';
void main() => runApp(const MaterialApp(home: InboxScreen()));
class InboxScreen extends StatelessWidget {
const InboxScreen({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child: Column(
children: [
const Padding(
padding: EdgeInsets.all(16),
child: Text('Inbox'),
),
Expanded(
child: ListView.builder(
itemCount: 100,
itemBuilder: (context, index) =>
ListTile(title: Text('Row $index')),
),
),
],
),
),
);
}
}The list now fills the screen below the title and keeps lazy building, so only visible rows are created. If two Expanded widgets share a Column, they split the leftover space by their flex values. Use Flexible instead when the list may be shorter than the leftover space and you do not want it forced to fill it.
The same applies to a Row: wrap a horizontal list in Expanded to bound its width.
Fix 2: a fixed size
When a list should have a set size regardless of the screen, wrap it in a SizedBox or ConstrainedBox. This is the normal way to place a horizontal list in a vertical layout, such as a carousel.
import 'package:flutter/material.dart';
class Carousel extends StatelessWidget {
const Carousel({super.key});
@override
Widget build(BuildContext context) {
return SizedBox(
height: 120,
child: ListView.builder(
scrollDirection: Axis.horizontal,
itemCount: 20,
itemBuilder: (context, index) => Card(
child: SizedBox(
width: 100,
child: Center(child: Text('Card $index')),
),
),
),
);
}
}A horizontal list inside a Column has bounded width (the screen) but unbounded height. The SizedBox supplies the missing height. Its cross-axis size is not derived from the items, so you must choose it.
Fix 3: shrinkWrap
Setting shrinkWrap: true tells the list to size itself to its content instead of filling the available space. The viewport then needs no bounded height.
import 'package:flutter/material.dart';
class ShortList extends StatelessWidget {
const ShortList({super.key});
@override
Widget build(BuildContext context) {
return Column(
children: [
const Text('Quick links'),
ListView(
shrinkWrap: true,
children: const [
ListTile(title: Text('Profile')),
ListTile(title: Text('Settings')),
ListTile(title: Text('About')),
],
),
const Text('Footer'),
],
);
}
}Here the list takes exactly the height of its three tiles, and the footer sits directly below. That is the right result for a short list.
The cost is that a shrink-wrapped viewport must know the total extent of its content. It measures every child up front, so the laziness of ListView.builder is lost. With 10,000 items, Flutter builds and lays out all 10,000 each time the list is laid out.
| Approach | Height of the list | Lazy building | Typical use |
|---|---|---|---|
| `Expanded` / `Flexible` | Space left over in the `Column` | Yes | Main list below a header |
| `SizedBox` with height | A value you choose | Yes | Horizontal carousels, fixed panels |
| `shrinkWrap: true` | Sum of all item heights | No | Short lists inside other layouts |
Nesting scrollables
Sometimes the whole page should scroll, with a list inside it. You might place a ListView inside a SingleChildScrollView, or inside another ListView. The parent gives its child unbounded height along the scroll axis, so the inner list hits the same error, and shrinkWrap is the common fix. A second problem follows: two vertical scrollables compete for drag gestures. The inner list captures touches, so the user scrolls the small list instead of the page. Turn the inner list's scrolling off:
import 'package:flutter/material.dart';
class ProfilePage extends StatelessWidget {
const ProfilePage({super.key});
@override
Widget build(BuildContext context) {
return SingleChildScrollView(
child: Column(
children: [
const SizedBox(
height: 200,
child: Center(child: Text('Header')),
),
ListView.builder(
shrinkWrap: true,
physics: const NeverScrollableScrollPhysics(),
itemCount: 10,
itemBuilder: (context, index) =>
ListTile(title: Text('Setting $index')),
),
],
),
);
}
}NeverScrollableScrollPhysics makes the inner list ignore drags, so the outer scroll view handles them. A Column inside a SingleChildScrollView also receives unbounded height, so Expanded cannot be used here. Using it throws "RenderFlex children have non-zero flex but incoming height constraints are unbounded".
This pattern has the same cost as any shrinkWrap use: every item is built. If the inner list is long, switch to a single CustomScrollView with slivers, covered in a later chapter. It treats the header, list and grid as parts of one scrollable and keeps lazy building for all of them.
A scrollable inside a scrollable on the same axis is a design warning. Two scrollables on different axes are fine. A vertical page containing horizontal carousels is common, and each carousel needs a fixed height as in Fix 2.
Choosing a fix
- If the list is the main content of the area, put it in
Expanded. - If it has a fixed visual size, such as a carousel, use
SizedBox. - If it is short and sits among other content, use
shrinkWrap: true. When it is also inside another scrollable, addNeverScrollableScrollPhysics. - If you need a long list, a header and a grid in one scroll, use
CustomScrollView.
GridView follows the same rules, because it is also a vertical viewport. Expanded, SizedBox and shrinkWrap fix it in the same way.
Exercise
A screen has a Column with a title Text, a horizontal ListView of 20 category chips, and a vertical ListView.builder of 500 products. The app throws an unbounded-height error. Which wrapper does each list need, and why should neither list use shrinkWrap?
Show answer
Wrap the horizontal chip list in a SizedBox with a fixed height, such as 48, because the Column gives it unbounded height and its cross-axis size is not derived from the chips. Wrap the product list in Expanded so it takes the remaining height. Neither should use shrinkWrap. For the 500 products it would force all items to be built and measured, and it would lose lazy building. The chip list does not need it, because the SizedBox already bounds it.