Skip to content

Commit b600766

Browse files
committed
Add WebView Web Wasm Support
1 parent cdced3c commit b600766

File tree

2 files changed

+65
-15
lines changed

2 files changed

+65
-15
lines changed

lib/src/transformers/node_transformers/passive_web_view_transformer.dart

Lines changed: 64 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
import 'dart:convert';
22
import 'dart:math';
3+
import 'dart:ui_web';
34

45
import 'package:codelessly_api/codelessly_api.dart';
56
import 'package:flutter/foundation.dart';
67
import 'package:flutter/gestures.dart';
78
import 'package:flutter/material.dart';
89
import 'package:url_launcher/url_launcher.dart';
10+
import 'package:web/web.dart';
911
import 'package:webview_flutter/webview_flutter.dart';
1012
import 'package:webview_flutter_android/webview_flutter_android.dart';
1113
import 'package:webview_flutter_wkwebview/webview_flutter_wkwebview.dart';
1214

1315
import '../../../codelessly_sdk.dart';
14-
import '../web/web_webview_platform.dart';
1516

1617
class PassiveWebViewTransformer extends NodeWidgetTransformer<WebViewNode> {
1718
PassiveWebViewTransformer(super.getNode, super.manager);
@@ -169,8 +170,8 @@ class _RawWebViewWidgetState extends State<RawWebViewWidget> {
169170
if (kIsWeb) {
170171
// WebView on web only supports loadRequest. Any other method invocation
171172
// on the controller will result in an exception. Be aware!!
172-
WebViewPlatform.instance = WebWebViewPlatform();
173-
_controller = WebViewController();
173+
// WebViewPlatform.instance = WebWebViewPlatform();
174+
// _controller = WebViewController();
174175
} else {
175176
final PlatformWebViewControllerCreationParams params;
176177
if (WebViewPlatform.instance is WebKitWebViewPlatform) {
@@ -200,45 +201,77 @@ class _RawWebViewWidgetState extends State<RawWebViewWidget> {
200201
WebViewMediaAutoPlaybackPolicy.alwaysPlayAllMedia);
201202
}
202203

203-
// Using this user-agent string to force the video to play in the webview
204-
// on Android. This is a hack, but it works.
205-
// _controller.setUserAgent(
206-
// 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36');
207-
208204
_controller.setBackgroundColor(
209205
props.backgroundColor?.toFlutterColor() ?? Colors.transparent);
210206
}
211207
}
212208

213209
Future<void> _loadData() {
214210
final ScopedValues scopedValues = ScopedValues.of(context);
215-
final props = widget.properties;
211+
final WebViewProperties props = widget.properties;
216212
switch (props.webviewType) {
217213
case WebViewType.webpage:
218-
final properties = props as WebPageWebViewProperties;
214+
final WebPageWebViewProperties properties =
215+
props as WebPageWebViewProperties;
219216
final String input =
220217
PropertyValueDelegate.getVariableValueFromPath<String>(
221218
properties.input,
222219
scopedValues: scopedValues) ??
223220
properties.input;
221+
if (kIsWeb) {
222+
// Use the underlying WebView directly on web.
223+
platformViewRegistry.registerViewFactory(
224+
'html-iframe',
225+
(int viewId) => HTMLIFrameElement()
226+
..setAttribute('credentialless', 'true')
227+
..width = '100%'
228+
..height = '100%'
229+
..src = input
230+
..style.border = 'none',
231+
);
232+
return Future.value();
233+
}
224234
switch (properties.pageSourceType) {
225235
case WebViewWebpageSourceType.url:
226-
print('Loading URL: $input');
227236
return _controller.loadRequest(Uri.parse(input));
228237
case WebViewWebpageSourceType.html:
229-
final content = _buildHtmlContent(input);
238+
final String content = _buildHtmlContent(input);
230239
return _controller.loadRequest(Uri.parse(content));
231240
case WebViewWebpageSourceType.asset:
232241
// provided from onWebViewCreated callback.
233242
return _controller.loadFlutterAsset(input);
234243
}
235244
case WebViewType.googleMaps:
236-
final content = buildGoogleMapsURL(
245+
final String content = buildGoogleMapsURL(
237246
props as GoogleMapsWebViewProperties, scopedValues);
247+
if (kIsWeb) {
248+
// Use the underlying WebView directly on web.
249+
platformViewRegistry.registerViewFactory(
250+
'html-iframe',
251+
(int viewId) => HTMLIFrameElement()
252+
..setAttribute('credentialless', 'true')
253+
..width = '100%'
254+
..src = content
255+
..style.border = 'none',
256+
);
257+
return Future.value();
258+
}
238259
return _controller.loadRequest(Uri.parse(content));
239260
case WebViewType.twitter:
240-
final content =
261+
final String content =
241262
buildTwitterURL(props as TwitterWebViewProperties, scopedValues);
263+
if (kIsWeb) {
264+
// Use the underlying WebView directly on web.
265+
platformViewRegistry.registerViewFactory(
266+
'html-iframe',
267+
(int viewId) => HTMLIFrameElement()
268+
..setAttribute('credentialless', 'true')
269+
..width = '100%'
270+
..src = content
271+
..style.border = 'none',
272+
);
273+
return Future.value();
274+
}
242275
return _controller.loadRequest(Uri.parse(content));
243276
}
244277
}
@@ -258,14 +291,30 @@ class _RawWebViewWidgetState extends State<RawWebViewWidget> {
258291

259292
@override
260293
Widget build(BuildContext context) {
261-
final props = widget.properties;
294+
final WebViewProperties props = widget.properties;
262295
Widget child;
263296
switch (props.webviewType) {
264297
case WebViewType.webpage:
298+
if (kIsWeb) {
299+
return const HtmlElementView(viewType: 'html-iframe');
300+
}
265301
child = buildWebpageWebView(context, props as WebPageWebViewProperties);
266302
case WebViewType.googleMaps:
303+
if (!isPlatformSupportedForWebView || widget.settings.isPreview) {
304+
return const WebViewPreviewWidget(
305+
icon: Icon(Icons.map_outlined),
306+
);
307+
}
308+
267309
child = buildWebView(props as GoogleMapsWebViewProperties);
268310
case WebViewType.twitter:
311+
if (!isPlatformSupportedForWebView || widget.settings.isPreview) {
312+
return const WebViewPreviewWidget(
313+
icon: ImageIcon(NetworkImage(
314+
'https://img.icons8.com/color/344/twitter--v2.png')),
315+
);
316+
}
317+
269318
child = buildWebView(props as TwitterWebViewProperties);
270319
}
271320

pubspec.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ dependencies:
4040
webview_flutter_android: ^3.16.7
4141
webview_flutter_wkwebview: ^3.15.0
4242
webview_flutter_web: ^0.2.3+2
43+
web: ^1.1.0
4344

4445
# 3rd Party Core Framework
4546
equatable: ^2.0.5

0 commit comments

Comments
 (0)