I am trying to set the new URL(ie an HTML string) which I am parsing every time the src changes. 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.
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.
static redraw(String newLink, String newTitle) {
IFrameElement element = document.getElementById('contentz');
element.src = newLink;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.