Sub topic 1
How scrolling works in Flutter: scrollables and viewports
Prerequisites
This chapter assumes basic Dart syntax, including lists and closures. You should also know how to build StatelessWidget and StatefulWidget classes, use Row, Column, and Expanded, and run a Flutter app with hot reload.
The working model
A scrollable widget displays content that is larger than the space available on screen. Flutter divides this job among several parts:
- A scrollable accepts user gestures and changes a scroll offset.
- A viewport is the visible window through which the content is seen.
- A scroll position records how far the content has moved.
- A scrollable child supplies the content, such as rows in a
ListViewor tiles in aGridView.
The viewport does not need to show every child at once. As the user scrolls, Flutter lays out the children that are visible and nearby. This approach is called lazy building. It keeps a long collection from creating thousands of widgets immediately.
ListView and GridView
ListView arranges children along one axis, usually vertically. GridView arranges children in rows and columns. Both provide scrolling behavior and a viewport.
Use the default ListView constructor when you already have a short, fixed list of widgets. Use ListView.builder when items come from data or the collection may be long. The builder receives an index and creates an item when Flutter needs it.
import 'package:flutter/material.dart';
void main() {
runApp(const MaterialApp(home: MessagePage()));
}
class MessagePage extends StatelessWidget {
const MessagePage({super.key});
@override
Widget build(BuildContext context) {
final messages = List<String>.generate(
1000,
(index) => 'Message ${index + 1}',
);
return Scaffold(
appBar: AppBar(title: const Text('Messages')),
body: ListView.builder(
itemCount: messages.length,
itemBuilder: (context, index) {
return ListTile(
leading: const Icon(Icons.mail),
title: Text(messages[index]),
);
},
),
);
}
}The itemCount tells the viewport how many items exist. The itemBuilder creates only the items needed for the current viewport and its cache area. The visible range changes as the scroll offset changes.
| Widget | Layout | Typical choice |
|---|---|---|
| `ListView` | One-dimensional list | Short, known widget list |
| `ListView.builder` | One-dimensional list | Long or data-driven list |
| `GridView` | Rows and columns | Small, known grid |
| `GridView.builder` | Rows and columns | Large or data-driven grid |
Constraints determine whether scrolling works
A scrollable needs a bounded size in its scrolling direction. For example, a vertical ListView must know its height. A Column can provide unbounded vertical space, so placing a ListView directly inside it often causes an unbounded-height error.
Give the list the remaining height with Expanded:
Column(
children: [
const Text('Inbox'),
Expanded(
child: ListView.builder(
itemCount: 100,
itemBuilder: (context, index) {
return ListTile(title: Text('Row $index'));
},
),
),
],
)Exercise
Why is ListView.builder usually better than a manually created list for 1,000 data items?
Show answer
It builds items on demand near the viewport instead of creating all 1,000 item widgets immediately.