Explorar o código

Merge pull request #413 from cvolzke4/back_button_over_dialog

Allow back button to be pressed on dialog displayed over web view.
Rafal Wachol %!s(int64=6) %!d(string=hai) anos
pai
achega
ea28244684
Modificáronse 1 ficheiros con 18 adicións e 6 borrados
  1. 18 6
      lib/src/webview_scaffold.dart

+ 18 - 6
lib/src/webview_scaffold.dart

@@ -79,13 +79,15 @@ class _WebviewScaffoldState extends State<WebviewScaffold> {
     _onBack = webviewReference.onBack.listen((_) async {
       if (!mounted) return;
 
-      // Equivalent of Navigator.maybePop(), except that [webviewReference]
-      // is closed when the pop goes ahead. Whether the pop was performed
-      // can't be determined from the return value of Navigator.maybePop().
-      final route = ModalRoute.of(context);
-      final pop = await route?.willPop();
+      // The willPop/pop pair here is equivalent to Navigator.maybePop(),
+      // which is what's called from the flutter back button handler.
+      final pop = await _topMostRoute.willPop();
       if (pop == RoutePopDisposition.pop) {
-        webviewReference.close();
+        // Close the webview if it's on the route at the top of the stack.
+        final isOnTopMostRoute = _topMostRoute == ModalRoute.of(context);
+        if (isOnTopMostRoute) {
+          webviewReference.close();
+        }
         Navigator.pop(context);
       }
     });
@@ -100,6 +102,16 @@ class _WebviewScaffoldState extends State<WebviewScaffold> {
     }
   }
 
+  /// Equivalent to [Navigator.of(context)._history.last].
+  Route<dynamic> get _topMostRoute {
+    var topMost;
+    Navigator.popUntil(context, (route) {
+      topMost = route;
+      return true;
+    });
+    return topMost;
+  }
+
   @override
   void dispose() {
     super.dispose();