Skip to content

Commit 73f438e

Browse files
committed
Fix webview transformers not working properly on non-web platforms.
1 parent 0b7f180 commit 73f438e

6 files changed

+31
-35
lines changed

lib/src/transformers/node_transformers/passive_embedded_video_transformer.dart

+7-6
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,11 @@ class _PassiveEmbeddedVideoWidgetState
8686
@override
8787
void initState() {
8888
super.initState();
89+
if (!isPlatformSupportedForWebView) {
90+
print('Unsupported platform: $defaultTargetPlatform for embedded '
91+
'video.');
92+
return;
93+
}
8994
final PlatformWebViewControllerCreationParams params;
9095
if (kIsWeb) {
9196
// WebView on web only supports loadRequest. Any other method invocation
@@ -177,9 +182,7 @@ class _PassiveEmbeddedVideoWidgetState
177182

178183
Widget buildEmbeddedYoutubeVideo(
179184
BuildContext context, EmbeddedYoutubeVideoProperties properties) {
180-
if (PassiveEmbeddedVideoWidget.supportedPlatforms
181-
.contains(Theme.of(context).platform) ||
182-
kIsWeb) {
185+
if (isPlatformSupportedForWebView) {
183186
return WebViewWidget(
184187
controller: _controller,
185188
key: ValueKey(properties),
@@ -205,9 +208,7 @@ class _PassiveEmbeddedVideoWidgetState
205208

206209
Widget buildEmbeddedVimeoVideo(
207210
BuildContext context, EmbeddedVimeoVideoProperties properties) {
208-
if (PassiveEmbeddedVideoWidget.supportedPlatforms
209-
.contains(Theme.of(context).platform) ||
210-
kIsWeb) {
211+
if (isPlatformSupportedForWebView) {
211212
return WebViewWidget(
212213
key: ValueKey(properties),
213214
controller: _controller,

lib/src/transformers/node_transformers/passive_web_view_transformer.dart

+8-10
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,12 @@ class _PassiveWebViewWidgetState extends State<PassiveWebViewWidget> {
8686
@override
8787
void initState() {
8888
super.initState();
89-
final props = widget.node.properties;
89+
if (!isPlatformSupportedForWebView) {
90+
print('Unsupported platform: $defaultTargetPlatform for WebView.');
91+
return;
92+
}
9093

94+
final props = widget.node.properties;
9195
if (kIsWeb) {
9296
// WebView on web only supports loadRequest. Any other method invocation
9397
// on the controller will result in an exception. Be aware!!
@@ -187,9 +191,7 @@ class _PassiveWebViewWidgetState extends State<PassiveWebViewWidget> {
187191

188192
Widget buildWebpageWebView(
189193
BuildContext context, WebPageWebViewProperties properties) {
190-
if (!PassiveWebViewWidget.supportedPlatforms
191-
.contains(Theme.of(context).platform) &&
192-
!kIsWeb) {
194+
if (!isPlatformSupportedForWebView) {
193195
return WebViewPreviewWidget(
194196
icon: Icon(Icons.language_rounded),
195197
node: widget.node,
@@ -213,9 +215,7 @@ class _PassiveWebViewWidgetState extends State<PassiveWebViewWidget> {
213215

214216
Widget buildGoogleMapsWebView(
215217
BuildContext context, GoogleMapsWebViewProperties properties) {
216-
if (!PassiveWebViewWidget.supportedPlatforms
217-
.contains(Theme.of(context).platform) &&
218-
!kIsWeb) {
218+
if (!isPlatformSupportedForWebView) {
219219
return WebViewPreviewWidget(
220220
icon: Icon(Icons.map_outlined),
221221
node: widget.node,
@@ -230,9 +230,7 @@ class _PassiveWebViewWidgetState extends State<PassiveWebViewWidget> {
230230

231231
Widget buildTwitterWebView(
232232
BuildContext context, TwitterWebViewProperties properties) {
233-
if (!PassiveWebViewWidget.supportedPlatforms
234-
.contains(Theme.of(context).platform) &&
235-
!kIsWeb) {
233+
if (!isPlatformSupportedForWebView) {
236234
return WebViewPreviewWidget(
237235
icon: ImageIcon(
238236
NetworkImage('https://img.icons8.com/color/344/twitter--v2.png')),

lib/src/transformers/transformer_functions.dart

+7-19
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,11 @@ import 'package:collection/collection.dart';
33
import 'package:flutter/foundation.dart';
44
import 'package:flutter/material.dart';
55
import 'package:flutter_svg/flutter_svg.dart';
6-
import 'package:universal_html/html.dart';
6+
import 'package:universal_html/html.dart' as html;
77

88
import '../utils/extensions.dart';
99
import 'transformers.dart';
1010

11-
/// Convenience method that looks up the [BaseNode] of a given [id].
12-
///
13-
/// This function uses [nodeDelegate] to look up the node. The node must
14-
/// be registered before-hand.
15-
// BaseNode getNodeByID(String id) => nodeDelegate.getNodeByID(id);
16-
17-
/// Convenience method that looks up the [BaseNode] of a given [id].
18-
///
19-
/// This function uses [nodeDelegate] to look up the node.
20-
// bool hasNodeID(String id) => nodeDelegate.hasNodeID(id);
21-
22-
/// Convenience method that gets a transformer from our registry of
23-
/// passive transformers for the sdk.
24-
// T getPassiveTransformer<T>() {
25-
// return passiveTransformerManager.getTransformer<T>();
26-
// }
27-
2811
void convertNodeToButtonType(ButtonTypeEnum type, ButtonNode node) {
2912
switch (type) {
3013
case ButtonTypeEnum.elevated:
@@ -412,7 +395,7 @@ String getVideoUrl(EmbeddedVideoSource source, String baseUrl) {
412395
String updatedBaseUrl = baseUrl;
413396
if (kIsWeb && !kReleaseMode) {
414397
// This allows to test and debug video player scripts in local environment.
415-
var url = Uri.parse(window.location.href);
398+
var url = Uri.parse(html.window.location.href);
416399
updatedBaseUrl = url.origin;
417400
}
418401

@@ -594,3 +577,8 @@ Widget wrapWithScrollable({
594577
child: child,
595578
);
596579
}
580+
581+
bool get isPlatformSupportedForWebView =>
582+
kIsWeb ||
583+
PassiveEmbeddedVideoWidget.supportedPlatforms
584+
.contains(defaultTargetPlatform);
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
1+
library web_webview_platform;
2+
13
export 'web_webview_platform_stub.dart'
24
if (dart.library.html) 'web_webview_platform_web.dart';
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
import 'package:webview_flutter/webview_flutter.dart';
22

3+
/// Stub for [WebViewPlatform] for web. Importing actual [WebWebViewPlatform]
4+
/// will cause error on non-web platforms. This stub is used to avoid that on
5+
/// unsupported platforms.
36
class WebWebViewPlatform extends WebViewPlatform {}
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
1+
/// This file is used to import the [WebWebViewPlatform] class from the
2+
/// `webview_flutter_web` package. This is done to avoid errors on non-web
3+
/// platforms.
4+
15
export 'package:webview_flutter_web/webview_flutter_web.dart';

0 commit comments

Comments
 (0)