Browse Source

Merge pull request #59 from howardt12345/master

Added reloading (Android)
Hadrien Lejard 7 năm trước cách đây
mục cha
commit
c93ea8fcb5

+ 34 - 0
android/src/main/java/com/flutter_webview_plugin/FlutterWebviewPlugin.java

@@ -48,6 +48,15 @@ public class FlutterWebviewPlugin implements MethodCallHandler {
             case "resize":
                 resize(call, result);
                 break;
+            case "reload":
+                reload(call, result);
+                break;
+            case "back":
+                back(call, result);
+                break;
+            case "forward":
+                forward(call, result);
+                break;
             default:
                 result.notImplemented();
                 break;
@@ -111,6 +120,31 @@ public class FlutterWebviewPlugin implements MethodCallHandler {
         }
     }
 
+    /** 
+    * Navigates back on the Webview.
+    */
+    private void back(MethodCall call, MethodChannel.Result result) {
+        if (webViewManager != null) {
+            webViewManager.back(call, result);
+        }
+    }
+    /** 
+    * Navigates forward on the Webview.
+    */
+    private void forward(MethodCall call, MethodChannel.Result result) {
+        if (webViewManager != null) {
+            webViewManager.forward(call, result);
+        }
+    }
+
+    /** 
+    * Reloads the Webview.
+    */
+    private void reload(MethodCall call, MethodChannel.Result result) {
+        if (webViewManager != null) {
+            webViewManager.reload(call, result);
+        }
+    }
     private void eval(MethodCall call, final MethodChannel.Result result) {
         if (webViewManager != null) {
             webViewManager.eval(call, result);

+ 36 - 0
android/src/main/java/com/flutter_webview_plugin/WebviewManager.java

@@ -122,8 +122,44 @@ class WebviewManager {
             }
         });
     }
+    /** 
+    * Reloads the Webview.
+    */
+    void reload(MethodCall call, MethodChannel.Result result) {
+        if (webView != null) {
+            webView.reload();
+        }
+    }
+    /** 
+    * Navigates back on the Webview.
+    */
+    void back(MethodCall call, MethodChannel.Result result) {
+        if (webView != null && webView.canGoBack()) {
+            webView.goBack();
+        }
+    }
+    /** 
+    * Navigates forward on the Webview.
+    */
+    void forward(MethodCall call, MethodChannel.Result result) {
+        if (webView != null && webView.canGoForward()) {
+            webView.goForward();
+        }
+    }
 
     void resize(FrameLayout.LayoutParams params) {
         webView.setLayoutParams(params);
     }
+    /** 
+    * Checks if going back on the Webview is possible.
+    */
+    boolean canGoBack() {
+        return webView.canGoBack();
+    }
+    /** 
+    * Checks if going forward on the Webview is possible.
+    */
+    boolean canGoForward() {
+        return webView.canGoForward();
+    }
 }

+ 12 - 0
lib/src/base.dart

@@ -117,6 +117,18 @@ class FlutterWebviewPlugin {
   /// Will trigger the [onDestroy] event
   Future close() => _channel.invokeMethod("close");
 
+  /// Reloads the WebView.
+  /// This is only available on Android for now.
+  Future reload() => _channel.invokeMethod("reload");
+  
+  /// Navigates back on the Webview.
+  /// This is only available on Android for now.
+  Future goBack() => _channel.invokeMethod("back");
+  
+  /// Navigates forward on the Webview.
+  /// This is only available on Android for now.
+  Future goForward() => _channel.invokeMethod("forward");
+
   /// Close all Streams
   void dispose() {
     _onDestroy.close();