首页 > 解决方案 > 在AppWebview中颤动:对点击pdf链接做出反应

问题描述

我正在尝试在颤振中使用 InAppWebview 插件,效果很好,但是当我单击 pdf 链接时,它什么也不做。

尝试一下,只需点击谷歌搜索中的第一个或第二个链接。

也不shouldOverrideUrlLoadingonDownloadStart

关于如何显示或如何拦截它的任何想法?

    class WebViewWidget extends StatefulWidget {
      final String url;
    
      WebViewWidget(this.url);
    
      @override
      _WebViewWidgetState createState() => _WebViewWidgetState();
    }
    
    class _WebViewWidgetState extends State<WebViewWidget> {
      final Completer<InAppWebViewController> _controller =
          Completer<InAppWebViewController>();
    
      final InAppWebViewGroupOptions _options = InAppWebViewGroupOptions(
        crossPlatform: InAppWebViewOptions(
          useShouldOverrideUrlLoading: true,
          mediaPlaybackRequiresUserGesture: false,
          javaScriptEnabled: true,
        ),
        android: AndroidInAppWebViewOptions(
          useHybridComposition: true,
          loadWithOverviewMode: true,
          useWideViewPort: false,
          builtInZoomControls: false,
          domStorageEnabled: true,
          supportMultipleWindows: true,
        ),
      );
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          body: Builder(builder: (BuildContext context) {
            return InAppWebView(
                initialUrlRequest: URLRequest(url: Uri.parse("https://www.google.com/search?client=firefox-b-d&q=pdf+example")),
                initialOptions: _options,
                shouldOverrideUrlLoading: (controller, action) {
                  print("override");
                  return Future.value(NavigationActionPolicy.ALLOW);
                },
                onWebViewCreated: (webViewController) {
                  _controller.complete(webViewController);
                },
                onDownloadStart: (controller, uri) {
                  print("download");
                },
             );
          }),
        );
      }
    }

标签: flutterpdfflutter-inappwebview

解决方案


onDownloadStart被调用useOnDownloadStart: true必须设置为 on crossPlatform: InAppWebViewOptions()。一旦完成,回调就会被正确调用。


推荐阅读