You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

已成功在GitHub托管Flutter Web应用,如何改用WebView托管网页?

使用WebView加载已部署的Flutter Web应用

核心实现代码

先确保已添加webview_flutter依赖到pubspec.yaml:

dependencies:
  webview_flutter: ^4.4.0 # 建议使用最新稳定版

直接通过initialUrl加载你的GitHub Pages部署地址,替代本地加载方式:

import 'package:webview_flutter/webview_flutter.dart';

class DeployedWebView extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return WebView(
      initialUrl: 'https://your-username.github.io/your-repo-name/', // 替换为你的实际部署URL
      javascriptMode: JavascriptMode.unrestricted, // Flutter Web应用依赖JS,必须开启
    );
  }
}

平台配置注意事项

  • Android:在android/app/src/main/AndroidManifest.xml中添加网络权限:
    <uses-permission android:name="android.permission.INTERNET" />
    
  • iOS:若使用HTTPS地址(GitHub Pages默认支持),无需额外配置;若用HTTP,需在ios/Runner/Info.plist中添加:
    <key>NSAppTransportSecurity</key>
    <dict>
      <key>NSAllowsArbitraryLoads</key>
      <true/>
    </dict>
    

内容的提问来源于stack exchange,提问作者Dev.s

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.08 03:50:42