[英]how to change src of iframe dynamically in the flutter web?
I am trying to set the new URL(ie an HTML string) which I am parsing every time the src changes.我正在尝试设置每次 src 更改时我都会解析的新 URL(即 HTML 字符串)。 but with this code, I am able to show only the first src and it is not changing and showing the same src of output every time.
但是使用这段代码,我只能显示第一个 src 并且它不会更改并且每次都显示相同的 output src。
class IframeView extends StatefulWidget { String url; class IframeView extends StatefulWidget { String url;
frameView(this.url);
@override
_IframeViewState createState() => _IframeViewState(url);
}
class _IframeViewState extends State<IframeView> {
String url;
_IframeViewState(this.url);
final html.IFrameElement _iframeElement = html.IFrameElement();
Widget _iframeWidget;
@override
void initState() {
_iframeElement.height = '500';
_iframeElement.width = '500';
_iframeElement.srcdoc = widget.url;
_iframeElement.style.border = 'none';
// print("src printing");
// print(_iframeElement.srcdoc);
// print("widget url");
// print(widget.url);
ui.platformViewRegistry.registerViewFactory('iframeElement', (int viewId) {
return _iframeElement;
});
_iframeWidget = HtmlElementView(
key: UniqueKey(),
viewType: 'iframeElement',
);
super.initState();
}
@override
void didUpdateWidget(IframeView oldWidget) {
super.didUpdateWidget(oldWidget);
print("from did update widget");
print(_iframeElement.srcdoc);
_iframeElement.srcdoc = widget.url;
}
@override
Widget build(BuildContext context) {
return Container(
margin: EdgeInsets.only(top: 50),
child: _iframeWidget,
);
}
}
This worked for me:这对我有用:
Widget buildVideoView(Key key) {
String? videoFileUrl;
String iFrameId = <some id defined in this stateful class>;
if (<default url flag>) {
videoFileUrl = <defaultVideoUrl>;
} else {
videoFileUrl = <new url>;
IFrameElement element = document.getElementById(iFrameId) as IFrameElement;
element.src = videoFileUrl;
}
html.IFrameElement _iframeElement;
Widget _iframeWidget;
_iframeElement = html.IFrameElement();
_iframeElement.id = iFrameId; << this is important
_iframeElement.height = '100%';
_iframeElement.width = '100%';
_iframeElement.allow = 'accelerometer; gyroscope;';
_iframeElement.allowFullscreen = true;
_iframeElement.src = videoFileUrl;
_iframeElement.style.border = 'none';
// ignore: undefined_prefixed_name
ui.platformViewRegistry.registerViewFactory(
'iframeElement',
(int viewId) => _iframeElement,
);
_iframeWidget = HtmlElementView(
key: key, << also important to prevent flicker/jitter/restart on rebuild
viewType: 'iframeElement',
);
return _iframeWidget;
}
I'm using _iframeElement.src = widget.url (not.srcdoc) and updating with a redraw function.我正在使用 _iframeElement.src = widget.url(not.srcdoc)并通过重绘 function 进行更新。
static redraw(String newLink, String newTitle) {
IFrameElement element = document.getElementById('contentz');
element.src = newLink;
} }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.