问题描述
我是开发新手,我一直在尝试在 Flutter 中制作一个简单的 WebView 应用程序,但如果用户未连接到互联网,我将无法实现“无连接/404”页面。到目前为止,我已经尝试实现“onWebViewError”和“onLoadError”但没有成功。任何帮助将不胜感激!
这是我的代码到目前为止的样子: main.dart
import 'package:Flutter/cupertino.dart';
import 'package:Flutter/material.dart';
import 'package:quest_musique/my_webview.dart';
void main() {runApp(MyApp());}
// Theme data etc.
class MyApp extends StatelessWidget {
Widget build(BuildContext context) {
return MaterialApp(
home: HomePage(),theme: ThemeData(
brightness: Brightness.dark,primaryColor: Colors.black,accentColor: Colors.orangeAccent,backgroundColor: Colors.white,fontFamily: 'Georgia'),);
}
}
// The web view widget,simply enter the desired landing page in selectedURL.
class HomePage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(""),toolbarHeight: 1,elevation: 3,),body: MyWebView(
title: "",selectedUrl: "https://www.google.com",)
);
}
}
my_webview.dart
import 'package:Flutter/material.dart';
import 'package:webview_Flutter/webview_Flutter.dart';
class MyWebView extends StatelessWidget {
final String title;
final String selectedUrl;
final Completer<WebViewController> _controller = Completer<WebViewController>();
MyWebView({
required this.title,required this.selectedUrl,});
@override
Widget build(BuildContext context) {
return Scaffold(
body: WebView(
initialUrl: selectedUrl,javascriptMode: JavascriptMode.unrestricted,onWebViewCreated: (WebViewController webViewController) {
_controller.complete(webViewController);
},));
}
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)