ListView
A scrollable list. It comes in two forms:
ListView
— the children are listed right in the markup, and
ListView.builder
— the rows are built from a template macro
@Macros id:itemBuilder
, usually from data. For long and dynamic lists use
ListView.builder
.
Type:
multi-child
(
ListView
),
multi-child
with a template macro (
ListView.builder
)
SizedBox height:200
ListView
Text "First row"
Text "Second row"
Text "Third row"
A list, like any scrolling element, must get a bounded height (or width when it scrolls horizontally): inside a Column wrap it in Expanded , or set its size with a SizedBox . More in Bad practices.
Parameters
Common to both forms:
| Parameter | Type | Default | Description |
|---|---|---|---|
scrollDirection
|
string | vertical |
Direction:
vertical
,
horizontal
|
reverse
|
bool | false | Start from the end (the first row is at the bottom or on the right) |
shrinkWrap
|
bool | false | Take only as much space as the content needs, not all that is available |
physics
|
string | always |
Response to gestures:
never
(do not scroll),
bouncing
,
clamp
,
always
.
never
passes the mouse wheel to the outer scrolling element — needed for a nested list
|
itemExtent
|
double | — | Fixed row size along the main axis — faster than measuring every row |
clipBehavior
|
string | hardEdge | Clipping to the list bounds |
keyboardDismissBehavior
|
string | manual |
onDrag
— hide the on-screen keyboard when the list is dragged
|
ListView.builder
A list whose rows are built from the template
@Macros id:itemBuilder
— one for each index from
0
to
itemCount - 1
, and only for the rows the scrolling has reached. This is the form for long lists and lists built from data.
Type:
multi-child
with a template macro
SizedBox height:240
ListView.builder itemCount:100
@Macros id:itemBuilder index:0
Text $"Row ${index}"
The parameters shared with
ListView
—
scrollDirection
,
reverse
,
shrinkWrap
,
physics
,
itemExtent
,
clipBehavior
,
keyboardDismissBehavior
and
#padding
— are described in the
Parameters
table and work the same way. Only on
ListView.builder
:
| Parameter | Type | Default | Description |
|---|---|---|---|
itemCount
|
int | 0 | How many rows to build |
keepVisible
|
int | — | Index of the row the list keeps on screen: when it changes, the list scrolls to it as little as needed |
keepVisibleAlign
|
edge
|
center
|
edge
|
Where
keepVisible
puts the row:
edge
— scroll as little as possible, to the nearer edge in the direction of the step;
center
— the middle of the row at the middle of the list (a tab strip). At the ends the list stops at its bounds. On the first frame the list is already at the row
|
The row template, the separator
id:separatorBuilder
and rows from data are covered in
Children
; examples with
keepVisible
are in
Examples
. What of this works on the site is in
Differences
. Inside a
CustomScrollView
use
SliverList.builder
instead.
Nested structures
#padding
Padding inside the scroll area: the content keeps away from the edges, but scrolls all the way to the edge.
SizedBox height:160
ListView
#padding symmetric horizontal:16 vertical:8
Text "Row 1"
Text "Row 2"
Text "Row 3"
Children
In
ListView
the children are the rows themselves.
In
ListView.builder
there is one child — the macro
@Macros id:itemBuilder
. Its body is the row template, and the
index
parameter (declared as
index:0
) receives the row number every time a row is built. A second macro with
id:separatorBuilder
turns the list into a list "with separators": it is placed
between
rows (
itemCount - 1
times), but not after the last one.
SizedBox height:240
ListView.builder itemCount:20
@Macros id:itemBuilder index:0
Padding
#padding all 12
Text $"Row ${index}"
@Macros id:separatorBuilder index:0
Divider
A list from data
Rows usually come from a data generator. The macro receives only
index
; fields are read by index at the place where they are used, always with
??
— until the data has loaded, the value is
null
:
JsonData src:"data/products.json" data:@products count:@count
Center id:loading
CircularProgressIndicator
SizedBox id:ready height:320
ListView.builder itemCount:$count
@Macros id:itemBuilder index:0
Padding
#padding all 12
Text {products[index]['title'] ?? ""}
Filter and sort with generators (
FilterData
) before the list, not by hiding rows inside the template: hidden rows break counters and scrolling.
Examples
Horizontal strip
SizedBox height:96
ListView scrollDirection:horizontal
SizedBox width:120
ColoredBox color:#4f46e5
SizedBox width:120
ColoredBox color:#7c3aed
SizedBox width:120
ColoredBox color:#059669
List under a header
Column
Padding
#padding all 16
Text "Heading"
Expanded
ListView.builder itemCount:50
@Macros id:itemBuilder index:0
Padding
#padding symmetric horizontal:16 vertical:8
Text $"Item ${index}"
Selected row stays on screen
@sel:0
Column
SizedBox height:200
ListView.builder itemCount:30 keepVisible:$sel
@Macros id:itemBuilder index:0
Text $"Row ${index}"
ElevatedButton onPressed:{sel = sel + 1}
Text "Next"
Tab strip: the selected tab in the middle
@cur:0
SizedBox height:44
ListView.builder itemCount:7 scrollDirection:horizontal keepVisible:$cur keepVisibleAlign:"center"
@Macros id:itemBuilder index:0
Padding
#padding only right:8
ElevatedButton onPressed:(){cur = index}
Text $"Step ${index + 1}"
The selected tab moves to the middle of the strip; at the first and the last tab the strip stops at its edge. After a reload (with the selection kept in
Storage
) the strip is already in place, with no animation.
Differences between the app and the site
On the site the list becomes an HTML list with scrolling, and not all parameters work there:
ListView
|
ListView.builder
|
|
|---|---|---|
scrollDirection
|
works | works |
#padding
|
works | does not work |
itemExtent
|
works | does not work |
reverse
,
shrinkWrap
,
physics
,
clipBehavior
|
do not work | do not work |
keyboardDismissBehavior
|
a page has no on-screen keyboard of its own — meaningless | the same |
keepVisible
,
keepVisibleAlign
|
— | work |
separator
id:separatorBuilder
|
— | not drawn: the page has only the rows |
On the site all rows of a
ListView.builder
go into the page at once, so do not build a list of thousands of rows for something a search engine should read as a page.
Bad practices
A list without a bounded height
// BAD — Column gives the ListView an unbounded height: in the app this is a layout error
Column
Text "Heading"
ListView
Text "Row 1"
Text "Row 2"
// GOOD — Expanded gives the list the remaining space
Column
Text "Heading"
Expanded
ListView
Text "Row 1"
Text "Row 2"
Hiding rows instead of filtering data
// BAD — the row is hidden but still counts in itemCount: counters and the "empty" state lie
ListView.builder itemCount:20
@Macros id:itemBuilder index:0
Visibility visible:{index % 2 == 0}
Text $"Row ${index}"
Filter the data with a generator (
FilterData
) and take
itemCount
from its
count
.
Best practices
-
Set
itemExtentif the rows have the same height: the list does not spend time measuring them. -
A nested list
(inside another scrolling element) —
shrinkWrap:trueandphysics:never. -
Do not copy a macro by hand in a loop
: whatever repeats hundreds of times is output with
ListView.builder. - If you need focus navigation with a remote control, see ReeelScroll.
See also
- SingleChildScrollView — scrolling a single child
- GridView — the same idea, as a grid
-
Expanded
— how to give a list space inside a
Column - ReeelScroll — a list with focus and navigation