edviron_kit 0.0.3 copy "edviron_kit: ^0.0.3" to clipboard
edviron_kit: ^0.0.3 copied to clipboard

Flutter SDK for Edviron payment integration.

example/lib/main.dart

import 'package:flutter/material.dart';
import 'package:edviron_kit/edviron_kit.dart';
import 'package:webview_flutter/webview_flutter.dart';
import 'package:url_launcher/url_launcher.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Edviron Collect Demo',
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
        useMaterial3: true,
      ),
      home: const MyHomePage(title: 'Collect Request Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key, required this.title});

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  String _collectRequestId = '';
  final Edviron _edviron = Edviron(mode: EdvironMode.production);

  void _showSuccessDialog() {
    showDialog(
      context: context,
      builder: (_) => AlertDialog(
        title: const Text("Success"),
        content: const Text("Collect request was successful!"),
        actions: [
          TextButton(
            onPressed: () => Navigator.pop(context),
            child: const Text("OK"),
          ),
        ],
      ),
    );
  }

  void _showErrorDialog() {
    showDialog(
      context: context,
      builder: (_) => AlertDialog(
        title: const Text("Failed"),
        content: const Text("Collect request failed."),
        actions: [
          TextButton(
            onPressed: () => Navigator.pop(context),
            child: const Text("Try Again"),
          ),
        ],
      ),
    );
  }

  void _triggerCollect() {
    _edviron.collect(
      context: context,
      collectRequestId: _collectRequestId,
      onWebUrl: (url) {
        Navigator.push(
          context,
          MaterialPageRoute(
            builder: (_) => CollectWebView(collectRequestUrl: url),
          ),
        );
      },
      onSuccess: _showSuccessDialog,
      onError: _showErrorDialog,
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Theme.of(context).colorScheme.inversePrimary,
        title: Text(widget.title),
      ),
      body: Padding(
        padding: const EdgeInsets.all(16.0),
        child: Column(
          children: [
            const Text("Enter the collect request ID:"),
            const SizedBox(height: 10),
            TextField(
              onChanged: (value) => _collectRequestId = value,
              decoration: const InputDecoration(
                labelText: "Collect Request ID",
                border: OutlineInputBorder(),
              ),
            ),
            const SizedBox(height: 20),
            ElevatedButton(
              onPressed: _triggerCollect,
              child: const Text("Start Collect"),
            ),
          ],
        ),
      ),
    );
  }
}

class CollectWebView extends StatelessWidget {
  final String collectRequestUrl;

  const CollectWebView({super.key, required this.collectRequestUrl});

  void _showCannotLaunchDialog(BuildContext context, String url) {
    showDialog(
      context: context,
      builder: (_) => AlertDialog(
        title: const Text("Cannot Launch URL"),
        content: Text("Could not launch: \n$url"),
        actions: [
          TextButton(
            onPressed: () => Navigator.pop(context),
            child: const Text("OK"),
          ),
        ],
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    final controller = WebViewController()
      ..setJavaScriptMode(JavaScriptMode.unrestricted)
      ..setNavigationDelegate(
        NavigationDelegate(
          onNavigationRequest: (request) async {
            final url = request.url;
            debugPrint('Navigation request: $url');
            final uri = Uri.parse(url);
            final scheme = uri.scheme.toLowerCase();
            // List of schemes to launch externally
            const externalSchemes = [
              'upi',
              'tez',
              'phonepe',
              'paytmmp',
              'intent',
            ];

            if (externalSchemes.contains(scheme) ||
                scheme == '' ||
                scheme == 'intent') {
              if (await canLaunchUrl(uri)) {
                await launchUrl(uri, mode: LaunchMode.externalApplication);
              } else {
                debugPrint("Cannot launch $url");
                _showCannotLaunchDialog(context, url);
              }
              return NavigationDecision.prevent;
            }

            // Optionally, handle all non-http(s) schemes externally
            if (scheme != 'http' && scheme != 'https') {
              if (await canLaunchUrl(uri)) {
                await launchUrl(uri, mode: LaunchMode.externalApplication);
              } else {
                debugPrint("Cannot launch $url");
                _showCannotLaunchDialog(context, url);
              }
              return NavigationDecision.prevent;
            }

            return NavigationDecision.navigate;
          },
        ),
      )
      ..loadRequest(Uri.parse(collectRequestUrl));

    return Scaffold(
      appBar: AppBar(title: const Text("Complete Payment")),
      body: WebViewWidget(controller: controller),
    );
  }
}
0
likes
120
points
44
downloads

Documentation

API reference

Publisher

unverified uploader

Weekly Downloads

Flutter SDK for Edviron payment integration.

Homepage
Repository (GitHub)
View/report issues

License

MIT (license)

Dependencies

flutter, url_launcher, webview_flutter

More

Packages that depend on edviron_kit