Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 55 additions & 43 deletions README.md
Original file line number Diff line number Diff line change
@@ -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 ...

Expand All @@ -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"),
),
),
},
);
```
Expand All @@ -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.....'),
),
),
),
},
);
```
Expand All @@ -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) {

});
```

Expand Down Expand Up @@ -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
Expand All @@ -126,41 +128,51 @@ flutterWebviewPlugin.launch(url,
- `Stream<WebViewStateChanged>` onStateChanged
- `Stream<String>` onError

***Don't forget to dispose webview***
**_Don't forget to dispose webview_**
`flutterWebviewPlugin.dispose()`

### Webview Functions

```dart
Future<Null> launch(String url,
{Map<String, String> 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<Null> launch(String url, {
Map<String, String> 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<String> evalJavascript(String code);
```

```dart
Future<Map<String, dynamic>> getCookies();
```

```dart
Future<Null> resize(Rect rect);
```

```dart
Future<Null> show();
```

```dart
Future<Null> hide();
```

```dart
Future<Null> reloadUrl(String url);
```
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ private void openUrl(MethodCall call, MethodChannel.Result result) {
boolean appCacheEnabled = call.argument("appCacheEnabled");
Map<String, String> headers = call.argument("headers");
boolean scrollBar = call.argument("scrollBar");
boolean allowFileURLs = call.argument("allowFileURLs");

if (webViewManager == null || webViewManager.closed == true) {
webViewManager = new WebviewManager(activity);
Expand All @@ -116,7 +117,8 @@ private void openUrl(MethodCall call, MethodChannel.Result result) {
withLocalStorage,
scrollBar,
supportMultipleWindows,
appCacheEnabled
appCacheEnabled,
allowFileURLs
);
result.success(null);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,17 +194,35 @@ private void clearCache() {
webView.clearFormData();
}

void openUrl(boolean withJavascript, boolean clearCache, boolean hidden, boolean clearCookies, String userAgent, String url, Map<String, String> 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<String, String> 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);
}

Expand Down
8 changes: 6 additions & 2 deletions lib/src/base.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'dart:async';
import 'dart:ui';
import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
Expand Down Expand Up @@ -109,7 +110,9 @@ class FlutterWebviewPlugin {
bool withLocalUrl,
bool scrollBar,
bool supportMultipleWindows,
bool appCacheEnabled}) async {
bool appCacheEnabled,
bool allowFileURLs,
}) async {
final args = <String, dynamic>{
'url': url,
'withJavascript': withJavascript ?? true,
Expand All @@ -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) {
Expand Down
6 changes: 5 additions & 1 deletion lib/src/webview_scaffold.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -111,6 +114,7 @@ class _WebviewScaffoldState extends State<WebviewScaffold> {
scrollBar: widget.scrollBar,
supportMultipleWindows: widget.supportMultipleWindows,
appCacheEnabled: widget.appCacheEnabled,
allowFileURLs: widget.allowFileURLs,
);
} else {
if (_rect != value) {
Expand Down