edviron_kit 0.0.2
edviron_kit: ^0.0.2 copied to clipboard
Flutter SDK for Edviron payment integration.
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),
);
}
}