welloo_sdk 0.0.46
welloo_sdk: ^0.0.46 copied to clipboard
Package de transaction Welloo
example/lib/main.dart
import 'package:flutter/material.dart';
import 'package:flutter_dotenv/flutter_dotenv.dart';
import 'package:welloo_sdk/welloo_sdk.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
// Load environment variables
try {
await dotenv.load(fileName: '.env');
} catch (e) {
debugPrint('⚠️ .env file not found. Please create one from .env.example');
}
// Initialize Welloo SDK
await WellooSdk.init();
runApp(const MainApp());
}
class MainApp extends StatelessWidget {
const MainApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Welloo SDK',
theme: ThemeData(colorSchemeSeed: Colors.blue, useMaterial3: true),
home: const DemoWello(),
);
}
}
class DemoWello extends StatefulWidget {
const DemoWello({super.key});
@override
State<DemoWello> createState() => _DemoWelloState();
}
class _DemoWelloState extends State<DemoWello> {
String? _lastTransactionRef;
final _sdk = WellooSdk();
String get _accessToken => dotenv.env['ACCESS_TOKEN'] ?? '';
String get _refreshToken => dotenv.env['REFRESH_TOKEN'] ?? '';
@override
void initState() {
super.initState();
}
Future<void> _handleDeposit() async {
// Check if tokens are configured
if (_accessToken.isEmpty || _refreshToken.isEmpty) {
if (!mounted) return;
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text(
'Please configure ACCESS_TOKEN and REFRESH_TOKEN in .env file',
),
backgroundColor: Colors.red,
duration: Duration(seconds: 3),
),
);
return;
}
Navigator.of(context).push<TransactionResult>(
MaterialPageRoute(
builder: (_) => WellooDeposit(
accessToken: _accessToken,
refreshToken: _refreshToken,
waitResponse: (response) {
debugPrint('📦 Response: $response');
// Save transaction reference for manual check if needed
if (response['reference_transaction'] != null) {
setState(() {
_lastTransactionRef = response['reference_transaction'];
});
}
},
onError: (error) {
debugPrint('❌ Error: $error');
},
),
),
);
}
/// Manual transaction status check
/// Useful when deep links don't work or for verification
Future<void> _checkTransactionStatus() async {
if (_lastTransactionRef == null) {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('No transaction to check')),
);
return;
}
try {
final result = await _sdk.checkDepositStatus(
reference: _lastTransactionRef!,
);
if (!mounted) return;
if (result.isSuccess && result.data != null) {
final transaction = result.data!;
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text(
'Status: ${transaction.status}\nReference: ${transaction.reference}',
),
backgroundColor:
transaction.status == 'success' ? Colors.green : Colors.orange,
),
);
} else {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text('Error: ${result.error ?? "Unknown error"}'),
backgroundColor: Colors.red,
),
);
}
} catch (e) {
if (!mounted) return;
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text('Error: $e')),
);
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Welloo SDK Demo')),
backgroundColor: Colors.white,
body: Center(
child: Padding(
padding: const EdgeInsets.all(20.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
ElevatedButton.icon(
onPressed: _handleDeposit,
icon: const Icon(Icons.account_balance_wallet),
label: const Text("Initiate Deposit"),
style: ElevatedButton.styleFrom(
minimumSize: const Size(250, 50),
backgroundColor: Colors.blue,
foregroundColor: Colors.white,
),
),
const SizedBox(height: 20),
if (_lastTransactionRef != null) ...[
Text(
'Last transaction: $_lastTransactionRef',
style: TextStyle(
fontSize: 12,
color: Colors.grey[600],
),
textAlign: TextAlign.center,
),
const SizedBox(height: 10),
ElevatedButton.icon(
onPressed: _checkTransactionStatus,
icon: const Icon(Icons.refresh),
label: const Text("Check Transaction Status"),
style: ElevatedButton.styleFrom(
minimumSize: const Size(250, 50),
backgroundColor: Colors.orange,
foregroundColor: Colors.white,
),
),
const SizedBox(height: 10),
Text(
'Use this button if deep link didn\'t work\nor to manually verify status',
style: TextStyle(
fontSize: 11,
color: Colors.grey[500],
fontStyle: FontStyle.italic,
),
textAlign: TextAlign.center,
),
],
],
),
),
),
);
}
}