Skip to content

avoid_shrink_wrap_in_scrollables

Reports shrinkWrap: true on a scrollable inside another scrollable. Make them one scrollable, such as a CustomScrollView of slivers.

Tier
advisory
Scope
Flutter
Fix
none

shrinkWrap: true on a scrollable that is inside another scrollable in the same widget expression.

  • A scrollable is any constructor of ListView, GridView, CustomScrollView or another ScrollView, of SingleChildScrollView, AnimatedList, AnimatedGrid, ReorderableList or ReorderableListView, or of a class that extends one.
  • Inside means written anywhere in an argument of the outer scrollable’s constructor call, at any depth: in a Column, in a list literal, in an itemBuilder or another builder callback.
  • A scrollable created in an event callback, such as the onPressed that opens a dialog, starts on its own: it is not inside the scrollable that holds the button.

It does not report:

  • a scrollable with no scrollable around it, such as a list in a Column, in a dialog or in a bottom sheet;
  • a shrinkWrap that is not the literal true;
  • a scrollable inside a PageView, a TabBarView, a ListWheelScrollView or a NestedScrollView;
  • a scrollable that a method or another widget builds: only the one widget expression is read.

Inside another scrollable, a list gets unbounded room along its axis, and Flutter stops with “Vertical viewport was given unbounded height”. shrinkWrap: true removes the error by making the list as large as all of its content: it builds and lays out every child at each layout, even those far off the screen, and no longer scrolls. What is meant is one scrollable: a CustomScrollView takes the content of both as slivers, and stays lazy over all of it.

The rule is advisory: a short, fixed list of three settings rows inside a scrolling page costs nothing that can be measured, and the rule cannot tell it from a list of ten thousand rows.

Violating
SingleChildScrollView(
child: Column(
children: [
const ProfileHeader(),
ListView.builder(
shrinkWrap: true,
physics: const NeverScrollableScrollPhysics(),
itemCount: orders.length,
itemBuilder: (context, index) => OrderTile(order: orders[index]),
),
],
),
)
Correctedas one scrollable made of slivers
CustomScrollView(
slivers: [
const SliverToBoxAdapter(child: ProfileHeader()),
SliverList.builder(
itemCount: orders.length,
itemBuilder: (context, index) => OrderTile(order: orders[index]),
),
],
)

An inner scrollable that is meant to scroll by itself, such as a horizontal row of cards in a vertical page, needs a size and no shrinkWrap:

SizedBox(
height: 160,
child: ListView.builder(
scrollDirection: Axis.horizontal,
itemCount: cards.length,
itemBuilder: (context, index) => CardTile(card: cards[index]),
),
)

Set under the assay: key of the project’s root analysis_options.yaml.

Rule parameter Default Effect
enabled true false switches the rule off for the whole project.
exclude [] Path globs where the rule is silent.
assay:
rules:
avoid_shrink_wrap_in_scrollables:
exclude:
- lib/legacy/**

The violation is at the argument, so the suppression goes on the line above shrinkWrap: true. Write why on the line directly above it:

// The list is the three rows of the settings group and never grows.
// ignore: assay/avoid_shrink_wrap_in_scrollables

// ignore_for_file: assay/avoid_shrink_wrap_in_scrollables silences it in a whole file, and // ignore: avoid_shrink_wrap_in_scrollables without the prefix works too. See Suppressions.

All cases

A scrollable is created by any constructor, such as ListView(…), ListView.builder(…), ListView.separated(…) or GridView.count(…), of one of these Flutter classes, or of a class of a package or of the project that extends one:

  • ScrollView, and so ListView, GridView, CustomScrollView and BoxScrollView;
  • SingleChildScrollView, AnimatedList, AnimatedGrid, ReorderableList and ReorderableListView.

Inside means that the constructor call is written somewhere in an argument of another scrollable’s constructor call. How deep does not matter, and neither does what stands between the two:

  • other widgets, such as a Column, a Padding or a SliverToBoxAdapter;
  • a list literal, with its if, for and spread elements, and a conditional expression;
  • a builder callback, a function that returns a widget or a collection of widgets: the itemBuilder of a ListView.builder, the builder of a LayoutBuilder or of a Builder, the function given to items.map(…).

The widget expression ends at a function that returns anything else, such as an event callback. A scrollable created in an onPressed, for a dialog or a bottom sheet, starts a widget expression of its own: it is not inside the scrollable that holds the button, but a shrink-wrapped list inside a SingleChildScrollView in that dialog is reported.

Where scrollables are three deep, each shrinkWrap: true with a scrollable around it is reported: the two inner ones, not the outermost.

Scrollables along different axes count too: a shrink-wrapped horizontal list in a vertical one also builds all of its children, and is better given a height. Where a widget in between gives the inner scrollable a bounded size, such as a SizedBox with a height, shrinkWrap: true is not needed and can be removed. A builder callback counts by what it returns, not by what calls it, so the list a PopupMenuButton builds in its itemBuilder is reported where the callback is written inside the scrollable.

It does not report:

  • a scrollable with no scrollable around it, such as a list in a Column, in a dialog or in a bottom sheet;
  • shrinkWrap: false, no shrinkWrap at all, or a value that is not the literal true, such as shrinkWrap: nested, shrinkWrap: !expanded or a constant;
  • two scrollables side by side, as two children of one Column;
  • a shrinkWrap argument of a widget that is no scrollable, such as SearchAnchor;
  • a scrollable inside a PageView, a TabBarView, a ListWheelScrollView or a NestedScrollView, which give their child a bounded size;
  • anything in a package that does not use Flutter.

A scrollable takes the size its parent allows, and builds only the children in view: a ListView.builder of ten thousand items builds the dozen on the screen. Inside another scrollable it gets unbounded room along its axis, and Flutter stops with “Vertical viewport was given unbounded height”.

shrinkWrap: true removes the error by making the inner scrollable as large as all of its content. To know that size, it builds and lays out every child, even those far off the screen, at each layout. The cost grows with the list: a long first frame, and jank while the outer scrollable moves. The inner one no longer scrolls either, so what is left is a plain column with the cost of a viewport.

Or, where the content above the list is small, as one list whose first item is that content:

ListView.builder(
itemCount: orders.length + 1,
itemBuilder: (context, index) => index == 0
? const ProfileHeader()
: OrderTile(order: orders[index - 1]),
)