blusalt_checkout 1.2.1
blusalt_checkout: ^1.2.1 copied to clipboard
Checkout SDK for Blusalt Payment
example/lib/main.dart
import 'dart:io';
import 'dart:async';
import 'dart:convert';
import 'package:blusalt_checkout/enums.dart';
import 'package:blusalt_checkout/blusalt_checkout.dart';
import 'package:blusalt_checkout/model.dart';
import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';
import 'package:blusalt_checkout_example/json_viewer_widget.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
final _checkoutPlugin = BlusaltCheckout();
File? imageUri;
TextEditingController apiKeyCC = TextEditingController(text: '');
TextEditingController amountCC = TextEditingController(text: '');
TextEditingController currencyCC = TextEditingController(text: '');
TextEditingController walletIdCC = TextEditingController(text: '');
TextEditingController companyEmail = TextEditingController();
TextEditingController referenceController = TextEditingController();
TextEditingController metaDataController = TextEditingController();
Color? selectedColor;
bool isDev = false;
bool isPaymentPro = true; // See your dashboard for more detail
final ImagePicker _picker = ImagePicker();
BlusaltCheckoutResultResponse? resultResponse;
@override
void dispose() {
super.dispose();
apiKeyCC.dispose();
amountCC.dispose();
currencyCC.dispose();
walletIdCC.dispose();
companyEmail.dispose();
referenceController.dispose();
metaDataController.dispose();
}
pickCompanyLogo() async {
if (imageUri != null) {
setState(() {
imageUri = null;
});
return;
}
final XFile? image = await _picker.pickImage(source: ImageSource.gallery);
if (image != null) {
setState(() {
imageUri = File(image.path);
});
}
}
Future<BlusaltCheckoutResultResponse?> startCheckout() async {
// Parse metaData if provided
Map<String, dynamic>? metaData;
if (metaDataController.text.isNotEmpty) {
try {
metaData = Map<String, dynamic>.from(
json.decode(metaDataController.text) as Map
);
} catch (e) {
debugPrint('Invalid JSON in metaData: $e');
metaData = null;
}
}
// Convert Color to hex string (format: #RRGGBB)
String? primaryColorHex;
if (selectedColor != null) {
final r = (selectedColor!.r * 255.0).round().clamp(0, 255);
final g = (selectedColor!.g * 255.0).round().clamp(0, 255);
final b =(selectedColor!.b * 255.0).round().clamp(0, 255);
primaryColorHex =
'#${r.toRadixString(16).padLeft(2, '0')}${g.toRadixString(16).padLeft(2, '0')}${b.toRadixString(16).padLeft(2, '0')}'
.toUpperCase();
}
resultResponse = await _checkoutPlugin.startCheckoutSDK(
apiKey: apiKeyCC.text,
isDev: isDev,
isPayment: isPaymentPro,
amount: amountCC.text.isNotEmpty ? double.parse(amountCC.text) : 0,
currency: currencyCC.text,
walletId: walletIdCC.text,
companyEmail: companyEmail.text.isEmpty ? null : companyEmail.text,
reference: referenceController.text,
companyLogo: imageUri != null ? (await imageUri?.readAsBytes()) : null,
metaData: metaData,
primaryColorHex: primaryColorHex,
iosConfig: IosConfig(
iosPresentationStyle: IosPresentationStyle.shrinkBottomModal,
),
);
if (resultResponse?.blusaltCheckoutProcess ==
BlusaltCheckoutProcess.completed) {
setState(() {});
} else {
debugPrint(resultResponse?.code ?? '');
debugPrint(resultResponse?.message ?? '');
}
return resultResponse;
}
double? validateNumber(String value) {
if (value.isEmpty) {
return null;
}
return double.tryParse(value);
}
int? validateDurationNumber(String value) {
if (value.isEmpty) {
return null;
}
return int.tryParse(value);
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(title: const Text('Plugin example app')),
body: SingleChildScrollView(
padding: const EdgeInsets.all(16),
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
if (imageUri != null)
Image.file(imageUri!, width: 100, height: 100)
else
const Padding(
padding: EdgeInsets.only(top: 20),
child: Text("No image selected"),
),
const SizedBox(height: 14),
TextField(
controller: apiKeyCC,
decoration: const InputDecoration(
border: OutlineInputBorder(),
hintText: "Enter api key",
hintStyle: TextStyle(color: Color(0xFFCCCCCC)),
),
onChanged: (text) => setState(() {}),
),
const SizedBox(height: 14),
TextField(
controller: amountCC,
decoration: const InputDecoration(
border: OutlineInputBorder(),
hintText: "Enter amount",
hintStyle: TextStyle(color: Color(0xFFCCCCCC)),
),
onChanged: (text) => setState(() {}),
),
const SizedBox(height: 14),
TextField(
controller: currencyCC,
decoration: const InputDecoration(
border: OutlineInputBorder(),
hintText: "Enter currency",
hintStyle: TextStyle(color: Color(0xFFCCCCCC)),
),
onChanged: (text) => setState(() {}),
),
const SizedBox(height: 14),
TextField(
controller: walletIdCC,
decoration: const InputDecoration(
border: OutlineInputBorder(),
hintText: "Enter wallet Id",
hintStyle: TextStyle(color: Color(0xFFCCCCCC)),
),
onChanged: (text) => setState(() {}),
),
const SizedBox(height: 14),
TextField(
controller: referenceController,
decoration: const InputDecoration(
border: OutlineInputBorder(),
hintText: "Enter reference (Optional)",
hintStyle: TextStyle(color: Color(0xFFCCCCCC)),
),
onChanged: (text) => setState(() {}),
),
const SizedBox(height: 14),
TextField(
controller: companyEmail,
decoration: const InputDecoration(
border: OutlineInputBorder(),
hintText: "Enter company email",
hintStyle: TextStyle(color: Color(0xFFCCCCCC)),
),
onChanged: (text) => setState(() {}),
),
const SizedBox(height: 14),
// Color Picker Section
const Text(
"Primary Color (Optional):",
style: TextStyle(fontWeight: FontWeight.bold),
),
const SizedBox(height: 8),
Row(
children: [
// Color preview
Container(
width: 60,
height: 60,
decoration: BoxDecoration(
color: selectedColor ?? Colors.grey[300],
border: Border.all(color: Colors.grey, width: 2),
borderRadius: BorderRadius.circular(8),
),
child: selectedColor == null
? const Center(
child: Text(
"No Color",
style: TextStyle(fontSize: 10, color: Colors.grey),
),
)
: null,
),
const SizedBox(width: 12),
// Color picker buttons
Expanded(
child: Column(
children: [
if (selectedColor != null)
ElevatedButton(
onPressed: () => setState(() => selectedColor = null),
style: ElevatedButton.styleFrom(
backgroundColor: Colors.red,
minimumSize: const Size(double.infinity, 40),
),
child: const Text(
"Clear Color",
style: TextStyle(color: Colors.white, fontSize: 12),
),
),
],
),
),
],
),
const SizedBox(height: 8),
// Preset colors
const Text(
"Quick Colors:",
style: TextStyle(fontWeight: FontWeight.bold),
),
const SizedBox(height: 8),
Wrap(
spacing: 8,
runSpacing: 8,
children: [
_ColorButton(
color: Colors.blue,
onTap: () => setState(() => selectedColor = Colors.blue),
),
_ColorButton(
color: Colors.green,
onTap: () => setState(() => selectedColor = Colors.green),
),
_ColorButton(
color: Colors.orange,
onTap: () => setState(() => selectedColor = Colors.orange),
),
_ColorButton(
color: Colors.purple,
onTap: () => setState(() => selectedColor = Colors.purple),
),
_ColorButton(
color: Colors.red,
onTap: () => setState(() => selectedColor = Colors.red),
),
_ColorButton(
color: Colors.cyan,
onTap: () => setState(() => selectedColor = Colors.cyan),
),
],
),
const SizedBox(height: 14),
// MetaData field
TextField(
controller: metaDataController,
decoration: const InputDecoration(
border: OutlineInputBorder(),
hintText: "Enter metadata (JSON) - Optional",
hintStyle: TextStyle(color: Color(0xFFCCCCCC)),
),
maxLines: 3,
onChanged: (text) => setState(() {}),
),
const SizedBox(height: 4),
CustomCheckbox(
label: "Point to Development Environment?",
value: isDev,
onCheck: (value) => setState(() => isDev = value),
),
const SizedBox(height: 2),
CustomCheckbox(
label: "Is Payment Pro",
value: isPaymentPro,
onCheck: (value) => setState(() => isPaymentPro = value),
),
const SizedBox(height: 24),
Row(
children: [
Expanded(
child: ElevatedButton(
onPressed: pickCompanyLogo,
style: ElevatedButton.styleFrom(
backgroundColor: Colors.blue,
minimumSize: const Size(double.infinity, 50),
),
child: Text(
imageUri != null
? 'Clear company logo'
: "Pick company logo",
textAlign: TextAlign.center,
style: TextStyle(color: Colors.white),
),
),
),
const SizedBox(width: 16),
Expanded(
child: ElevatedButton(
onPressed: startCheckout,
style: ElevatedButton.styleFrom(
backgroundColor: Colors.blue,
minimumSize: const Size(double.infinity, 50),
),
child: const Text(
"Start Checkout SDK",
textAlign: TextAlign.center,
style: TextStyle(color: Colors.white),
),
),
),
],
),
if (resultResponse?.blusaltCheckoutProcess ==
BlusaltCheckoutProcess.completed)
const SizedBox(height: 34),
if (resultResponse?.blusaltCheckoutProcess ==
BlusaltCheckoutProcess.completed)
Text(
"Result",
textAlign: TextAlign.center,
style: const TextStyle(fontWeight: FontWeight.w500),
),
if (resultResponse?.blusaltCheckoutProcess ==
BlusaltCheckoutProcess.completed)
const SizedBox(height: 4),
if (resultResponse?.blusaltCheckoutProcess ==
BlusaltCheckoutProcess.completed)
JsonViewerWidget(jsonData: resultResponse?.toJson()),
const SizedBox(height: 34),
Image.asset(
'assets/images/blusalt_logo.png',
width: 100,
height: 100,
color: Colors.black,
),
const SizedBox(height: 24),
],
),
),
),
);
}
}
class CustomCheckbox extends StatelessWidget {
final String label;
final bool value;
final Function(bool) onCheck;
const CustomCheckbox({
Key? key,
required this.label,
required this.value,
required this.onCheck,
}) : super(key: key);
@override
Widget build(BuildContext context) {
return Row(
children: [
Checkbox(
materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
value: value,
onChanged: (newValue) => onCheck(newValue ?? false),
),
Expanded(
child: Text(
label,
style: const TextStyle(fontWeight: FontWeight.w500),
),
),
],
);
}
}
class _ColorButton extends StatelessWidget {
final Color color;
final VoidCallback onTap;
const _ColorButton({
required this.color,
required this.onTap,
});
@override
Widget build(BuildContext context) {
return GestureDetector(
onTap: onTap,
child: Container(
width: 40,
height: 40,
decoration: BoxDecoration(
color: color,
border: Border.all(color: Colors.grey, width: 1),
borderRadius: BorderRadius.circular(4),
),
),
);
}
}