I’m currently developing an app which uses the flutter_split_view
plugin to automatically display split view. There’s one main annoyance, though, pressing the Android’s native back button from the child screen (i.e. the right widget) simply closes the app.
I have tried using WillPopScope
to call SplitView.of(context)
on the child screen, because the SplitView
constructor does not accept external controllers (e.g. TabController
for tabs) which I could call to redirect the back button calls to the SplitView
instead.
import 'package:flutter/material.dart';
import 'package:flutter_split_view/flutter_split_view.dart';
class ChildPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
SplitViewState splitView = SplitView.of(context);
return WillPopScope(
onWillPop: () async {
splitView.pop();
return false;
},
child: Scaffold(...),
);
}
}
Is there a way to solve this?
2
Answers
You can use the WillPopScope widget to capture the back button press event, and then use the splitView.pop() method to navigate to the previous screen, instead of closing the app.
By wrapping the Scaffold widget with a WillPopScope widget, you can intercept the back button press event, and then use the splitView.pop() method to navigate to the previous screen. The onWillPop callback should return Future, returning false will prevent the back button from doing its default action, in this case close the app. And that way you can redirect the back button calls to the SplitView instead.
This should work as expected if implemented correctly. However, you should test it on the actual device to make sure the behavior is the one you expect.