Sub topic 15
Testing Taps and Drags with `flutter_test`
This chapter assumes basic Dart syntax, widget trees, StatefulWidget, setState, and layout with Stack and Positioned. It also assumes that you know how the button and gesture callbacks from earlier chapters work.
Flutter widget tests run widgets in a test environment. They can find widgets, send pointer input, rebuild the widget tree, and check the result. This lets you test user-visible behavior without manually using an emulator.
The interaction test cycle
Most interaction tests follow this cycle:
- Build the widget with
pumpWidget. - Find the target with a finder.
- Send an action such as
tapordrag. - Rebuild with
pumpor wait for animations withpumpAndSettle. - Check the result with
expect.
A WidgetTester controls the test environment. A finder, such as find.text or find.byKey, identifies a widget. Prefer stable keys for important interactive elements because text and widget types can change during a redesign.
A widget with tap and drag behavior
The following widget has a button that increments a counter and a box that follows horizontal drags.
import 'package:flutter/material.dart';
class InteractionDemo extends StatefulWidget {
const InteractionDemo({super.key});
@override
State<InteractionDemo> createState() => _InteractionDemoState();
}
class _InteractionDemoState extends State<InteractionDemo> {
int _count = 0;
double _x = 20;
@override
Widget build(BuildContext context) {
return Column(
children: [
FilledButton(
onPressed: () => setState(() => _count++),
child: Text('Pressed $_count times'),
),
SizedBox(
width: 240,
height: 160,
child: Stack(
children: [
Positioned(
left: _x,
top: 40,
child: GestureDetector(
key: const ValueKey('drag-box'),
onPanUpdate: (details) {
setState(() => _x += details.delta.dx);
},
child: Container(
width: 60,
height: 60,
color: Colors.indigo,
alignment: Alignment.center,
child: Text('x: ${_x.round()}'),
),
),
),
],
),
),
],
);
}
}Testing taps and drags
Put this test in test/interaction_demo_test.dart. The MaterialApp supplies the material context required by FilledButton.
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:your_app/interaction_demo.dart';
void main() {
testWidgets('tapping increments the counter', (tester) async {
await tester.pumpWidget(
const MaterialApp(home: Scaffold(body: InteractionDemo())),
);
expect(find.text('Pressed 0 times'), findsOneWidget);
await tester.tap(find.byType(FilledButton));
await tester.pump();
expect(find.text('Pressed 1 times'), findsOneWidget);
});
testWidgets('dragging moves the box', (tester) async {
await tester.pumpWidget(
const MaterialApp(home: Scaffold(body: InteractionDemo())),
);
expect(find.text('x: 20'), findsOneWidget);
await tester.drag(
find.byKey(const ValueKey('drag-box')),
const Offset(30, 0),
);
await tester.pump();
expect(find.text('x: 50'), findsOneWidget);
});
}Replace your_app with the package name in your pubspec.yaml. Run the tests with flutter test.
tester.drag starts a gesture on the center of the found widget, moves by the supplied offset, and releases it. If the widget uses an animation after the gesture, call await tester.pumpAndSettle() instead of one pump. Avoid relying on exact pixel positions unless position is the behavior being tested.
Exercise
Why is pump() needed after tester.tap() or tester.drag()?
Show answer
The input callback may call setState, but the test must pump a frame so Flutter rebuilds the widget tree before the expectation checks the new UI.