Преглед на файлове

implement on url changed

Hadrien Lejard преди 8 години
родител
ревизия
f445329687

+ 31 - 7
android/src/main/java/com/flutter_webview_plugin/WebviewActivity.java

@@ -1,12 +1,17 @@
 package com.flutter_webview_plugin;
 
 import android.app.Activity;
+import android.graphics.Bitmap;
+import android.os.Build;
 import android.os.Bundle;
 import android.webkit.CookieManager;
 import android.webkit.ValueCallback;
 import android.webkit.WebView;
 import android.webkit.WebViewClient;
 
+import java.util.HashMap;
+import java.util.Map;
+
 /**
  * Created by lejard_h on 23/04/2017.
  */
@@ -41,12 +46,16 @@ public class WebviewActivity extends Activity {
 
     protected void clearCookies() {
         if (getIntent().getBooleanExtra(CLEAR_COOKIES_KEY, false)) {
-            CookieManager.getInstance().removeAllCookies(new ValueCallback<Boolean>() {
-                @Override
-                public void onReceiveValue(Boolean aBoolean) {
-
-                }
-            });
+            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
+                CookieManager.getInstance().removeAllCookies(new ValueCallback<Boolean>() {
+                    @Override
+                    public void onReceiveValue(Boolean aBoolean) {
+
+                    }
+                });
+            } else {
+                CookieManager.getInstance().removeAllCookie();
+            }
         }
     }
 
@@ -58,7 +67,7 @@ public class WebviewActivity extends Activity {
     }
 
     protected WebViewClient setWebViewClient() {
-        WebViewClient webViewClient = new WebViewClient();
+        WebViewClient webViewClient = new BrowserClient();
         webView.setWebViewClient(webViewClient);
         return webViewClient;
     }
@@ -83,4 +92,19 @@ public class WebviewActivity extends Activity {
         FlutterWebviewPlugin.channel.invokeMethod("onBackPressed", null);
         super.onBackPressed();
     }
+
+
+    private class BrowserClient extends WebViewClient {
+        private BrowserClient() {
+            super();
+        }
+
+        @Override
+        public void onPageStarted(WebView view, String url, Bitmap favicon) {
+            super.onPageStarted(view, url, favicon);
+            Map<String, Object> data = new HashMap<>();
+            data.put("url", url);
+            FlutterWebviewPlugin.channel.invokeMethod("onUrlChanged", data);
+        }
+    }
 }

+ 4 - 6
example/android/app/src/main/java/com/yourcompany/flutter_webview_plugin_example/MainActivity.java

@@ -1,16 +1,14 @@
 package com.yourcompany.flutter_webview_plugin_example;
 
 import android.os.Bundle;
+
 import io.flutter.app.FlutterActivity;
-import io.flutter.plugins.PluginRegistry;
+import io.flutter.plugins.GeneratedPluginRegistrant;
 
 public class MainActivity extends FlutterActivity {
-    PluginRegistry pluginRegistry;
-
     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
-        pluginRegistry = new PluginRegistry();
-        pluginRegistry.registerAll(this);
+        GeneratedPluginRegistrant.registerWith(this);
     }
-}
+}

+ 21 - 3
example/lib/main.dart

@@ -37,9 +37,15 @@ class _MyHomePageState extends State<MyHomePage> {
   // On destroy stream
   StreamSubscription _onDestroy;
 
-  TextEditingController _ctrl = new TextEditingController(text: "https://flutter.io");
+  // On urlChanged stream
+  StreamSubscription<String> _onUrlChanged;
+
+  TextEditingController _ctrl =
+  new TextEditingController(text: "https://flutter.io");
   GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey();
 
+  final _history = [];
+
   @override
   initState() {
     super.initState();
@@ -48,7 +54,17 @@ class _MyHomePageState extends State<MyHomePage> {
     _onDestroy = flutterWebviewPlugin.onDestroy.listen((_) {
       if (mounted) {
         // Actions like show a info toast.
-        _scaffoldKey.currentState.showSnackBar(new SnackBar(content: new Text("Webview Destroyed")));
+        _scaffoldKey.currentState
+            .showSnackBar(new SnackBar(content: new Text("Webview Destroyed")));
+      }
+    });
+
+    // Add a listener to on url changed
+    _onUrlChanged = flutterWebviewPlugin.onUrlChanged.listen((String url) {
+      if (mounted) {
+        setState(() {
+          _history.add(url);
+        });
       }
     });
   }
@@ -57,6 +73,7 @@ class _MyHomePageState extends State<MyHomePage> {
   void dispose() {
     // Every listener should be canceled, the same should be done with this stream.
     _onDestroy?.cancel();
+    _onUrlChanged?.cancel();
 
     super.dispose();
   }
@@ -78,7 +95,8 @@ class _MyHomePageState extends State<MyHomePage> {
           new RaisedButton(
             onPressed: _onPressed,
             child: new Text("Open Webview"),
-          )
+          ),
+          new Text(_history.join(", "))
         ],
       ),
     );

+ 12 - 2
lib/flutter_webview_plugin.dart

@@ -8,8 +8,11 @@ const _kChannel = 'flutter_webview_plugin';
 /// Have to be instanciate after `runApp` called.
 class FlutterWebviewPlugin {
   final MethodChannel _channel = const MethodChannel(_kChannel);
-  StreamController<Null> _onDestroy = new StreamController.broadcast();
-  StreamController<Null> _onBackPressed = new StreamController.broadcast();
+  final StreamController<Null> _onDestroy = new StreamController.broadcast();
+  final StreamController<Null> _onBackPressed =
+      new StreamController.broadcast();
+  final StreamController<String> _onUrlChanged =
+      new StreamController.broadcast();
 
   static FlutterWebviewPlugin _instance;
   FlutterWebviewPlugin._() {
@@ -30,6 +33,9 @@ class FlutterWebviewPlugin {
       case "onBackPressed":
         _onBackPressed.add(null);
         break;
+      case "onUrlChanged":
+        _onUrlChanged.add(call.arguments["url"]);
+        break;
     }
   }
 
@@ -63,4 +69,8 @@ class FlutterWebviewPlugin {
   /// Close the Webview
   /// Will trigger the [onDestroy] event
   Future<Null> close() => _channel.invokeMethod("close");
+
+  /// Listening url changed
+  ///
+  Stream<String> get onUrlChanged => _onUrlChanged.stream;
 }

+ 1 - 1
pubspec.yaml

@@ -4,7 +4,7 @@ authors:
 - Hadrien Lejard <hadrien.lejard@gmail.com>
 - Toufik Zitouni <toufiksapps@gmail.com>
 homepage: https://github.com/dart-flitter/flutter_webview_plugin
-version: 0.0.5
+version: 0.0.7
 
 flutter:
   plugin: