flutter_hyperswitch 0.0.3
flutter_hyperswitch: ^0.0.3 copied to clipboard
Hyperswitch SDK for Flutter.
example/lib/main.dart
import 'dart:convert';
import 'dart:async';
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter_hyperswitch/flutter_hyperswitch.dart';
import 'package:http/http.dart' as http;
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
final String _endpoint = Platform.isAndroid ? "http://10.0.2.2:5252" : "http://localhost:5252";
final _flutterHyperswitchPlugin = FlutterHyperswitch();
bool isButtonEnabled = false;
String _response = '';
@override
void initState() {
super.initState();
initPlatformState();
}
Future<void> initPlatformState() async {
try {
var response = await http.get(Uri.parse("$_endpoint/create-payment-intent"));
if (response.statusCode == 200) {
print(response.body);
Map<String, dynamic> responseBody = jsonDecode(response.body);
Map<String, dynamic> initPaymentSheetResponse = await _flutterHyperswitchPlugin
.initPaymentSheet(PaymentSheetParams(publishableKey: responseBody['publishableKey'], clientSecret: responseBody['clientSecret'])) ??
{};
if (initPaymentSheetResponse["type"] == "success") {
setState(() {
isButtonEnabled = true;
});
}
} else {
_response = "API Call Failed";
}
} catch (error) {
_response = "API Call Failed";
}
}
Future<void> onPress() async {
Map<String, dynamic> presentPaymentSheetResponse = await _flutterHyperswitchPlugin.presentPaymentSheet() ?? {};
print(presentPaymentSheetResponse);
if (!mounted) return;
setState(() {
_response = presentPaymentSheetResponse["message"];
if (presentPaymentSheetResponse["type"] != "cancelled") {
isButtonEnabled = false;
initPlatformState();
}
});
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Plugin example app'),
),
body: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Center(
child: ElevatedButton(onPressed: isButtonEnabled ? onPress : null, child: Text(isButtonEnabled ? "Open Payment Sheet" : "Loading ...")),
),
const SizedBox(height: 20),
Center(
child: Text(_response),
),
],
),
),
);
}
}