import 'package:flutter/material.dart';
import 'package:didit_sdk/sdk_flutter.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Didit SDK Example',
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: const Color(0xFF1A1A1A)),
useMaterial3: true,
),
home: const VerificationScreen(),
);
}
}
class VerificationScreen extends StatefulWidget {
const VerificationScreen({super.key});
@override
State<VerificationScreen> createState() => _VerificationScreenState();
}
class _VerificationScreenState extends State<VerificationScreen> {
final _tokenController = TextEditingController();
bool _loading = false;
@override
void dispose() {
_tokenController.dispose();
super.dispose();
}
Future<void> _startVerification() async {
final token = _tokenController.text.trim();
if (token.isEmpty) {
_showAlert('Error', 'Please enter a session token.');
return;
}
setState(() => _loading = true);
try {
final result = await DiditSdk.startVerification(
token,
config: const DiditConfig(loggingEnabled: true),
);
switch (result) {
case VerificationCompleted(:final session):
_showAlert(
'Verification Complete',
'Status: ${session.status.name}\nSession: ${session.sessionId}',
);
case VerificationCancelled():
_showAlert('Cancelled', 'The user cancelled the verification.');
case VerificationFailed(:final error):
_showAlert('Failed', '${error.type.name}: ${error.message}');
}
} catch (e) {
_showAlert('Error', 'Unexpected error: $e');
} finally {
setState(() => _loading = false);
}
}
void _showAlert(String title, String message) {
showDialog(
context: context,
builder: (ctx) => AlertDialog(
title: Text(title),
content: Text(message),
actions: [
TextButton(
onPressed: () => Navigator.pop(ctx),
child: const Text('OK'),
),
],
),
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child: Padding(
padding: const EdgeInsets.all(24),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
TextField(
controller: _tokenController,
decoration: InputDecoration(
hintText: 'Enter session token...',
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(8),
),
),
autocorrect: false,
),
const SizedBox(height: 16),
ElevatedButton(
onPressed: _loading ? null : _startVerification,
style: ElevatedButton.styleFrom(
backgroundColor: const Color(0xFF1A1A1A),
foregroundColor: Colors.white,
padding: const EdgeInsets.symmetric(vertical: 16),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(8),
),
),
child: _loading
? const SizedBox(
height: 20,
width: 20,
child: CircularProgressIndicator(
strokeWidth: 2,
color: Colors.white,
),
)
: const Text(
'Start Verification',
style: TextStyle(fontWeight: FontWeight.w600),
),
),
],
),
),
),
);
}
}