Skip to content

Commit c2ddc91

Browse files
committed
QUICKFIX WebView Web URL Change Update
- Fix WebView on Web not updating when a new URL is passed. Create a new platform view iFrame.
1 parent d81aa93 commit c2ddc91

File tree

1 file changed

+29
-28
lines changed

1 file changed

+29
-28
lines changed

lib/src/transformers/node_transformers/passive_web_view_transformer.dart

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ class RawWebViewWidget extends StatefulWidget {
156156
class _RawWebViewWidgetState extends State<RawWebViewWidget> {
157157
late WebViewController _controller;
158158

159+
String url = '';
159160
bool _isDataLoaded = false;
160161

161162
@override
@@ -206,86 +207,86 @@ class _RawWebViewWidgetState extends State<RawWebViewWidget> {
206207
}
207208
}
208209

210+
@override
211+
void didChangeDependencies() {
212+
super.didChangeDependencies();
213+
if (!isPlatformSupportedForWebView || widget.settings.isPreview) {
214+
print('Unsupported platform: $defaultTargetPlatform for WebView.');
215+
return;
216+
}
217+
if (!_isDataLoaded) {
218+
_isDataLoaded = true;
219+
_loadData();
220+
}
221+
}
222+
209223
Future<void> _loadData() {
210224
final ScopedValues scopedValues = ScopedValues.of(context);
211225
final WebViewProperties props = widget.properties;
212226
switch (props.webviewType) {
213227
case WebViewType.webpage:
214228
final WebPageWebViewProperties properties =
215229
props as WebPageWebViewProperties;
216-
final String input =
230+
url =
217231
PropertyValueDelegate.getVariableValueFromPath<String>(
218232
properties.input,
219233
scopedValues: scopedValues) ??
220234
properties.input;
221235
if (kIsWeb) {
222236
// Use the underlying WebView directly on web.
223237
platformViewRegistry.registerViewFactory(
224-
'html-iframe',
238+
url,
225239
(int viewId) => HTMLIFrameElement()
226240
..setAttribute('credentialless', 'true')
227241
..width = '100%'
228242
..height = '100%'
229-
..src = input
243+
..src = url
230244
..style.border = 'none',
231245
);
232246
return Future.value();
233247
}
234248
switch (properties.pageSourceType) {
235249
case WebViewWebpageSourceType.url:
236-
return _controller.loadRequest(Uri.parse(input));
250+
return _controller.loadRequest(Uri.parse(url));
237251
case WebViewWebpageSourceType.html:
238-
final String content = _buildHtmlContent(input);
252+
final String content = _buildHtmlContent(properties.input);
239253
return _controller.loadRequest(Uri.parse(content));
240254
case WebViewWebpageSourceType.asset:
241255
// provided from onWebViewCreated callback.
242-
return _controller.loadFlutterAsset(input);
256+
return _controller.loadFlutterAsset(properties.input);
243257
}
244258
case WebViewType.googleMaps:
245-
final String content = buildGoogleMapsURL(
259+
url = buildGoogleMapsURL(
246260
props as GoogleMapsWebViewProperties, scopedValues);
247261
if (kIsWeb) {
248262
// Use the underlying WebView directly on web.
249263
platformViewRegistry.registerViewFactory(
250-
'html-iframe',
264+
url,
251265
(int viewId) => HTMLIFrameElement()
252266
..setAttribute('credentialless', 'true')
253267
..width = '100%'
254-
..src = content
268+
..src = url
255269
..style.border = 'none',
256270
);
257271
return Future.value();
258272
}
259-
return _controller.loadRequest(Uri.parse(content));
273+
return _controller.loadRequest(Uri.parse(url));
260274
case WebViewType.twitter:
261-
final String content =
275+
url =
262276
buildTwitterURL(props as TwitterWebViewProperties, scopedValues);
263277
if (kIsWeb) {
264278
// Use the underlying WebView directly on web.
265279
platformViewRegistry.registerViewFactory(
266-
'html-iframe',
280+
url,
267281
(int viewId) => HTMLIFrameElement()
268282
..setAttribute('credentialless', 'true')
269283
..width = '100%'
270-
..src = content
284+
..src = url
271285
..style.border = 'none',
272286
);
273287
return Future.value();
274288
}
275-
return _controller.loadRequest(Uri.parse(content));
276-
}
277-
}
278-
279-
@override
280-
void didChangeDependencies() {
281-
super.didChangeDependencies();
282-
if (!isPlatformSupportedForWebView || widget.settings.isPreview) {
283-
print('Unsupported platform: $defaultTargetPlatform for WebView.');
284-
return;
285-
}
286-
if (!_isDataLoaded) {
287-
_isDataLoaded = true;
288-
_loadData();
289+
return _controller.loadRequest(Uri.parse(url));
289290
}
290291
}
291292

@@ -296,7 +297,7 @@ class _RawWebViewWidgetState extends State<RawWebViewWidget> {
296297
switch (props.webviewType) {
297298
case WebViewType.webpage:
298299
if (kIsWeb) {
299-
return const HtmlElementView(viewType: 'html-iframe');
300+
return HtmlElementView(viewType: url);
300301
}
301302
child = buildWebpageWebView(context, props as WebPageWebViewProperties);
302303
case WebViewType.googleMaps:

0 commit comments

Comments
 (0)