import 'dart:js_interop'; import 'package:flutter/foundation.dart'; import 'package:flutter/widgets.dart'; import 'package:web/web.dart' as web; import 'artifact_html_links_web.dart'; /// The frame's document cannot be pinched: a pinch over it is a page zoom, /// which the app's viewport (`user-scalable=no`) refuses, and the sandbox /// keeps the document out of the app's reach. The full-screen view zooms /// the live frame with buttons instead. const bool inertHtmlZoomButtons = true; /// A sandboxed iframe ([artifactHtmlSandbox]): the document cannot run /// script, reach the app's origin, submit forms or navigate the app. Its /// links are fixed first ([artifactHtmlWithInertLinks]): a `#place` scrolls /// the frame, a web or mail address opens a new tab, anything else is inert. /// /// The frame scrolls its document itself, by wheel, trackpad and touch. /// [zoom] scales the live frame as a browser's page zoom would: the frame is /// laid out 1/zoom as wide and tall and scaled back to fill its box, so the /// document reflows and still scrolls inside it. In the app, Chrome returns /// the frame's document to its top when a zoom step resizes it. /// /// While another route covers this one (a menu, a sheet) the frame stops /// taking pointers: a platform view takes every click over its area, even /// where Flutter draws that menu on top. class InertHtmlView extends StatefulWidget { const InertHtmlView({ super.key, required this.html, this.interactive = false, this.zoom, }); final String html; /// The full-screen view's frame. Pointers already reach the frame directly /// on the web, inline as in full screen. final bool interactive; final ValueListenable? zoom; @override State createState() => _InertHtmlViewState(); } class _InertHtmlViewState extends State { web.HTMLIFrameElement? _frame; bool _covered = false; @override void initState() { super.initState(); widget.zoom?.addListener(_style); } @override void didUpdateWidget(covariant InertHtmlView oldWidget) { super.didUpdateWidget(oldWidget); if (oldWidget.zoom != widget.zoom) { oldWidget.zoom?.removeListener(_style); widget.zoom?.addListener(_style); _style(); } } @override void dispose() { widget.zoom?.removeListener(_style); super.dispose(); } void _style() { final frame = _frame; if (frame == null) return; final zoom = widget.zoom?.value ?? 1; final size = '${100 / zoom}%'; frame.style ..width = size ..height = size ..transformOrigin = '0 0' ..transform = zoom == 1 ? '' : 'scale($zoom)' ..pointerEvents = _covered ? 'none' : ''; } @override Widget build(BuildContext context) { final covered = !(ModalRoute.of(context)?.isCurrent ?? true); if (covered != _covered) { _covered = covered; _style(); } return HtmlElementView.fromTagName( key: ValueKey(widget.html.hashCode), tagName: 'iframe', onElementCreated: (element) { final frame = element as web.HTMLIFrameElement; frame.setAttribute('sandbox', artifactHtmlSandbox); frame.srcdoc = artifactHtmlWithInertLinks(widget.html).toJS; frame.style ..border = 'none' ..backgroundColor = 'white'; _frame = frame; _style(); }, ); } }