Sub topic 10
Reading and Driving Scroll Position with ScrollController
Assume you know basic Dart, StatefulWidget, widget lifecycle methods, and ListView.builder. A ScrollController is an object that lets you read a scrollable's position and change it programmatically.
How the controller works
A scrollable widget owns a ScrollPosition. The position describes how far the content has moved. A ScrollController connects your code to that position.
Useful values include:
pixels: the current scroll offset.maxScrollExtent: the largest valid offset.extentBefore: the content above the viewport.extentAfter: the content below the viewport.hasClients: whether the controller is currently attached to a scrollable.
Pass the controller to ListView, GridView, or another compatible scrollable through its controller property.
import 'package:flutter/material.dart';
class MessagesPage extends StatefulWidget {
const MessagesPage({super.key});
@override
State<MessagesPage> createState() => _MessagesPageState();
}
class _MessagesPageState extends State<MessagesPage> {
final ScrollController _controller = ScrollController();
final List<String> _messages =
List<String>.generate(30, (index) => 'Message $index');
@override
void initState() {
super.initState();
_controller.addListener(_onScroll);
}
void _onScroll() {
if (!_controller.hasClients) {
return;
}
debugPrint(
'offset: ${_controller.position.pixels}, '
'remaining: ${_controller.position.extentAfter}',
);
}
void _scrollToTop() {
if (!_controller.hasClients) {
return;
}
_controller.animateTo(
0,
duration: const Duration(milliseconds: 300),
curve: Curves.easeOut,
);
}
@override
void dispose() {
_controller.removeListener(_onScroll);
_controller.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Messages'),
actions: [
IconButton(
onPressed: _scrollToTop,
icon: const Icon(Icons.vertical_align_top),
),
],
),
body: ListView.builder(
controller: _controller,
itemCount: _messages.length,
itemBuilder: (context, index) {
return ListTile(title: Text(_messages[index]));
},
),
);
}
}jumpTo changes the offset immediately. animateTo moves there over time and accepts a duration and animation curve. Both require a valid attached position, so check hasClients when the call might happen before the first layout or after the widget is removed.
Reacting near the end
A listener runs whenever the position changes. This makes it useful for pagination, which loads another page of data when the user approaches the end.
Common lifecycle and layout errors
Create the controller once as a State field, not inside build. Dispose it in dispose to release its resources. Do not read controller.position until the controller has clients.
A controller also does not remove the need for bounded layout constraints. A scrollable inside a Column still needs Expanded, Flexible, or another finite height.
Exercise
Which property should you check before reading controller.position, and which method should you call when the State is disposed?
Show answer
Check controller.hasClients before reading the position, and call controller.dispose() in dispose.