From bcd2b5d710dbea2b0c599f52f3d7d39be762c9a0 Mon Sep 17 00:00:00 2001 From: dev Date: Wed, 11 Jul 2018 22:24:18 +0530 Subject: [PATCH 1/6] added allowFileURLs for Android --- .../FlutterWebviewPlugin.java | 37 +++++++++++-------- .../WebviewManager.java | 13 ++++++- lib/src/base.dart | 9 ++++- lib/src/webview_scaffold.dart | 7 +++- 4 files changed, 45 insertions(+), 21 deletions(-) diff --git a/android/src/main/java/com/flutter_webview_plugin/FlutterWebviewPlugin.java b/android/src/main/java/com/flutter_webview_plugin/FlutterWebviewPlugin.java index a621175e..c2678083 100644 --- a/android/src/main/java/com/flutter_webview_plugin/FlutterWebviewPlugin.java +++ b/android/src/main/java/com/flutter_webview_plugin/FlutterWebviewPlugin.java @@ -81,6 +81,7 @@ private void openUrl(MethodCall call, MethodChannel.Result result) { boolean clearCookies = call.argument("clearCookies"); boolean withZoom = call.argument("withZoom"); boolean withLocalStorage = call.argument("withLocalStorage"); + boolean allowFileURLs = call.argument("allowFileURLs"); if (webViewManager == null || webViewManager.closed == true) { webViewManager = new WebviewManager(activity); @@ -90,14 +91,16 @@ private void openUrl(MethodCall call, MethodChannel.Result result) { activity.addContentView(webViewManager.webView, params); - webViewManager.openUrl(withJavascript, - clearCache, - hidden, - clearCookies, - userAgent, - url, - withZoom, - withLocalStorage + webViewManager.openUrl( + withJavascript, + clearCache, + hidden, + clearCookies, + userAgent, + url, + withZoom, + withLocalStorage, + allowFileURLs ); result.success(null); } @@ -157,14 +160,16 @@ private void reload(MethodCall call, MethodChannel.Result result) { private void reloadUrl(MethodCall call, MethodChannel.Result result) { if (webViewManager != null) { String url = call.argument("url"); - webViewManager.openUrl(false, - false, - false, - false, - "", - url, - false, - false + webViewManager.openUrl( + false, + false, + false, + false, + "", + url, + false, + false, + false ); } } diff --git a/android/src/main/java/com/flutter_webview_plugin/WebviewManager.java b/android/src/main/java/com/flutter_webview_plugin/WebviewManager.java index b374b112..7f805805 100644 --- a/android/src/main/java/com/flutter_webview_plugin/WebviewManager.java +++ b/android/src/main/java/com/flutter_webview_plugin/WebviewManager.java @@ -68,11 +68,22 @@ private void clearCache() { webView.clearFormData(); } - void openUrl(boolean withJavascript, boolean clearCache, boolean hidden, boolean clearCookies, String userAgent, String url, boolean withZoom, boolean withLocalStorage) { + void openUrl( + boolean withJavascript, + boolean clearCache, + boolean hidden, + boolean clearCookies, + String userAgent, + String url, + boolean withZoom, + boolean withLocalStorage, + boolean allowFileURLs) { webView.getSettings().setJavaScriptEnabled(withJavascript); webView.getSettings().setBuiltInZoomControls(withZoom); webView.getSettings().setSupportZoom(withZoom); webView.getSettings().setDomStorageEnabled(withLocalStorage); + webView.getSettings().setAllowFileAccessFromFileURLs(allowFileURLs); + webView.getSettings().setAllowUniversalAccessFromFileURLs(allowFileURLs); if (clearCache) { clearCache(); diff --git a/lib/src/base.dart b/lib/src/base.dart index ac3ca79c..e1139534 100644 --- a/lib/src/base.dart +++ b/lib/src/base.dart @@ -1,5 +1,6 @@ import 'dart:async'; import 'dart:ui'; +import 'dart:io'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; @@ -86,7 +87,8 @@ class FlutterWebviewPlugin { String userAgent, bool withZoom, bool withLocalStorage, - bool withLocalUrl}) async { + bool withLocalUrl, + bool allowFileURLs}) async { Map args = { "url": url, "withJavascript": withJavascript ?? true, @@ -97,7 +99,7 @@ class FlutterWebviewPlugin { "userAgent": userAgent, "withZoom": withZoom ?? false, "withLocalStorage": withLocalStorage ?? true, - "withLocalUrl": withLocalUrl ?? false + "withLocalUrl": withLocalUrl ?? false, }; if (rect != null) { args["rect"] = { @@ -107,6 +109,9 @@ class FlutterWebviewPlugin { "height": rect.height }; } + if (Platform.isAndroid) { + args["allowFileURLs"] = allowFileURLs ?? false; + } await _channel.invokeMethod('launch', args); } diff --git a/lib/src/webview_scaffold.dart b/lib/src/webview_scaffold.dart index 91c4a602..b871f5a1 100644 --- a/lib/src/webview_scaffold.dart +++ b/lib/src/webview_scaffold.dart @@ -19,6 +19,7 @@ class WebviewScaffold extends StatefulWidget { final bool withZoom; final bool withLocalStorage; final bool withLocalUrl; + final bool allowFileURLs; WebviewScaffold( {Key key, @@ -34,7 +35,8 @@ class WebviewScaffold extends StatefulWidget { this.bottomNavigationBar, this.withZoom, this.withLocalStorage, - this.withLocalUrl}) + this.withLocalUrl, + this.allowFileURLs}) : super(key: key); @override @@ -70,7 +72,8 @@ class _WebviewScaffoldState extends State { rect: _rect, withZoom: widget.withZoom, withLocalStorage: widget.withLocalStorage, - withLocalUrl: widget.withLocalUrl); + withLocalUrl: widget.withLocalUrl, + allowFileURLs: widget.allowFileURLs); } else { Rect rect = _buildRect(context); if (_rect != rect) { From 09055395f2dccc37459071abd8a50e4132f0a113 Mon Sep 17 00:00:00 2001 From: dev Date: Wed, 11 Jul 2018 22:28:26 +0530 Subject: [PATCH 2/6] updated README --- README.md | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 666bdcc2..fd0a8052 100644 --- a/README.md +++ b/README.md @@ -1,11 +1,10 @@ -[![pub package](https://img.shields.io/pub/v/flutter_webview_plugin.svg)](https://pub.dartlang.org/packages/flutter_webview_plugin) - +[![pub package](https://img.shields.io/pub/v/flutter_webview_plugin.svg)](https://pub.dartlang.org/packages/flutter_webview_plugin) # flutter_webview_plugin Plugin that allow Flutter to communicate with a native WebView. -***Warning:*** +**_Warning:_** The webview is not integrated in the widget tree, it is a native view on top of the flutter view. you won't be able to use snackbars, dialogs ... @@ -34,11 +33,12 @@ new MaterialApp( so you can take control of the webview from anywhere in the app listen for events + ```dart final flutterWebviewPlugin = new FlutterWebviewPlugin(); flutterWebviewPlugin.onUrlChanged.listen((String url) { - + }); ``` @@ -64,9 +64,9 @@ final flutterWebviewPlugin = new FlutterWebviewPlugin(); flutterWebviewPlugin.launch(url, fullScreen: false, rect: new Rect.fromLTWH( - 0.0, - 0.0, - MediaQuery.of(context).size.width, + 0.0, + 0.0, + MediaQuery.of(context).size.width, 300.0)); ``` @@ -77,7 +77,7 @@ flutterWebviewPlugin.launch(url, - `Stream` onStateChanged - `Stream` onError -***Don't forget to dispose webview*** +**_Don't forget to dispose webview_** `flutterWebviewPlugin.dispose()` ### Webview Functions @@ -92,23 +92,30 @@ Future launch(String url, Rect rect: null, String userAgent: null, bool withZoom: false, - bool withLocalStorage: true}); + bool withLocalStorage: true, + bool allowFileURLs: flase # Allows using "file:///" urls in Android}); ``` + ```dart Future evalJavascript(String code); ``` + ```dart Future> getCookies(); ``` + ```dart Future resize(Rect rect); ``` + ```dart Future show(); ``` + ```dart Future hide(); ``` + ```dart Future reloadUrl(String url); ``` From 2735c15f7da4f85285cebfb2772c69188fd4f358 Mon Sep 17 00:00:00 2001 From: Dev Aggarwal Date: Wed, 11 Jul 2018 22:29:43 +0530 Subject: [PATCH 3/6] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index fd0a8052..c1bf881b 100644 --- a/README.md +++ b/README.md @@ -93,7 +93,7 @@ Future launch(String url, String userAgent: null, bool withZoom: false, bool withLocalStorage: true, - bool allowFileURLs: flase # Allows using "file:///" urls in Android}); + bool allowFileURLs: flase}); // Allows using "file:///" urls in Android ``` ```dart From 1f328757291258ffedf995a2c627c420160ca7f5 Mon Sep 17 00:00:00 2001 From: Dev Aggarwal Date: Wed, 11 Jul 2018 22:30:08 +0530 Subject: [PATCH 4/6] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index c1bf881b..878954b5 100644 --- a/README.md +++ b/README.md @@ -93,7 +93,7 @@ Future launch(String url, String userAgent: null, bool withZoom: false, bool withLocalStorage: true, - bool allowFileURLs: flase}); // Allows using "file:///" urls in Android + bool allowFileURLs: false}); // Allows using "file:///" urls in Android ``` ```dart From a55a172b8ec317068059ad1b2e7844812ffaaf77 Mon Sep 17 00:00:00 2001 From: dev Date: Wed, 11 Jul 2018 22:34:06 +0530 Subject: [PATCH 5/6] fix bug --- lib/src/base.dart | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lib/src/base.dart b/lib/src/base.dart index e1139534..d7bad07c 100644 --- a/lib/src/base.dart +++ b/lib/src/base.dart @@ -112,6 +112,9 @@ class FlutterWebviewPlugin { if (Platform.isAndroid) { args["allowFileURLs"] = allowFileURLs ?? false; } + else { + args["allowFileURLs"] = false; + } await _channel.invokeMethod('launch', args); } From e57e2d97ff4cc01313a58081e34dd3a727449da2 Mon Sep 17 00:00:00 2001 From: dev Date: Sun, 29 Jul 2018 11:19:34 +0530 Subject: [PATCH 6/6] fix conflicts --- .../FlutterWebviewPlugin.java | 3 ++- lib/src/base.dart | 23 ++++++++----------- lib/src/webview_scaffold.dart | 13 ++++++----- 3 files changed, 18 insertions(+), 21 deletions(-) diff --git a/android/src/main/java/com/flutter_webview_plugin/FlutterWebviewPlugin.java b/android/src/main/java/com/flutter_webview_plugin/FlutterWebviewPlugin.java index e37c39aa..0a3213bb 100644 --- a/android/src/main/java/com/flutter_webview_plugin/FlutterWebviewPlugin.java +++ b/android/src/main/java/com/flutter_webview_plugin/FlutterWebviewPlugin.java @@ -83,8 +83,8 @@ private void openUrl(MethodCall call, MethodChannel.Result result) { boolean clearCookies = call.argument("clearCookies"); boolean withZoom = call.argument("withZoom"); boolean withLocalStorage = call.argument("withLocalStorage"); - boolean allowFileURLs = call.argument("allowFileURLs"); boolean scrollBar = call.argument("scrollBar"); + boolean allowFileURLs = call.argument("allowFileURLs"); if (webViewManager == null || webViewManager.closed == true) { webViewManager = new WebviewManager(activity); @@ -172,6 +172,7 @@ private void reloadUrl(MethodCall call, MethodChannel.Result result) { url, false, false, + false, false ); } diff --git a/lib/src/base.dart b/lib/src/base.dart index 359e1f78..1041412b 100644 --- a/lib/src/base.dart +++ b/lib/src/base.dart @@ -102,7 +102,8 @@ class FlutterWebviewPlugin { "withZoom": withZoom ?? false, "withLocalStorage": withLocalStorage ?? true, "withLocalUrl": withLocalUrl ?? false, - "scrollBar": scrollBar ?? true + "scrollBar": scrollBar ?? true, + "allowFileURLs": allowFileURLs ?? false }; if (rect != null) { args["rect"] = { @@ -112,12 +113,6 @@ class FlutterWebviewPlugin { "height": rect.height }; } - if (Platform.isAndroid) { - args["allowFileURLs"] = allowFileURLs ?? false; - } - else { - args["allowFileURLs"] = false; - } await _channel.invokeMethod('launch', args); } @@ -142,28 +137,28 @@ 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"); // Reload webview with a new url Future reloadUrl(String url) async { - Map args = { - "url": url - }; + Map args = {"url": url}; await _channel.invokeMethod("reloadUrl", args); } /// adds the plugin as ActivityResultListener /// Only needed and used on Android - Future registerAcitivityResultListener() => _channel.invokeMethod("registerAcitivityResultListener"); + Future registerAcitivityResultListener() => + _channel.invokeMethod("registerAcitivityResultListener"); /// removes the plugin as ActivityResultListener /// Only needed and used on Android - Future removeAcitivityResultListener() => _channel.invokeMethod("removeAcitivityResultListener"); + Future removeAcitivityResultListener() => + _channel.invokeMethod("removeAcitivityResultListener"); /// Close all Streams void dispose() { diff --git a/lib/src/webview_scaffold.dart b/lib/src/webview_scaffold.dart index 67795d6f..ae468201 100644 --- a/lib/src/webview_scaffold.dart +++ b/lib/src/webview_scaffold.dart @@ -37,8 +37,8 @@ class WebviewScaffold extends StatefulWidget { this.withZoom, this.withLocalStorage, this.withLocalUrl, - this.scrollBar - this.allowFileURLs}) + this.scrollBar, + this.allowFileURLs}) : super(key: key); @override @@ -106,15 +106,16 @@ class _WebviewScaffoldState extends State { num height = mediaQuery.size.height - top; if (widget.bottomNavigationBar != null) { - height -= - 56.0 + mediaQuery.padding.bottom; // todo(lejard_h) find a way to determine bottomNavigationBar programmatically + height -= 56.0 + + mediaQuery.padding + .bottom; // todo(lejard_h) find a way to determine bottomNavigationBar programmatically } if (widget.persistentFooterButtons != null) { height -= 53.0; // todo(lejard_h) find a way to determine persistentFooterButtons programmatically - if (widget.bottomNavigationBar == null){ - height -= mediaQuery.padding.bottom; + if (widget.bottomNavigationBar == null) { + height -= mediaQuery.padding.bottom; } }