Home > Enterprise >  Flutter : Replace LaunchUrl with WebView
Flutter : Replace LaunchUrl with WebView

Time:02-05

Within my flutter app, I am launching a payment gateway using the url_launcher plugin with opens the page in a browser. However I would rather have it open in a WebView plugin instead, within the app.

Do I need to use both? how can implement such.

Please assist

Url launcher

if (selectedPaymentMethod == 'Pay Now' &&
                                      _formKey.currentState!.validate()) {
                                    () async {
                                      final ozowApiRes = await OzowApiProvider()
                                          .createPaymentRequest(
                                        'R${(cart.totalAmount   cart.serviceFee   value * cart.newCylinderPrice).toStringAsFixed(0)}',
                                        userData?['username'],
                                        userData?['phoneNumber'],
                                        userData?['email'],
                                      );
                                      () async {
                                        try {
                                          await launchUrl(
                                            Uri.parse(ozowApiRes.data),
                                          );
                                        } catch (ex) {
                                          throw 'Could not launch $ozowApiRes';
                                        }
                                      }();
                                    }();

CodePudding user response:

To do this, you can use url_launcher or flutter_inappwebview. I will recommend you to use flutter_inappwebview if possible cause url_launcher 100% not guaranteed you to launch in inAppWebView and also flutter_inappwebview gives you the granular controll in a page.

(1) you can use url_launcher with mode LaunchMode.inAppWebView

await launchUrl(
  Uri.parse("https://www.google.com/"),
  mode: LaunchMode.inAppWebView,
);

(2) or you can use a stateless widget page with flutter_inappwebview and just pass the purchase url

import 'package:flutter/material.dart';
import 'package:flutter_inappwebview/flutter_inappwebview.dart';

class InAppPurchasePage extends StatelessWidget {
  const InAppPurchasePage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("Purchase Page"),
      ),
      body: InAppWebView(
        initialUrlRequest: URLRequest(
          url: Uri.parse(
            "https://flutter.dev",
          ),
        ),
      ),
    );
  }
}
  • Related