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
What it reports
Section titled “What it reports”shrinkWrap: true on a scrollable that is inside another scrollable in the same widget expression.
- A scrollable is any constructor of
ListView,GridView,CustomScrollViewor anotherScrollView, ofSingleChildScrollView,AnimatedList,AnimatedGrid,ReorderableListorReorderableListView, 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 anitemBuilderor another builder callback. - A scrollable created in an event callback, such as the
onPressedthat 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
shrinkWrapthat is not the literaltrue; - a scrollable inside a
PageView, aTabBarView, aListWheelScrollViewor aNestedScrollView; - 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.
Example
Section titled “Example”SingleChildScrollView( child: Column( children: [ const ProfileHeader(), ListView.builder( shrinkWrap: true, physics: const NeverScrollableScrollPhysics(), itemCount: orders.length, itemBuilder: (context, index) => OrderTile(order: orders[index]), ), ], ),)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]), ),)Rule parameters
Section titled “Rule parameters”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/**Suppressing it
Section titled “Suppressing it”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.
Details
Section titled “Details”All cases
What counts
Section titled “What counts”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 soListView,GridView,CustomScrollViewandBoxScrollView;SingleChildScrollView,AnimatedList,AnimatedGrid,ReorderableListandReorderableListView.
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, aPaddingor aSliverToBoxAdapter; - a list literal, with its
if,forand spread elements, and a conditional expression; - a builder callback, a function that returns a widget or a collection of widgets: the
itemBuilderof aListView.builder, thebuilderof aLayoutBuilderor of aBuilder, the function given toitems.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.
What it does not report
Section titled “What it does not report”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, noshrinkWrapat all, or a value that is not the literaltrue, such asshrinkWrap: nested,shrinkWrap: !expandedor a constant;- two scrollables side by side, as two children of one
Column; - a
shrinkWrapargument of a widget that is no scrollable, such asSearchAnchor; - a scrollable inside a
PageView, aTabBarView, aListWheelScrollViewor aNestedScrollView, which give their child a bounded size; - anything in a package that does not use Flutter.
What it costs
Section titled “What it costs”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.
One list instead
Section titled “One list instead”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]),)