Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: cannot pop with Android back button #89

Merged
merged 4 commits into from
May 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions lib/flow_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,9 @@ class _FlowBuilderState<T> extends State<FlowBuilder<T>> {

Future<bool> _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;
Expand Down
62 changes: 62 additions & 0 deletions test/flow_builder_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<int>(
state: 0,
onGeneratePages: (state, pages) {
return <Page>[
MaterialPage<void>(
child: Builder(
builder: (context) {
return Scaffold(
body: TextButton(
key: buttonKey,
child: const Text('Button'),
onPressed: () {
Navigator.of(context).push<String>(
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__');
Expand Down