0

I am trying to build webview in my mobile application

In the webview when click on search all the records to particular time period are shown it works but when click on Export To Excel button it should download the excel sheet but nothing works in my application..

Below is the code what I have done

Thanks for help!![enter image description here][1]

PS: URL hidden for security reasons..

`

   Stack(
       children: [
      Container(
          padding: EdgeInsets.all(10.0),
          child: progress < 1.0
              ? LinearProgressIndicator(
                  value: progress,
                  backgroundColor: Colors.amber,
                )
              : Container()),
      WebView(
        initialUrl:
            "URL",
        javascriptMode: JavascriptMode.unrestricted,
        gestureNavigationEnabled: true,
        onWebViewCreated: (WebViewController controller) {
          _webViewController = controller;
        },
        onProgress: (int progress) {
          setState(() {
            this.progress = progress / 100;
          });
        },
        onPageFinished: (finish) {
          setState(
            () {
              isLoading = false;
            },
          );
        },
      ),
    ],
  ),
Nikhil Patil
  • 11
  • 3
  • 8
  • Possible duplicate of https://stackoverflow.com/questions/57937664/flutter-how-to-download-files-in-webview and https://stackoverflow.com/questions/56247542/how-to-download-create-pdf-through-webview-in-flutter – Lorenzo Pichilli Apr 02 '21 at 14:54
  • Tried those methods but still not working.. In my application when clicked on button, `webview` is opened and in the same website there's option to `download excel file` how can i achieve the same? – Nikhil Patil Apr 03 '21 at 05:32

1 Answers1

1

Use navigationDelegate parameter in Webview constructor.

WebView(
  initialUrl: 'https://google.com',
  navigationDelegate: (action) {
    if (action.url.contains('mail.google.com')) {
      print('Trying to open Gmail');
      Navigator.pop(context); // Close current window
      return NavigationDecision.prevent; // Prevent opening url
    } else if (action.url.contains('youtube.com')) {
      print('Trying to open Youtube');
      return NavigationDecision.navigate; // Allow opening url
    } else {
      return NavigationDecision.navigate; // Default decision
    }
  },
),

That's it. Enjoy coding!

Hardik Hirpara
  • 2,594
  • 20
  • 34