Browse Source

Add hide/show on webview for Android & iOS

Gerry High 7 năm trước cách đây
mục cha
commit
29f0249eca

+ 6 - 0
README.md

@@ -103,3 +103,9 @@ Future<Map<String, dynamic>> getCookies();
 ```dart
 Future<Null> resize(Rect rect);
 ```
+```dart
+Future<Null> show();
+```
+```dart
+Future<Null> hide();
+```

+ 3 - 2
android/build.gradle

@@ -3,11 +3,12 @@ version '1.0-SNAPSHOT'
 
 buildscript {
     repositories {
+        google()
         jcenter()
     }
 
     dependencies {
-        classpath 'com.android.tools.build:gradle:2.3.0'
+        classpath 'com.android.tools.build:gradle:3.1.0'
     }
 }
 
@@ -21,7 +22,7 @@ apply plugin: 'com.android.library'
 
 android {
     compileSdkVersion 25
-    buildToolsVersion '25.0.0'
+    buildToolsVersion '27.0.3'
 
     defaultConfig {
         testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

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

@@ -57,6 +57,12 @@ public class FlutterWebviewPlugin implements MethodCallHandler {
             case "forward":
                 forward(call, result);
                 break;
+            case "hide":
+                hide(call, result);
+                break;
+            case "show":
+                show(call, result);
+                break;
             default:
                 result.notImplemented();
                 break;
@@ -158,6 +164,16 @@ public class FlutterWebviewPlugin implements MethodCallHandler {
         }
         result.success(null);
     }
+    private void hide(MethodCall call, final MethodChannel.Result result) {
+        if (webViewManager != null) {
+            webViewManager.hide(call, result);
+        }
+    }
+    private void show(MethodCall call, final MethodChannel.Result result) {
+        if (webViewManager != null) {
+            webViewManager.show(call, result);
+        }
+    }
 
     private int dp2px(Context context, float dp) {
         final float scale = context.getResources().getDisplayMetrics().density;

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

@@ -162,4 +162,14 @@ class WebviewManager {
     boolean canGoForward() {
         return webView.canGoForward();
     }
+	void hide(MethodCall call, MethodChannel.Result result) {
+        if (webView != null) {
+            webView.setVisibility(View.INVISIBLE);
+        }
+    }
+    void show(MethodCall call, MethodChannel.Result result) {
+        if (webView != null) {
+            webView.setVisibility(View.VISIBLE);
+        }
+    }
 }

+ 2 - 2
example/android/app/build.gradle

@@ -15,8 +15,8 @@ apply plugin: 'com.android.application'
 apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"
 
 android {
-    compileSdkVersion 25
-    buildToolsVersion '25.0.2'
+    compileSdkVersion 27
+    buildToolsVersion '27.0.3'
 
     lintOptions {
         disable 'InvalidPackage'

+ 9 - 1
example/android/build.gradle

@@ -1,16 +1,24 @@
 buildscript {
     repositories {
+        google()
         jcenter()
+        maven {
+            url 'https://maven.google.com/'
+        }
     }
 
     dependencies {
-        classpath 'com.android.tools.build:gradle:2.2.3'
+        classpath 'com.android.tools.build:gradle:3.1.1'
     }
 }
 
 allprojects {
     repositories {
+        google()
         jcenter()
+        maven {
+            url 'https://maven.google.com/'
+        }
     }
 }
 

+ 18 - 0
ios/Classes/FlutterWebviewPlugin.m

@@ -46,6 +46,12 @@ static NSString *const CHANNEL_NAME = @"flutter_webview_plugin";
     } else if ([@"resize" isEqualToString:call.method]) {
         [self resize:call];
         result(nil);
+    } else if ([@"show" isEqualToString:call.method]) {
+        [self show];
+        result(nil);
+    } else if ([@"hide" isEqualToString:call.method]) {
+        [self hide];
+        result(nil);
     } else {
         result(FlutterMethodNotImplemented);
     }
@@ -150,6 +156,18 @@ static NSString *const CHANNEL_NAME = @"flutter_webview_plugin";
     }
 }
 
+- (void)show {
+    if (self.webview 1= nil) {
+        self.webview.hidden = false;
+    }
+}
+
+- (void)hide {
+    if (self.webview 1= nil) {
+        self.webview.hidden = true;
+    }
+}
+
 #pragma mark -- WkWebView Delegate
 - (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction
     decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler {

+ 6 - 0
lib/src/base.dart

@@ -131,6 +131,12 @@ class FlutterWebviewPlugin {
   /// Navigates forward on the Webview.
   /// This is only available on Android for now.
   Future goForward() => _channel.invokeMethod("forward");
+  
+  // Hides the webview
+  Future hide() => _channel.invokeMethod("hide");
+  
+  // Shows the webview
+  Future show() => _channel.invokeMethod("show");
 
   /// Close all Streams
   void dispose() {