Home > database >  How to do an action when flutter app lunch url close
How to do an action when flutter app lunch url close

Time:08-22

I am building a flutter app that integrates with payment gateway through API when I do HTTP Post request to the API, the gateway reply with Iframe (URL), where the user should use to enter his credit card details and complete payment.

I am using Lunch_url to open the Iframe recevied like this.

 Future CompetePaymntProcess(
   String paymentid, Map<String, dynamic> transactionresult) async {
   String iframe = transactionresult["data"]["iframe_url"];
   await launchUrlString(iframe);
}

now I would like to do some actions based on the result of the user payment when he closes the web broswer opened by LaunchURLString.

these actions are to send one more request to the API to get te payment result and do update my records.

how can I trigger an action when this web brosed is closed?

I did some research and found on Flutter web there is the below code:

 html.window.onBeforeUnload.listen((event) async {}

is there anything similar in flutter app? or what is the best practice in my case?

Thanks

CodePudding user response:

You can use webview which has a navigation delegate. Once the user enters the details navigate the user to another page which webview can capture

WebView(
        initialUrl: 'https://flutter.dev',
        javascriptMode: JavascriptMode.unrestricted,
        navigationDelegate: (NavigationRequest request) {
          if (request.url.startsWith('https://www.youtube.com/')) {
            print('blocking navigation to $request}');
//You can add your logic here
            return NavigationDecision.prevent;

          }
          
        },
      ),

https://pub.dev/packages/webview_flutter/example

  • Related