Flutter Webview:未加载初始 url 的替代屏幕

Flutter Webview : Alternate screen for inital url not loaded

我的网页未加载时需要备用屏幕;

注意:网页托管在本地主机上,因此无法进行基于互联网连接的转换。

提问:能否ping通IP并换屏

任何可能的方法来实现这个?

** main.dart **

import 'package:flutter/material.dart';
import 'package:webview_flutter/webview_flutter.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'flutter webview',
      home: Scaffold(
          appBar: AppBar(
            title: const Text('flutter webview'),
          ),
          body: WebView(
              initialUrl: "http://192.168.29.8/public/_weblogin.html",
              javascriptMode: JavascriptMode.unrestricted)),
    );
  }
}


您可以在 onWebResourceError 上查看替代设计 完整代码:

import 'package:flutter/material.dart';
import 'package:webview_flutter/webview_flutter.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const HomeView(),
    );
  }
}

class HomeView extends StatefulWidget {
  const HomeView({Key? key}) : super(key: key);

  @override
  _HomeViewState createState() => _HomeViewState();
}

class _HomeViewState extends State<HomeView> {
  bool isError = false;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("Home"),
      ),
      body: Stack(
        children: [
          if (!isError)
            WebView(
              initialUrl: 'http://192.168.29.8/public/_weblogin.html',
              javascriptMode: JavascriptMode.unrestricted,
              onWebResourceError: (error) => setState(() {
                isError = true;
              }),
            ),
          if (isError) const Center(child: Text("WEB PAGE ERROR")),
        ],
      ),
    );
  }
}