flutter inAppWebview: reacting to click on pdf link

2,005

To onDownloadStart to be called useOnDownloadStart: true must be set on crossPlatform: InAppWebViewOptions(). Once that was done the callback was called correctly.

Share:
2,005
jack_the_beast
Author by

jack_the_beast

Updated on December 02, 2022

Comments

  • jack_the_beast
    jack_the_beast over 1 year

    I'm trying to use InAppWebview plugin in flutter, works well but when I click on a pdf link it does nothing.

    to try that just click on the first or second link in the google search.

    nor shouldOverrideUrlLoading or onDownloadStart

    any idea on how to show or how to intercept it?

        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");
                    },
                 );
              }),
            );
          }
        }
    
    • Rahul
      Rahul over 2 years
      You need to do this first WebView.shouldOverrideUrlLoading = true just before runApp is called in main.dart pub.dev/documentation/flutter_inappwebview/latest/…
    • jack_the_beast
      jack_the_beast over 2 years
      @Rahul that property is not defined for the class WebView, besides is already set to true in cross platform options and it works for normal links, but not for pdfs