已成功在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
相关产品推荐
相关产品推荐

