Browse Source

onBackPressed listener

Hadrien Lejard 8 years ago
parent
commit
d5ce53c330

+ 6 - 0
android/src/main/java/com/flutter_webview_plugin/WebviewActivity.java

@@ -73,4 +73,10 @@ public class WebviewActivity extends Activity {
         super.onDestroy();
         FlutterWebviewPlugin.channel.invokeMethod("onDestroy", null);
     }
+
+    @Override
+    protected void onBackPressed() {
+        super.onBackPressed();
+        FlutterWebviewPlugin.channel.invokeMethod("onBackPressed", null);
+    }
 }

+ 7 - 4
example/lib/main.dart

@@ -49,7 +49,8 @@ class MyHomePage extends StatefulWidget {
 }
 
 class _MyHomePageState extends State<MyHomePage> {
-  TextEditingController _ctrl = new TextEditingController(text: "https://flutter.io");
+  TextEditingController _ctrl =
+      new TextEditingController(text: "https://flutter.io");
   StreamSubscription _onDestroy;
   GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey();
 
@@ -59,12 +60,12 @@ class _MyHomePageState extends State<MyHomePage> {
     FlutterWebviewPlugin.init();
     _onDestroy = FlutterWebviewPlugin.onDestroy.listen((_) {
       if (mounted) {
-        _scaffoldKey.currentState.showSnackBar(new SnackBar(content: new Text("Webview Destroyed")));
+        _scaffoldKey.currentState
+            .showSnackBar(new SnackBar(content: new Text("Webview Destroyed")));
       }
     });
   }
 
-
   @override
   void dispose() {
     super.dispose();
@@ -79,7 +80,9 @@ class _MyHomePageState extends State<MyHomePage> {
         title: new Text('Plugin example app'),
       ),
       body: new Column(children: [
-        new Container(padding: const EdgeInsets.all(24.0), child: new TextField(controller: _ctrl)),
+        new Container(
+            padding: const EdgeInsets.all(24.0),
+            child: new TextField(controller: _ctrl)),
         new RaisedButton(onPressed: _onPressed, child: new Text("Open Webview"))
       ], mainAxisAlignment: MainAxisAlignment.center),
     );

+ 17 - 6
lib/flutter_webview_plugin.dart

@@ -4,15 +4,21 @@ import 'package:flutter/material.dart';
 import 'package:flutter/services.dart';
 
 class FlutterWebviewPlugin {
-
   static bool _init = false;
   static StreamController<Null> _onDestroy = new StreamController.broadcast();
   static Stream<Null> get onDestroy => _onDestroy.stream;
 
+  static StreamController<Null> _onBackPressed =
+      new StreamController.broadcast();
+  static Stream<Null> get onBackPressed => _onDestroy.stream;
+
   static const MethodChannel _channel =
-  const MethodChannel('flutter_webview_plugin');
+      const MethodChannel('flutter_webview_plugin');
 
-  static Future<Null> launch(String url, {bool withJavascript: true, bool clearCache: false, bool clearCookies: false}) =>
+  static Future<Null> launch(String url,
+          {bool withJavascript: true,
+          bool clearCache: false,
+          bool clearCookies: false}) =>
       _channel.invokeMethod('launch', {
         "url": url,
         "withJavascript": withJavascript,
@@ -30,8 +36,13 @@ class FlutterWebviewPlugin {
   }
 
   static Future<Null> _handleMessages(MethodCall call) async {
-    if (call.method == "onDestroy") {
-      _onDestroy.add(null);
+    switch (call.method) {
+      case "onDestroy":
+        _onDestroy.add(null);
+        break;
+      case "onBackPressed":
+        _onBackPressed.add(null);
+        break;
     }
   }
-}
+}