Skip to content

Commit 9c05312

Browse files
authored
Merge pull request #413 from cvolzke4/back_button_over_dialog
Allow back button to be pressed on dialog displayed over web view.
2 parents 1d1daeb + 2fda5d6 commit 9c05312

File tree

1 file changed

+18
-6
lines changed

1 file changed

+18
-6
lines changed

lib/src/webview_scaffold.dart

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,13 +79,15 @@ class _WebviewScaffoldState extends State<WebviewScaffold> {
7979
_onBack = webviewReference.onBack.listen((_) async {
8080
if (!mounted) return;
8181

82-
// Equivalent of Navigator.maybePop(), except that [webviewReference]
83-
// is closed when the pop goes ahead. Whether the pop was performed
84-
// can't be determined from the return value of Navigator.maybePop().
85-
final route = ModalRoute.of(context);
86-
final pop = await route?.willPop();
82+
// The willPop/pop pair here is equivalent to Navigator.maybePop(),
83+
// which is what's called from the flutter back button handler.
84+
final pop = await _topMostRoute.willPop();
8785
if (pop == RoutePopDisposition.pop) {
88-
webviewReference.close();
86+
// Close the webview if it's on the route at the top of the stack.
87+
final isOnTopMostRoute = _topMostRoute == ModalRoute.of(context);
88+
if (isOnTopMostRoute) {
89+
webviewReference.close();
90+
}
8991
Navigator.pop(context);
9092
}
9193
});
@@ -100,6 +102,16 @@ class _WebviewScaffoldState extends State<WebviewScaffold> {
100102
}
101103
}
102104

105+
/// Equivalent to [Navigator.of(context)._history.last].
106+
Route<dynamic> get _topMostRoute {
107+
var topMost;
108+
Navigator.popUntil(context, (route) {
109+
topMost = route;
110+
return true;
111+
});
112+
return topMost;
113+
}
114+
103115
@override
104116
void dispose() {
105117
super.dispose();

0 commit comments

Comments
 (0)