Sub topic 12
Testing Navigation Between Screens
Assume you know basic Dart, async and await, Flutter widgets, and how to run flutter test. This chapter uses testWidgets, WidgetTester, finders, matchers, and frame pumping.
A navigation test checks that a user action changes the current screen correctly. It usually verifies three things:
- The starting screen is visible.
- An interaction, such as a tap, starts navigation.
- The destination screen appears, and returning goes back to the starting screen.
Flutter navigation is managed by a Navigator. A navigator keeps a stack of routes. A route represents a screen or page. Navigator.push adds a route to the stack. Navigator.pop removes the top route and reveals the previous screen.
Build a small navigable app
The test below defines two screens and checks the complete navigation path. The MaterialApp provides a navigator and the material theme needed by the buttons.
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
class HomeScreen extends StatelessWidget {
const HomeScreen({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Home')),
body: Center(
child: ElevatedButton(
onPressed: () {
Navigator.of(context).push(
MaterialPageRoute<void>(
builder: (context) => const DetailsScreen(),
),
);
},
child: const Text('Open details'),
),
),
);
}
}
class DetailsScreen extends StatelessWidget {
const DetailsScreen({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Details')),
body: Center(
child: ElevatedButton(
onPressed: () => Navigator.of(context).pop(),
child: const Text('Back to home'),
),
),
);
}
}
void main() {
testWidgets('opens details and returns home', (tester) async {
await tester.pumpWidget(
const MaterialApp(home: HomeScreen()),
);
expect(find.text('Home'), findsOneWidget);
expect(find.text('Open details'), findsOneWidget);
await tester.tap(find.text('Open details'));
await tester.pumpAndSettle();
expect(find.text('Details'), findsOneWidget);
expect(find.text('Open details'), findsNothing);
await tester.tap(find.text('Back to home'));
await tester.pumpAndSettle();
expect(find.text('Home'), findsOneWidget);
expect(find.text('Details'), findsNothing);
});
}Pump after navigation
tap sends the interaction, but it does not always finish the resulting route transition. pumpAndSettle repeatedly advances frames until there are no scheduled frames. This makes it useful for navigation animations.
Use pump when you need to inspect a specific point in a transition. For example, await tester.pump(const Duration(milliseconds: 200)); advances simulated time by 200 milliseconds. Use pumpAndSettle when the final screen is what matters.
Testing named routes
Applications often configure named routes instead of constructing MaterialPageRoute directly. The test still follows the same pattern: build the app, tap a control, pump, and assert the destination.
You can also verify a back action with tester.pageBack(). This simulates the platform back operation and is useful when the screen has no visible back button.
Exercise
Exercise
Why should a navigation test usually call pumpAndSettle after tapping a button that pushes a route?
Show answer
The tap starts navigation, but the route transition may schedule additional animation frames. pumpAndSettle advances those frames until the destination is stable.