Repository navigation
Expand file tree
/
Copy pathWebView.dart
More file actions
210 lines (179 loc) · 6.18 KB
/
Copy pathWebView.dart
File metadata and controls
210 lines (179 loc) · 6.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
import 'dart:async';
import 'package:flutter/foundation.dart';
import 'package:flutter/gestures.dart';
import 'package:flutter/services.dart';
import 'package:flutter/widgets.dart';
typedef void WebViewCreatedCallback(WebViewController controller);
enum JavaScriptMode {
/// JavaScript execution is disabled.
disabled,
/// JavaScript execution is not restricted.
unrestricted,
}
/// A web view widget for showing html content.
class WebView extends StatefulWidget {
/// Creates a new web view.
///
/// The web view can be controlled using a `WebViewController` that is passed to the
/// `onWebViewCreated` callback once the web view is created.
///
/// The `gestureRecognizers` and `javaScriptMode` parameters must not be null.
const WebView({
Key key,
this.onWebViewCreated,
this.initialUrl,
this.javaScriptMode = JavaScriptMode.disabled,
this.gestureRecognizers = const <OneSequenceGestureRecognizer>[],
}) : assert(gestureRecognizers != null),
assert(javaScriptMode != null),
super(key: key);
/// If not null invoked once the web view is created.
final WebViewCreatedCallback onWebViewCreated;
/// Which gestures should be consumed by the web view.
///
/// It is possible for other gesture recognizers to be competing with the web view on pointer
/// events, e.g if the webview is inside a [ListView] the [ListView] will want to handle
/// vertical drags. The web view will claim gestures that are recognized by any of the
/// recognizers on this list.
///
/// When this list is empty, the web view will only handle pointer events for gestures that
/// were not claimed by any other gesture recognizer.
final List<OneSequenceGestureRecognizer> gestureRecognizers;
/// The initial URL to load.
final String initialUrl;
/// Whether JavaScript execution is enabled.
final JavaScriptMode javaScriptMode;
@override
State<StatefulWidget> createState() => _WebViewState();
}
class _WebViewState extends State<WebView> {
final Completer<WebViewController> _controller =
Completer<WebViewController>();
_WebSettings _settings;
@override
Widget build(BuildContext context) {
if (defaultTargetPlatform == TargetPlatform.android) {
return GestureDetector(
// We prevent text selection by intercepting long press event.
// This is a temporary workaround to prevent a native crash on a second
// text selection.
// TODO(amirh): remove this when the selection handles crash is resolved.
// https://github.com/flutter/flutter/issues/21239
onLongPress: () {},
child: AndroidView(
viewType: 'plugins.flutter.io/webview',
onPlatformViewCreated: _onPlatformViewCreated,
gestureRecognizers: widget.gestureRecognizers,
// WebView content is not affected by the Android view's layout direction,
// we explicitly set it here so that the widget doesn't require an ambient
// directionality.
layoutDirection: TextDirection.rtl,
creationParams: _CreationParams.fromWidget(widget).toMap(),
creationParamsCodec: const StandardMessageCodec(),
),
);
}
return Text(
'$defaultTargetPlatform is not yet supported by the webview_flutter plugin');
}
@override
void initState() {
super.initState();
_settings = _WebSettings.fromWidget(widget);
}
@override
void didUpdateWidget(WebView oldWidget) {
super.didUpdateWidget(oldWidget);
final _WebSettings newSettings = _WebSettings.fromWidget(widget);
final Map<String, dynamic> settingsUpdate =
_settings.updatesMap(newSettings);
_updateSettings(settingsUpdate);
_settings = newSettings;
}
Future<void> _updateSettings(Map<String, dynamic> update) async {
if (update == null) {
return;
}
final WebViewController controller = await _controller.future;
controller._updateSettings(update);
}
void _onPlatformViewCreated(int id) {
final WebViewController controller = WebViewController._(id);
_controller.complete(controller);
if (widget.onWebViewCreated != null) {
widget.onWebViewCreated(controller);
}
}
}
class _CreationParams {
_CreationParams({this.initialUrl, this.settings});
static _CreationParams fromWidget(WebView widget) {
return _CreationParams(
initialUrl: widget.initialUrl,
settings: _WebSettings.fromWidget(widget),
);
}
final String initialUrl;
final _WebSettings settings;
Map<String, dynamic> toMap() {
return <String, dynamic>{
'initialUrl': initialUrl,
'settings': settings.toMap(),
};
}
}
class _WebSettings {
_WebSettings({
this.javaScriptMode,
});
static _WebSettings fromWidget(WebView widget) {
return _WebSettings(javaScriptMode: widget.javaScriptMode);
}
final JavaScriptMode javaScriptMode;
Map<String, dynamic> toMap() {
return <String, dynamic>{
'jsMode': javaScriptMode.index,
};
}
Map<String, dynamic> updatesMap(_WebSettings newSettings) {
if (javaScriptMode == newSettings.javaScriptMode) {
return null;
}
return <String, dynamic>{
'jsMode': newSettings.javaScriptMode.index,
};
}
}
/// Controls a [WebView].
///
/// A [WebViewController] instance can be obtained by setting the [WebView.onWebViewCreated]
/// callback for a [WebView] widget.
class WebViewController {
WebViewController._(int id)
: _channel = MethodChannel('plugins.flutter.io/webview_$id');
final MethodChannel _channel;
/// Loads the specified URL.
///
/// `url` must not be null.
///
/// Throws an ArgumentError if `url` is not a valid URL string.
Future<void> loadUrl(String url) async {
assert(url != null);
_validateUrlString(url);
return _channel.invokeMethod('loadUrl', url);
}
Future<void> _updateSettings(Map<String, dynamic> update) async {
return _channel.invokeMethod('updateSettings', update);
}
}
// Throws an ArgumentError if url is not a valid url string.
void _validateUrlString(String url) {
try {
final Uri uri = Uri.parse(url);
if (uri.scheme.isEmpty) {
throw ArgumentError('Missing scheme in URL string: "$url"');
}
} on FormatException catch (e) {
throw ArgumentError(e);
}
}