Sub topic 8
Scrolling Lists and Dragging
Assume you know basic Dart, Flutter widgets, testWidgets, pumpWidget, finders, and matchers. This chapter focuses on scrollable content, especially lists that build items only when they are near the viewport.
Why scrolling needs special handling
A ListView.builder creates list items lazily. It does not build all 100 items when the test starts. It usually builds only the items visible on screen and a small area around them.
Because of this, a finder may return no result for an item that exists logically but has not been built yet. Scroll until the item becomes visible, then assert on it.
Scrolling to a specific item
scrollUntilVisible repeatedly scrolls a scrollable widget until the target finder matches a visible item. Its second argument is the scroll distance for each attempt.
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
void main() {
testWidgets('finds a lazily built list item', (tester) async {
await tester.pumpWidget(
MaterialApp(
home: Scaffold(
body: ListView.builder(
itemCount: 100,
itemBuilder: (context, index) {
return ListTile(title: Text('Item $index'));
},
),
),
),
);
expect(find.text('Item 80'), findsNothing);
await tester.scrollUntilVisible(
find.text('Item 80'),
500,
);
expect(find.text('Item 80'), findsOneWidget);
});
}The first assertion confirms that Item 80 is not initially in the widget tree. The scroll operation moves the list by up to 500 logical pixels per attempt. The final assertion checks that the item is now built and visible.
scrollUntilVisible is often clearer than manually calculating how far a list must move. It also works when row heights change.
Simulating a drag
Use tester.drag when the test should model a user gesture. Pass the finder for the scrollable widget and an offset. A negative vertical offset moves content upward, revealing items farther down the list.
await tester.drag(
find.byType(ListView),
const Offset(0, -400),
);
await tester.pump();
expect(find.text('Item 20'), findsOneWidget);This approach is useful when you need to test behavior caused by a particular gesture, such as a refresh threshold, a collapsing header, or a custom scroll effect. It is less predictable than scrollUntilVisible when the target may be far away.
| Operation | Best use | Main consideration |
|---|---|---|
| `scrollUntilVisible` | Reaching a known target | Does not model one exact gesture |
| `drag` | Testing gesture-driven behavior | May require several drags |
| `fling` | Testing fast scrolling | Depends on velocity and physics |
Common errors
- Searching for a far-off item before scrolling. The item may not exist in the tree yet.
- Dragging the wrong finder. Target the
ListViewor a parent scrollable, not an individual row. - Forgetting
await. Scrolling and dragging are asynchronous tester operations. - Expecting an exact position after a drag. Use
scrollUntilVisiblewhen visibility is the real requirement. - Using a large fixed drag in a test that should remain stable across different row sizes.
> Note > Prefer semantic targets such as a row key or unique text. Avoid relying on a fixed number of drags when the test only needs to reach an item.
Exercise
Which operation should you choose to verify that Item 75 appears in a long ListView.builder?
Show answer
Use scrollUntilVisible(find.text('Item 75'), delta) because it scrolls until the lazily built item is visible instead of depending on a fixed gesture distance.