Skip to content

Commit bf64045

Browse files
devxpyslightfoot
authored andcommitted
Allow using "file:///" urls in Android. (#122)
* added allowFileURLs for Android
1 parent a9ed99a commit bf64045

File tree

5 files changed

+89
-49
lines changed

5 files changed

+89
-49
lines changed

README.md

Lines changed: 55 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
[![pub package](https://img.shields.io/pub/v/flutter_webview_plugin.svg)](https://pub.dartlang.org/packages/flutter_webview_plugin)
2-
1+
[![pub package](https://img.shields.io/pub/v/flutter_webview_plugin.svg)](https://pub.dartlang.org/packages/flutter_webview_plugin)
32

43
# flutter_webview_plugin
54

65
Plugin that allows Flutter to communicate with a native WebView.
76

8-
***Warning:***
7+
**_Warning:_**
98
The webview is not integrated in the widget tree, it is a native view on top of the flutter view.
109
you won't be able to use snackbars, dialogs ...
1110

@@ -21,11 +20,11 @@ For help getting started with Flutter, view our online [documentation](http://fl
2120
new MaterialApp(
2221
routes: {
2322
"/": (_) => new WebviewScaffold(
24-
url: "https://www.google.com",
25-
appBar: new AppBar(
26-
title: new Text("Widget webview"),
27-
),
28-
)
23+
url: "https://www.google.com",
24+
appBar: new AppBar(
25+
title: new Text("Widget webview"),
26+
),
27+
),
2928
},
3029
);
3130
```
@@ -44,20 +43,20 @@ return new MaterialApp(
4443
routes: {
4544
'/': (_) => const MyHomePage(title: 'Flutter WebView Demo'),
4645
'/widget': (_) => new WebviewScaffold(
47-
url: selectedUrl,
48-
appBar: new AppBar(
49-
title: const Text('Widget webview'),
50-
),
51-
withZoom: true,
52-
withLocalStorage: true,
53-
hidden: true,
54-
initialChild: Container(
55-
color: Colors.redAccent,
56-
child: const Center(
57-
child: Text('Waiting.....'),
58-
),
59-
),
60-
)
46+
url: selectedUrl,
47+
appBar: new AppBar(
48+
title: const Text('Widget webview'),
49+
),
50+
withZoom: true,
51+
withLocalStorage: true,
52+
hidden: true,
53+
initialChild: Container(
54+
color: Colors.redAccent,
55+
child: const Center(
56+
child: Text('Waiting.....'),
57+
),
58+
),
59+
),
6160
},
6261
);
6362
```
@@ -66,11 +65,12 @@ return new MaterialApp(
6665
so you can take control of the webview from anywhere in the app
6766

6867
listen for events
68+
6969
```dart
7070
final flutterWebviewPlugin = new FlutterWebviewPlugin();
7171
7272
flutterWebviewPlugin.onUrlChanged.listen((String url) {
73-
73+
7474
});
7575
```
7676

@@ -111,12 +111,14 @@ flutterWebviewPlugin.close();
111111
final flutterWebviewPlugin = new FlutterWebviewPlugin();
112112
113113
flutterWebviewPlugin.launch(url,
114-
fullScreen: false,
115-
rect: new Rect.fromLTWH(
116-
0.0,
117-
0.0,
118-
MediaQuery.of(context).size.width,
119-
300.0));
114+
fullScreen: false,
115+
rect: new Rect.fromLTWH(
116+
0.0,
117+
0.0,
118+
MediaQuery.of(context).size.width,
119+
300.0,
120+
),
121+
);
120122
```
121123

122124
### Webview Events
@@ -126,41 +128,51 @@ flutterWebviewPlugin.launch(url,
126128
- `Stream<WebViewStateChanged>` onStateChanged
127129
- `Stream<String>` onError
128130

129-
***Don't forget to dispose webview***
131+
**_Don't forget to dispose webview_**
130132
`flutterWebviewPlugin.dispose()`
131133

132134
### Webview Functions
133135

134136
```dart
135-
Future<Null> launch(String url,
136-
{Map<String, String> headers: null,
137-
bool withJavascript: true,
138-
bool clearCache: false,
139-
bool clearCookies: false,
140-
bool hidden: false,
141-
bool enableAppScheme: true,
142-
Rect rect: null,
143-
String userAgent: null,
144-
bool withZoom: false,
145-
bool withLocalStorage: true,
146-
bool withLocalUrl: true,
147-
bool scrollBar: true});
137+
Future<Null> launch(String url, {
138+
Map<String, String> headers: null,
139+
bool withJavascript: true,
140+
bool clearCache: false,
141+
bool clearCookies: false,
142+
bool hidden: false,
143+
bool enableAppScheme: true,
144+
Rect rect: null,
145+
String userAgent: null,
146+
bool withZoom: false,
147+
bool withLocalStorage: true,
148+
bool withLocalUrl: true,
149+
bool scrollBar: true,
150+
bool supportMultipleWindows: false,
151+
bool appCacheEnabled: false,
152+
bool allowFileURLs: false,
153+
});
148154
```
155+
149156
```dart
150157
Future<String> evalJavascript(String code);
151158
```
159+
152160
```dart
153161
Future<Map<String, dynamic>> getCookies();
154162
```
163+
155164
```dart
156165
Future<Null> resize(Rect rect);
157166
```
167+
158168
```dart
159169
Future<Null> show();
160170
```
171+
161172
```dart
162173
Future<Null> hide();
163174
```
175+
164176
```dart
165177
Future<Null> reloadUrl(String url);
166178
```

android/src/main/java/com/flutter_webview_plugin/FlutterWebviewPlugin.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ private void openUrl(MethodCall call, MethodChannel.Result result) {
9696
boolean appCacheEnabled = call.argument("appCacheEnabled");
9797
Map<String, String> headers = call.argument("headers");
9898
boolean scrollBar = call.argument("scrollBar");
99+
boolean allowFileURLs = call.argument("allowFileURLs");
99100

100101
if (webViewManager == null || webViewManager.closed == true) {
101102
webViewManager = new WebviewManager(activity);
@@ -116,7 +117,8 @@ private void openUrl(MethodCall call, MethodChannel.Result result) {
116117
withLocalStorage,
117118
scrollBar,
118119
supportMultipleWindows,
119-
appCacheEnabled
120+
appCacheEnabled,
121+
allowFileURLs
120122
);
121123
result.success(null);
122124
}

android/src/main/java/com/flutter_webview_plugin/WebviewManager.java

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,17 +194,35 @@ private void clearCache() {
194194
webView.clearFormData();
195195
}
196196

197-
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) {
197+
void openUrl(
198+
boolean withJavascript,
199+
boolean clearCache,
200+
boolean hidden,
201+
boolean clearCookies,
202+
String userAgent,
203+
String url,
204+
Map<String, String> headers,
205+
boolean withZoom,
206+
boolean withLocalStorage,
207+
boolean scrollBar,
208+
boolean supportMultipleWindows,
209+
boolean appCacheEnabled,
210+
boolean allowFileURLs,
211+
) {
198212
webView.getSettings().setJavaScriptEnabled(withJavascript);
199213
webView.getSettings().setBuiltInZoomControls(withZoom);
200214
webView.getSettings().setSupportZoom(withZoom);
201215
webView.getSettings().setDomStorageEnabled(withLocalStorage);
202216
webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(supportMultipleWindows);
217+
203218
webView.getSettings().setSupportMultipleWindows(supportMultipleWindows);
219+
204220
webView.getSettings().setAppCacheEnabled(appCacheEnabled);
221+
222+
webView.getSettings().setAllowFileAccessFromFileURLs(allowFileURLs);
223+
webView.getSettings().setAllowUniversalAccessFromFileURLs(allowFileURLs);
205224

206225
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
207-
Log.d("WebviewManager", "Mixed Content enabled");
208226
webView.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_COMPATIBILITY_MODE);
209227
}
210228

lib/src/base.dart

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import 'dart:async';
22
import 'dart:ui';
3+
import 'dart:io';
34

45
import 'package:flutter/material.dart';
56
import 'package:flutter/services.dart';
@@ -109,7 +110,9 @@ class FlutterWebviewPlugin {
109110
bool withLocalUrl,
110111
bool scrollBar,
111112
bool supportMultipleWindows,
112-
bool appCacheEnabled}) async {
113+
bool appCacheEnabled,
114+
bool allowFileURLs,
115+
}) async {
113116
final args = <String, dynamic>{
114117
'url': url,
115118
'withJavascript': withJavascript ?? true,
@@ -123,7 +126,8 @@ class FlutterWebviewPlugin {
123126
'withLocalUrl': withLocalUrl ?? false,
124127
'scrollBar': scrollBar ?? true,
125128
'supportMultipleWindows': supportMultipleWindows ?? false,
126-
'appCacheEnabled': appCacheEnabled ?? false
129+
'appCacheEnabled': appCacheEnabled ?? false,
130+
"allowFileURLs": allowFileURLs ?? false,
127131
};
128132

129133
if (headers != null) {

lib/src/webview_scaffold.dart

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ class WebviewScaffold extends StatefulWidget {
2222
final bool withZoom;
2323
final bool withLocalStorage;
2424
final bool withLocalUrl;
25+
final bool allowFileURLs;
2526
final bool scrollBar;
2627
final bool hidden;
2728
final Widget initialChild;
@@ -48,7 +49,9 @@ class WebviewScaffold extends StatefulWidget {
4849
this.withLocalUrl,
4950
this.scrollBar,
5051
this.hidden = false,
51-
this.initialChild})
52+
this.initialChild,
53+
this.allowFileURLs,
54+
})
5255
: super(key: key);
5356

5457
@override
@@ -111,6 +114,7 @@ class _WebviewScaffoldState extends State<WebviewScaffold> {
111114
scrollBar: widget.scrollBar,
112115
supportMultipleWindows: widget.supportMultipleWindows,
113116
appCacheEnabled: widget.appCacheEnabled,
117+
allowFileURLs: widget.allowFileURLs,
114118
);
115119
} else {
116120
if (_rect != value) {

0 commit comments

Comments
 (0)