diff --git a/example/lib/main.dart b/example/lib/main.dart index a30cf7a..de5da50 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -8,7 +8,7 @@ import 'package:flutter_bloc/flutter_bloc.dart'; void main() => runApp(MyApp(locationRepository: LocationRepository())); class MyApp extends StatelessWidget { - MyApp({super.key, required LocationRepository locationRepository}) + MyApp({required LocationRepository locationRepository}) : _locationRepository = locationRepository; final LocationRepository _locationRepository; diff --git a/lib/flow_builder.dart b/lib/flow_builder.dart index c2819f6..8d27c66 100644 --- a/lib/flow_builder.dart +++ b/lib/flow_builder.dart @@ -136,9 +136,9 @@ class _FlowBuilderState extends State> { Future _pop() async { if (mounted) { - final popHandled = await _navigator?.maybePop(_state) ?? false; + final popHandled = await _navigator?.maybePop() ?? false; if (popHandled) return true; - if (!_canPop) return await Navigator.of(context).maybePop(_state); + if (!_canPop) return await Navigator.of(context).maybePop(); return false; } return false; diff --git a/test/flow_builder_test.dart b/test/flow_builder_test.dart index c43987f..61ed7e8 100644 --- a/test/flow_builder_test.dart +++ b/test/flow_builder_test.dart @@ -624,6 +624,68 @@ void main() { expect(find.byKey(scaffoldKey), findsNothing); }); + testWidgets('system back button pops typed routes that have been pushed', + (tester) async { + var systemPopCallCount = 0; + SystemChannels.platform.setMockMethodCallHandler((call) { + if (call.method == 'SystemNavigator.pop') { + systemPopCallCount++; + } + return null; + }); + const buttonKey = Key('__button__'); + const scaffoldKey = Key('__scaffold__'); + await tester.pumpWidget( + MaterialApp( + home: FlowBuilder( + state: 0, + onGeneratePages: (state, pages) { + return [ + MaterialPage( + child: Builder( + builder: (context) { + return Scaffold( + body: TextButton( + key: buttonKey, + child: const Text('Button'), + onPressed: () { + Navigator.of(context).push( + MaterialPageRoute( + builder: (context) => const Scaffold( + key: scaffoldKey, + ), + ), + ); + }, + ), + ); + }, + ), + ), + ]; + }, + ), + ), + ); + expect(find.byKey(buttonKey), findsOneWidget); + expect(find.byKey(scaffoldKey), findsNothing); + + await tester.tap(find.byKey(buttonKey)); + await tester.pumpAndSettle(); + + expect(find.byKey(buttonKey), findsNothing); + expect(find.byKey(scaffoldKey), findsOneWidget); + + await TestSystemNavigationObserver.handleSystemNavigation( + const MethodCall('popRoute'), + ); + await tester.pumpAndSettle(); + + expect(systemPopCallCount, equals(0)); + expect(find.byKey(buttonKey), findsOneWidget); + expect(find.byKey(scaffoldKey), findsNothing); + }); + testWidgets('Navigator.pop pops parent route', (tester) async { const button1Key = Key('__button1__'); const button2Key = Key('__button2__');