biometric_auth_library 0.0.1
biometric_auth_library: ^0.0.1 copied to clipboard
A Flutter plugin for seamless biometric authentication using fingerprint or Face ID. Simplify user authentication in your applications.
import 'package:flutter/material.dart';
import 'package:biometric_auth_library/biometric_auth_library.dart';
import 'package:local_auth/local_auth.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Exemple Auth Biométrique',
theme: ThemeData(
primarySwatch: Colors.blue,
useMaterial3: true,
),
home: const AuthHomePage(),
);
}
}
class AuthHomePage extends StatefulWidget {
const AuthHomePage({super.key});
@override
State<AuthHomePage> createState() => _AuthHomePageState();
}
class _AuthHomePageState extends State<AuthHomePage> {
final BiometricAuthService _authService = BiometricAuthService();
bool _canCheckBiometrics = false;
List<BiometricType> _availableBiometrics = [];
String _authStatus = '';
@override
void initState() {
super.initState();
// Vérifie la disponibilité de la biométrie au démarrage de l'écran
_checkBiometricsStatus();
}
Future<void> _checkBiometricsStatus() async {
// Vérifie si la biométrie est physiquement disponible sur l'appareil
bool canCheck = await _authService.canAuthenticate();
List<BiometricType> availableTypes = [];
if (canCheck) {
// Si disponible, récupère les types spécifiques (face, empreinte)
availableTypes = await _authService.getAvailableBiometrics();
}
// Met à jour l'état de l'UI si le widget est encore monté
if (mounted) {
setState(() {
_canCheckBiometrics = canCheck;
_availableBiometrics = availableTypes;
});
}
}
Future<void> _authenticate() async {
setState(() {
_authStatus = 'Authentification en cours...';
});
// Tente d'authentifier l'utilisateur
final authenticated = await _authService.authenticate(
reason: 'Veuillez scanner votre empreinte digitale ou votre visage pour vous connecter.',
);
// Met à jour l'état de l'UI avec le résultat de l'authentification
if (mounted) {
setState(() {
if (authenticated) {
_authStatus = 'Authentification réussie ! Bienvenue.';
// Ici, vous pouvez naviguer vers un autre écran,
// afficher un message de succès, ou effectuer une action.
} else {
_authStatus = 'Authentification échouée ou annulée.';
}
});
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Test Authentification Biométrique'),
),
body: Padding(
padding: const EdgeInsets.all(20.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Text(
'Statut Biométrique :',
style: Theme.of(context).textTheme.headlineSmall,
textAlign: TextAlign.center,
),
const SizedBox(height: 10),
Text(
// Affiche si la biométrie est disponible
_canCheckBiometrics
? 'Biométrie disponible.'
: 'Biométrie non disponible ou non configurée sur cet appareil.',
textAlign: TextAlign.center,
style: TextStyle(
color: _canCheckBiometrics ? Colors.black : Colors.orange[700],
fontWeight: FontWeight.bold,
),
),
if (_canCheckBiometrics)
Padding(
padding: const EdgeInsets.only(top: 8.0),
child: Text(
// Affiche les types de biométrie disponibles
'Types disponibles : ${_availableBiometrics.map((type) => type.toString().split('.').last).join(', ')}',
textAlign: TextAlign.center,
style: const TextStyle(fontStyle: FontStyle.italic),
),
),
const SizedBox(height: 30),
ElevatedButton.icon(
// Le bouton est activé uniquement si la biométrie est disponible
onPressed: _canCheckBiometrics ? _authenticate : null,
icon: const Icon(Icons.fingerprint),
label: const Text('Authentifier'),
style: ElevatedButton.styleFrom(
padding: const EdgeInsets.symmetric(vertical: 15),
textStyle: const TextStyle(fontSize: 18),
),
),
const SizedBox(height: 20),
Text(
_authStatus,
textAlign: TextAlign.center,
style: TextStyle(
color: _authStatus.contains('réussie') ? Colors.green : Colors.red,
fontWeight: FontWeight.bold,
),
),
],
),
),
);
}
}