微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在 Flutter WebView 中编写“无连接”屏幕的最佳方法是什么?

如何解决在 Flutter WebView 中编写“无连接”屏幕的最佳方法是什么?

我是开发新手,我一直在尝试在 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 举报,一经查实,本站将立刻删除。