antifraud_sdk 1.1.0
antifraud_sdk: ^1.1.0 copied to clipboard
Flutter SDK for Antifraud.id integration.
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:antifraud_sdk/antifraud_sdk.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
theme: ThemeData.dark().copyWith(
scaffoldBackgroundColor: const Color(0xFF0F172A),
colorScheme: const ColorScheme.dark(
primary: Color(0xFF10B981),
secondary: Color(0xFF3B82F6),
surface: Color(0xFF1E293B),
),
),
home: const HomeScreen(),
);
}
}
class HomeScreen extends StatefulWidget {
const HomeScreen({super.key});
@override
State<HomeScreen> createState() => _HomeScreenState();
}
class _HomeScreenState extends State<HomeScreen> {
bool _initialized = false;
bool _loading = false;
String _statusMessage = 'SDK Not Initialized';
String? _sessionId;
Future<void> _initializeSDK() async {
setState(() {
_loading = true;
});
try {
final config = AntifraudConfig(
projectId: '45608b8c-ed39-4ce8-8607-81a4cd26deed',
publicKey: '''
-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA0wMhiEV9q92d860fYfQ6
NByd0mq3FNB3uKTTctFx25OVHd6YN3jiNbiLGDtPK8mbooSwMTMBBWKahcfN3otq
kSt3jUNA5DuNQlmPHrPmPvdH1Oq7OISALjgXYjk4e5FQI9LSE1HoJ0B86LnHsXsp
1exittk4uM9iiSo2I4Zn4u52nur8JwNbaXAsAivPht9gL9sb0Ul1VQZUkWHN3RdE
E3Lm4Nws4qk6zIp60OzV+UYMDzwN9tamCGo+pXFFIpKqWra31ncROuGB8zk5vweS
IJS5V2kF18SN9YLUY8raaOEo7i5ZWoEvwLPKPVjLpA5rLz4yzjPBfNKGyCIuCLed
xQIDAQAB
-----END PUBLIC KEY-----
''',
apiUrl: 'https://api.antifraud.id',
timeoutMs: 5000,
);
await Antifraud.initialize(config);
setState(() {
_initialized = true;
_statusMessage = 'SDK Initialized Successfully';
});
} on PlatformException catch (e) {
setState(() {
_statusMessage = 'Initialization Failed: ${e.message}';
});
} finally {
setState(() {
_loading = false;
});
}
}
Future<void> _createSession() async {
if (!_initialized) {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('Please initialize the SDK first.')),
);
return;
}
setState(() {
_loading = true;
_sessionId = null;
});
try {
final result = await Antifraud.createSession();
setState(() {
_sessionId = result.sessionId;
_statusMessage = 'Session Created Successfully';
});
} on PlatformException catch (e) {
setState(() {
_statusMessage = 'Failed to create session: ${e.message}';
});
} finally {
setState(() {
_loading = false;
});
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text(
'Antifraud SDK Demo',
style: TextStyle(fontWeight: FontWeight.bold, letterSpacing: 1.2),
),
backgroundColor: const Color(0xFF1E293B),
elevation: 0,
centerTitle: true,
),
body: SafeArea(
child: Padding(
padding: const EdgeInsets.all(24.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
const SizedBox(height: 20),
Card(
color: const Color(0xFF1E293B),
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(16)),
child: Padding(
padding: const EdgeInsets.all(20.0),
child: Column(
children: [
Icon(
_initialized ? Icons.check_circle : Icons.warning_amber_rounded,
color: _initialized ? const Color(0xFF10B981) : Colors.orangeAccent,
size: 64,
),
const SizedBox(height: 12),
Text(
_statusMessage,
style: const TextStyle(fontSize: 16, fontWeight: FontWeight.w600),
textAlign: TextAlign.center,
),
],
),
),
),
const SizedBox(height: 24),
if (!_initialized)
ElevatedButton(
onPressed: _loading ? null : _initializeSDK,
style: ElevatedButton.styleFrom(
backgroundColor: const Color(0xFF10B981),
foregroundColor: Colors.white,
padding: const EdgeInsets.symmetric(vertical: 16),
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(12)),
),
child: _loading
? const CircularProgressIndicator(color: Colors.white)
: const Text('Initialize SDK', style: TextStyle(fontSize: 16, fontWeight: FontWeight.bold)),
)
else
ElevatedButton(
onPressed: _loading ? null : _createSession,
style: ElevatedButton.styleFrom(
backgroundColor: const Color(0xFF3B82F6),
foregroundColor: Colors.white,
padding: const EdgeInsets.symmetric(vertical: 16),
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(12)),
),
child: _loading
? const CircularProgressIndicator(color: Colors.white)
: const Text('Generate Session ID', style: TextStyle(fontSize: 16, fontWeight: FontWeight.bold)),
),
const SizedBox(height: 24),
if (_sessionId != null)
Expanded(
child: Card(
color: const Color(0xFF1E293B),
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(16)),
child: Padding(
padding: const EdgeInsets.all(20.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
const Text(
'Session ID:',
style: TextStyle(fontSize: 14, color: Colors.grey, fontWeight: FontWeight.bold),
),
const SizedBox(height: 8),
SelectableText(
_sessionId!,
style: const TextStyle(
fontSize: 18,
fontFamily: 'Courier',
fontWeight: FontWeight.bold,
color: Color(0xFF10B981),
),
),
const Spacer(),
Center(
child: OutlinedButton.icon(
onPressed: () {
Clipboard.setData(ClipboardData(text: _sessionId!));
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('Session ID copied to clipboard')),
);
},
icon: const Icon(Icons.copy),
label: const Text('Copy Session ID'),
style: OutlinedButton.styleFrom(
foregroundColor: Colors.white,
side: const BorderSide(color: Colors.white54),
padding: const EdgeInsets.symmetric(horizontal: 24, vertical: 12),
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(12)),
),
),
),
],
),
),
),
),
],
),
),
),
);
}
}