跳转到内容

Web

Web 由 webview_all_web 1.2.0 提供,底层是 HTML iframe

Controller WebWebViewController
Widget WebWebViewWidget
Delegate WebNavigationDelegate
Cookie manager WebWebViewCookieManager
引擎 浏览器 iframe + Dart JS interop
final params = WebWebViewControllerCreationParams(
iFrameAllow: 'camera; microphone; fullscreen',
iFrameSandbox: 'allow-same-origin allow-scripts allow-forms',
iFrameReferrerPolicy: 'strict-origin-when-cross-origin',
iFrameAttributes: const <String, String?>{
'loading': 'lazy',
},
);
API 作用
setIFrameAttribute 设置或移除任意 iframe 属性。
setIFrameAllow 设置 allow
setIFrameSandbox 设置 sandbox
setIFrameReferrerPolicy 设置 referrerpolicy

简单 GET 会直接设置 iframe src

await controller.loadRequest(Uri.parse('https://example.com'));

带 method、headers 或 body 的请求会使用浏览器 fetch,再把响应渲染为 data: URL:

await controller.loadRequest(
Uri.parse('https://api.example.com/page'),
headers: const <String, String>{'X-App': 'demo'},
);

跨域 fetch 需要服务端 CORS 允许。

以下能力需要同源 iframe:

  • runJavaScript
  • runJavaScriptReturningResult
  • JavaScript channel
  • console hook
  • JavaScript dialog hook
  • 滚动读写
  • 读取标题
API 行为
loadFile UnsupportedError
setUserAgent(nonNull) UnsupportedError
SSL auth 浏览器不暴露可恢复证书决策。
HTTP auth iframe 没有 WebView 风格回调。
跨域 JS 浏览器同源策略禁止。