saafe_aa_sdk 0.1.3
saafe_aa_sdk: ^0.1.3 copied to clipboard
Flutter SDK for integrating SAAFE's secure authentication and financial experience
example/lib/main.dart
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';
import 'dart:io' show Platform;
import 'package:saafe_aa_sdk/saafe_sdk.dart';
import 'package:http/http.dart' as http;
void main() {
// Initialize SAAFE SDK
SaafeSdk.initialize();
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'SAAFE SDK Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const SaafeDemo(),
);
}
}
class SaafeDemo extends StatefulWidget {
const SaafeDemo({Key? key}) : super(key: key);
@override
State<SaafeDemo> createState() => SaafeDemoState();
}
class SaafeDemoState extends State<SaafeDemo> {
String fi = '';
String reqdate = '';
String ecreq = '';
final TextEditingController textEditingController = TextEditingController();
final FocusNode focusNode = FocusNode();
SaafeRedirectInstance? _redirectInstance;
String _status = 'Not started';
bool _isSupported = true;
@override
void initState() {
super.initState();
_checkPlatformSupport();
fetchAndExtractData();
}
void _checkPlatformSupport() {
setState(() {
_isSupported = !(kIsWeb || !(Platform.isAndroid || Platform.isIOS));
if (!_isSupported) {
_status = 'Platform not supported';
}
});
}
bool get isTenDigits => textEditingController.text.length == 10;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('SAAFE SDK Demo'),
),
body: Center(
child: SingleChildScrollView(
padding: const EdgeInsets.all(16),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'Status: $_status',
style: const TextStyle(fontSize: 18),
),
const SizedBox(height: 30),
if (!_isSupported)
const Text(
'This platform does not support WebView. SAAFE SDK requires Android or iOS.',
textAlign: TextAlign.center,
style: TextStyle(color: Colors.red, fontSize: 16),
)
else ...[
TextFormField(
controller: textEditingController,
focusNode: focusNode,
keyboardType: TextInputType.number,
maxLength: 10,
inputFormatters: [
FilteringTextInputFormatter.digitsOnly,
LengthLimitingTextInputFormatter(10),
],
decoration: InputDecoration(
labelText: 'Mobile Number',
hintText: 'Enter 10-digit mobile number',
prefixIcon: const Icon(Icons.phone),
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(12),
),
errorText: (textEditingController.text.isEmpty ||
isTenDigits)
? null
: 'Please enter exactly 10 digits',
),
onChanged: (_) {
setState(() {});
},
),
const SizedBox(height: 20),
ElevatedButton(
onPressed: isTenDigits ? _startSaafeFlow : null,
style: ElevatedButton.styleFrom(
backgroundColor: Colors.blueAccent,
minimumSize: const Size(200, 50),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(12),
),
),
child: const Text('Start SAAFE Flow',style: TextStyle(color: Colors.white),),
),
if (_redirectInstance != null)
Padding(
padding: const EdgeInsets.only(top: 16.0),
child: ElevatedButton(
onPressed: _closeFlow,
style: ElevatedButton.styleFrom(
backgroundColor: Colors.redAccent,
minimumSize: const Size(200, 50),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(12),
),
),
child: const Text('Close Flow',style: TextStyle(color: Colors.white),),
),
),
],
],
),
),
),
);
}
Future<void> fetchAndExtractData() async {
const apiUrl = 'https://api.sandbox.saafe.tech/bank-data/generate_consent';
try {
final response = await http.post(
Uri.parse(apiUrl),
body: {
"client_ref_num": "test123",
"txn_completed_cburl": "https://www.evil.com",
"mobile_num": "7010146301",
"aa_vendor": "dashboard-aa-preprod",
"fipId": "ACME-FIP,setu-fip-2",
"institutionId": "93"
},
);
if (response.statusCode == 200) {
final jsonResponse = jsonDecode(response.body);
setState(() {
fi = jsonResponse['fi'] ?? '';
reqdate = jsonResponse['reqdate'] ?? '';
ecreq = jsonResponse['ecreq'] ?? '';
});
print('Extracted values:\nfi: $fi\nreqdate: $reqdate\necreq: $ecreq');
} else {
print('Failed to fetch data. Status code: ${response.statusCode}');
}
} catch (e) {
print('Error fetching data: $e');
}
}
Future<void> _startSaafeFlow() async {
setState(() {
_status = 'Starting...';
});
final options = SaafeRedirectOptions(
fi: fi,
reqdate: reqdate,
ecreq: ecreq,
onLoad: () {
setState(() {
_status = 'Flow loaded';
});
},
onComplete: (data) {
setState(() {
_status = 'Completed: $data';
_redirectInstance = null;
});
},
onCancel: () {
setState(() {
_status = 'Cancelled by user';
_redirectInstance = null;
});
},
onError: (error) {
setState(() {
_status = 'Error: $error';
_redirectInstance = null;
});
},
);
_redirectInstance = await SaafeSdk.triggerRedirect(context, options);
}
void _closeFlow() {
_redirectInstance?.close();
setState(() {
_status = 'Closed by app';
_redirectInstance = null;
});
}
}