diff --git a/README.md b/README.md index 54f69cb7..ffdab360 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 allows 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 ... @@ -21,11 +20,11 @@ For help getting started with Flutter, view our online [documentation](http://fl new MaterialApp( routes: { "/": (_) => new WebviewScaffold( - url: "https://www.google.com", - appBar: new AppBar( - title: new Text("Widget webview"), - ), - ) + url: "https://www.google.com", + appBar: new AppBar( + title: new Text("Widget webview"), + ), + ), }, ); ``` @@ -44,20 +43,20 @@ return new MaterialApp( routes: { '/': (_) => const MyHomePage(title: 'Flutter WebView Demo'), '/widget': (_) => new WebviewScaffold( - url: selectedUrl, - appBar: new AppBar( - title: const Text('Widget webview'), - ), - withZoom: true, - withLocalStorage: true, - hidden: true, - initialChild: Container( - color: Colors.redAccent, - child: const Center( - child: Text('Waiting.....'), - ), - ), - ) + url: selectedUrl, + appBar: new AppBar( + title: const Text('Widget webview'), + ), + withZoom: true, + withLocalStorage: true, + hidden: true, + initialChild: Container( + color: Colors.redAccent, + child: const Center( + child: Text('Waiting.....'), + ), + ), + ), }, ); ``` @@ -66,11 +65,12 @@ return 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) { - + }); ``` @@ -111,12 +111,14 @@ flutterWebviewPlugin.close(); final flutterWebviewPlugin = new FlutterWebviewPlugin(); flutterWebviewPlugin.launch(url, - fullScreen: false, - rect: new Rect.fromLTWH( - 0.0, - 0.0, - MediaQuery.of(context).size.width, - 300.0)); + fullScreen: false, + rect: new Rect.fromLTWH( + 0.0, + 0.0, + MediaQuery.of(context).size.width, + 300.0, + ), +); ``` ### Webview Events @@ -126,41 +128,51 @@ flutterWebviewPlugin.launch(url, - `Stream` onStateChanged - `Stream` onError -***Don't forget to dispose webview*** +**_Don't forget to dispose webview_** `flutterWebviewPlugin.dispose()` ### Webview Functions ```dart -Future launch(String url, - {Map headers: null, - bool withJavascript: true, - bool clearCache: false, - bool clearCookies: false, - bool hidden: false, - bool enableAppScheme: true, - Rect rect: null, - String userAgent: null, - bool withZoom: false, - bool withLocalStorage: true, - bool withLocalUrl: true, - bool scrollBar: true}); +Future launch(String url, { + Map headers: null, + bool withJavascript: true, + bool clearCache: false, + bool clearCookies: false, + bool hidden: false, + bool enableAppScheme: true, + Rect rect: null, + String userAgent: null, + bool withZoom: false, + bool withLocalStorage: true, + bool withLocalUrl: true, + bool scrollBar: true, + bool supportMultipleWindows: false, + bool appCacheEnabled: false, + bool allowFileURLs: false, +}); ``` + ```dart Future evalJavascript(String code); ``` + ```dart Future> getCookies(); ``` + ```dart Future resize(Rect rect); ``` + ```dart Future show(); ``` + ```dart Future hide(); ``` + ```dart Future reloadUrl(String url); ``` 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 0276aa83..aef02fb7 100644 --- a/android/src/main/java/com/flutter_webview_plugin/FlutterWebviewPlugin.java +++ b/android/src/main/java/com/flutter_webview_plugin/FlutterWebviewPlugin.java @@ -96,6 +96,7 @@ private void openUrl(MethodCall call, MethodChannel.Result result) { boolean appCacheEnabled = call.argument("appCacheEnabled"); Map headers = call.argument("headers"); boolean scrollBar = call.argument("scrollBar"); + boolean allowFileURLs = call.argument("allowFileURLs"); if (webViewManager == null || webViewManager.closed == true) { webViewManager = new WebviewManager(activity); @@ -116,7 +117,8 @@ private void openUrl(MethodCall call, MethodChannel.Result result) { withLocalStorage, scrollBar, supportMultipleWindows, - appCacheEnabled + appCacheEnabled, + allowFileURLs ); result.success(null); } 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 3bc6fae3..313b9e68 100644 --- a/android/src/main/java/com/flutter_webview_plugin/WebviewManager.java +++ b/android/src/main/java/com/flutter_webview_plugin/WebviewManager.java @@ -194,17 +194,35 @@ private void clearCache() { webView.clearFormData(); } - void openUrl(boolean withJavascript, boolean clearCache, boolean hidden, boolean clearCookies, String userAgent, String url, Map headers, boolean withZoom, boolean withLocalStorage, boolean scrollBar, boolean supportMultipleWindows, boolean appCacheEnabled) { + void openUrl( + boolean withJavascript, + boolean clearCache, + boolean hidden, + boolean clearCookies, + String userAgent, + String url, + Map headers, + boolean withZoom, + boolean withLocalStorage, + boolean scrollBar, + boolean supportMultipleWindows, + boolean appCacheEnabled, + boolean allowFileURLs, + ) { webView.getSettings().setJavaScriptEnabled(withJavascript); webView.getSettings().setBuiltInZoomControls(withZoom); webView.getSettings().setSupportZoom(withZoom); webView.getSettings().setDomStorageEnabled(withLocalStorage); webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(supportMultipleWindows); + webView.getSettings().setSupportMultipleWindows(supportMultipleWindows); + webView.getSettings().setAppCacheEnabled(appCacheEnabled); + + webView.getSettings().setAllowFileAccessFromFileURLs(allowFileURLs); + webView.getSettings().setAllowUniversalAccessFromFileURLs(allowFileURLs); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { - Log.d("WebviewManager", "Mixed Content enabled"); webView.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_COMPATIBILITY_MODE); } diff --git a/lib/src/base.dart b/lib/src/base.dart index 639233c4..dba3c18d 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'; @@ -109,7 +110,9 @@ class FlutterWebviewPlugin { bool withLocalUrl, bool scrollBar, bool supportMultipleWindows, - bool appCacheEnabled}) async { + bool appCacheEnabled, + bool allowFileURLs, + }) async { final args = { 'url': url, 'withJavascript': withJavascript ?? true, @@ -123,7 +126,8 @@ class FlutterWebviewPlugin { 'withLocalUrl': withLocalUrl ?? false, 'scrollBar': scrollBar ?? true, 'supportMultipleWindows': supportMultipleWindows ?? false, - 'appCacheEnabled': appCacheEnabled ?? false + 'appCacheEnabled': appCacheEnabled ?? false, + "allowFileURLs": allowFileURLs ?? false, }; if (headers != null) { diff --git a/lib/src/webview_scaffold.dart b/lib/src/webview_scaffold.dart index 39002d71..683298b8 100644 --- a/lib/src/webview_scaffold.dart +++ b/lib/src/webview_scaffold.dart @@ -22,6 +22,7 @@ class WebviewScaffold extends StatefulWidget { final bool withZoom; final bool withLocalStorage; final bool withLocalUrl; + final bool allowFileURLs; final bool scrollBar; final bool hidden; final Widget initialChild; @@ -48,7 +49,9 @@ class WebviewScaffold extends StatefulWidget { this.withLocalUrl, this.scrollBar, this.hidden = false, - this.initialChild}) + this.initialChild, + this.allowFileURLs, + }) : super(key: key); @override @@ -111,6 +114,7 @@ class _WebviewScaffoldState extends State { scrollBar: widget.scrollBar, supportMultipleWindows: widget.supportMultipleWindows, appCacheEnabled: widget.appCacheEnabled, + allowFileURLs: widget.allowFileURLs, ); } else { if (_rect != value) {