加载内容时如何在Flutter WebView中显示微调器

问题描述

我正在尝试在我的应用程序中使用Flutter Web视图。它正在运行,但是,我尝试启动的页面需要一些时间才能加载。因此,用户显示内容之前会看到一个空白页

我想继续显示微调框,直到显示页面内容

这是我尝试过的代码

 body: Stack(
        children: <Widget>[
          WebView(
            key: _key,initialUrl: widget.selectedUrl,javascriptMode: JavascriptMode.unrestricted,onPageFinished: (finish) {
              setState(() {
                isLoading = false;
              });
            },),isLoading
              ? Center(
                  child: CircularProgressIndicator(),)
              : Stack(),],

我看到了微调器,但在显示内容之前就消失了。因此,大约5到8秒钟的时间里,用户会看到一块空的画布而没有任何旋转器

在屏幕上显示所有内容之前,如何保持微调器?

谢谢

医生摘要(要查看所有详细信息,请运行Flutter doctor -v): [✓] Flutter(频道稳定,在Mac OS X 10.15.6 19G2021上为1.22.0,语言环境为美国)

[✓] Android工具链-为Android设备开发(Android SDK版本28.0.3) [✓] Xcode-为iOS和macOS开发(Xcode 12.0) [✓] Android Studio(3.6版) [✓] VS代码(1.49.2版) [✓]已连接的设备(1个可用)

•找不到问题!

  • 更新。

调用的URL在显示页面之前进行了一些其他验证。我在代码添加一个计时器。

不确定这是否是正确的方法

 Future.delayed(const Duration(milliseconds: 9000),() {
                  setState(() {
                    isLoading = false;
                  });
                });

解决方法

// before build method
bool isLoading = true;

// somewhere in stateful calss
 Stack(
            children: [
              WebView(
                navigationDelegate: (NavigationRequest request) {
                  if (request.url
                      .startsWith('https://books-by-weight.herokuapp.com')) {
                    Navigator.pop(context);
                    // print('blocking navigation to $request}');
                    return NavigationDecision.prevent;
                  }
                  // print('allowing navigation to $request');
                  return NavigationDecision.navigate;
                },initialUrl: url
                javascriptMode: JavascriptMode.unrestricted,onWebViewCreated: (WebViewController webViewController) {
                  _controller.complete(webViewController);
                },onPageFinished: (finish) {
                  setState(() {
                    isLoading = false;
                  });
                },),isLoading
                  ? Center(
                      child: Container(
                        height: 30,width: 30,child: CircularProgressIndicator(
                          backgroundColor: Colors.primaries[0],strokeWidth: 3,)
                  : Stack(),],