Issue
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 browser 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 Browser 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?
Edit - Adding my tried code for Webview_Flutter Widget
class _WebViewPageState extends State<WebViewPage> {
@override
Widget build(BuildContext context) {
return WebView(
initialUrl: paymentiframe,
javascriptMode: JavascriptMode.unrestricted,
navigationDelegate: (NavigationRequest request) {
print("Navigation is detected");
if (request.url.startsWith('https://valleysys.net')) {
print('blocking navigation to $request}');
}
return NavigationDecision.prevent;
},
onPageStarted: (String url) {
print("Started $url");
},
);
}
Problem: Navigation Delegate never gets called, OnPageStarted only get called once when the Iframe is opened, but doesn't sense any navigation happening.
Code for Flutter_webview_plugin
@override
void initState() {
final flutterWebviewPlugin = new FlutterWebviewPlugin();
flutterWebviewPlugin.launch(paymentiframe);
flutterWebviewPlugin.onUrlChanged.listen((String url) {
print("Url Change is Detected");
print(url);
if (url == "https://valleysys.net") {
print("Navigation to return URL");
Navigator.of(context).pop();
}
});
super.initState();
}
same problem is here, it detects the url only once when the first Iframe is opened, but doesn't get called again when the url is changed.
Solution
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
Answered By - Kaushik Chandru
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.