flutter WebView loadFile无法在Android上运行,但可以在IOS上运行

tv6aics1  于 2023-02-16  发布在  Flutter
关注(0)|答案(1)|浏览(253)

加载进度等于100,但未显示文件。
尝试更改路径尝试从西里尔文更改为拉丁文尝试externalStorageDir

@override
  void initState() {
    controller
      ..setJavaScriptMode(JavaScriptMode.unrestricted)
      ..enableZoom(true)
      ..setBackgroundColor(const Color(0x00000000))
      ..setNavigationDelegate(
        NavigationDelegate(
          onProgress: (int progress) {
            printMessage("viewer progress : $progress");
          },
          onPageStarted: (String url) {},
          onPageFinished: (String url) {},
          onWebResourceError: (WebResourceError error) {
            setState(() {
              canRenderFile = false;
            });
            printMessage("error viewer ${error.description}");
          },
        ),
      );
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    printMessage(widget._path ?? "");
    if (canRenderFile) {
      controller.loadFile(widget._path ?? "");
    }
    if (canRenderFile == true) {
      return WebViewWidget(controller: controller);
    } else {
      return DownloadFileView(
        fileName: widget.file?.name ?? "",
        fileSize: widget.file?.fileSize.toUiSize().toString() ?? "",
        onAction: () {
          widget.onDownload?.call();
        },
      );
    }
  }
}

这是我的代码canRenderFile是默认=真

piok6c0g

piok6c0g1#

在Android上,如果您尝试使用Webview来显示任何文件(如PDF格式),那么它将不会显示任何内容。进度将显示100%,没有错误,但内容不会显示。所以在这里,您将不得不使用一些库来预览文件或只是做一些代码来显示系统弹出窗口,显示查看该文件的选项

相关问题