Venkata Subbarao
Venkata Subbarao

Reputation: 420

Closing Webview on loading specific URL in Flutter

Working on a Flutter App in which I am opening an external webpage using WebView. Is there a way to know when user another page from the initial page and close the webview even before loading it?

More specifically, this is for a Payment process, where the page to enter the payment details is opened using web view. The URL passed to WebView has options to redirect to different web pages depending on different status of the payment. I do not want to show the redirected page using web view. Instead, I want to close web view and show the status using Flutter widget.

Upvotes: 2

Views: 9300

Answers (2)

Venkata Subbarao
Venkata Subbarao

Reputation: 420

Followed the steps given in this link.

Here is the code.

// Add a listener to on url changed
_onUrlChanged =
    flutterWebviewPlugin.onUrlChanged.listen((String url) async {
  if (mounted) {
    //print("URL changed: $url");
    if (url.startsWith(widget.checkoutResult.returnUrl)) {
        // handle success case
    } else if (if (url.startsWith(widget.checkoutResult.cancelUrl)) {
        // Handle cancel case
    }
  }

Upvotes: 3

Adelina
Adelina

Reputation: 11881

  navigationDelegate: (NavigationRequest request) {
        if (request.url.startsWith('https://www.youtube.com/')) {
          print('blocking navigation to $request}'); # add your code of closing webView
          return NavigationDecision.prevent;
        }
        print('allowing navigation to $request');
        return NavigationDecision.navigate;
      },

Could also disable javascriptMode(JavascriptMode.disabled) this would't allow any redirects

Upvotes: 2

Related Questions