Skip to content

Commit 8d4d529

Browse files
committed
Add the ability for iframe to switch url if HTML data changes
1 parent 86ac375 commit 8d4d529

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

lib/src/replaced_element.dart

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,11 @@ class ImageContentElement extends ReplacedElement {
9090
}
9191
}
9292

93+
/// Create a webview controller and old src independent of [IframeContentElement]
94+
/// to make sure it doesn't reset when the html string is updated
95+
WebViewController controller;
96+
String oldUrl;
97+
9398
/// [IframeContentElement is a [ReplacedElement] with web content.
9499
class IframeContentElement extends ReplacedElement {
95100
final String src;
@@ -110,11 +115,18 @@ class IframeContentElement extends ReplacedElement {
110115
@override
111116
Widget toWidget(RenderContext context) {
112117
final sandboxMode = attributes["sandbox"];
118+
if (oldUrl != null && src != oldUrl && controller != null) {
119+
controller.loadUrl(src);
120+
}
121+
oldUrl = src;
113122
return Container(
114123
width: width ?? (height ?? 150) * 2,
115124
height: height ?? (width ?? 300) / 2,
116125
child: WebView(
117126
initialUrl: src,
127+
onWebViewCreated: (WebViewController webController) {
128+
controller = webController;
129+
},
118130
javascriptMode: sandboxMode == null || sandboxMode == "allow-scripts"
119131
? JavascriptMode.unrestricted
120132
: JavascriptMode.disabled,

0 commit comments

Comments
 (0)