Sub topic 4
Fixing Unbounded Height: Expanded, shrinkWrap and Nesting
The constraint problem
This chapter assumes you know basic Dart, StatelessWidget, StatefulWidget, build, Column, and Expanded. You should also be able to run a Flutter app and use hot reload.
Flutter layout uses constraints. A parent tells each child how much space it may use. A vertical ListView is a scrollable viewport, so it needs a finite height. It must know the height of the area in which it will scroll.
A Column often gives its non-flex children unlimited height. This creates a conflict:
Columnsays, “Use as much vertical space as you need.”ListViewsays, “I need a finite viewport height.”
Flutter may report an error such as Vertical viewport was given unbounded height.
Use Expanded for the main scrollable
Use Expanded when the list should fill the space left after the other children. This is the usual solution for a screen with a heading, toolbar, or buttons above a list.
import 'package:flutter/material.dart';
void main() {
runApp(const MaterialApp(home: InboxPage()));
}
class InboxPage extends StatelessWidget {
const InboxPage({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Inbox')),
body: Column(
children: [
const Padding(
padding: EdgeInsets.all(16),
child: Align(
alignment: Alignment.centerLeft,
child: Text('Messages'),
),
),
Expanded(
child: ListView.builder(
itemCount: 30,
itemBuilder: (context, index) {
return ListTile(title: Text('Message $index'));
},
),
),
],
),
);
}
}Expanded is a flex widget. It tells the Column to allocate its remaining bounded height to the list. The list can then scroll inside that area.
Use shrinkWrap for a small nested list
shrinkWrap: true tells a scrollable to size itself from its contents instead of expanding along its scrolling direction.
Column(
children: [
const Text('Recent labels'),
ListView.builder(
shrinkWrap: true,
physics: const NeverScrollableScrollPhysics(),
itemCount: 3,
itemBuilder: (context, index) {
return ListTile(title: Text('Label $index'));
},
),
],
)This pattern is useful when the inner list is short and the outer layout owns scrolling. NeverScrollableScrollPhysics prevents two vertical scrollables from competing for gestures.
shrinkWrap has a cost. Flutter must measure more of the inner list to determine its size, so avoid it for large or frequently changing lists. Prefer one main scrollable when possible.
Avoid competing scrollables
Nesting two vertical ListView widgets usually causes layout or scrolling problems. Choose one owner for vertical scrolling:
- Put fixed content and the list in a
Column, then wrap the list withExpanded. - Put a small inner list in a parent scrollable with
shrinkWrapand disabled inner scrolling. - For large mixed content, use one
CustomScrollViewwith slivers.
Exercise
A ListView inside a Column shows an unbounded-height error. What is the usual fix when the list should fill the remaining screen space?
Show answer
Wrap the ListView with Expanded so Flutter gives it a finite height.