empreinte_faceid_groupe05 0.0.1+1
empreinte_faceid_groupe05: ^0.0.1+1 copied to clipboard
Un plugin Flutter pour l'authentification biométrique (empreinte digitale et reconnaissance faciale) sur Android.
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:flutter/services.dart';
import 'package:empreinte_faceid_groupe05/empreinte_faceid_groupe05.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
String _authStatus = 'Non authentifié';
bool _biometricAvailable = false;
Color _statusColor = Colors.grey; // Nouvelle variable pour la couleur du statut
@override
void initState() {
super.initState();
_checkBiometricAvailability();
}
// Vérifie si la biométrie est disponible sur l'appareil.
Future<void> _checkBiometricAvailability() async {
bool available;
String message;
Color color;
try {
available = await EmpreinteFaceidGroupe05.isBiometricAvailable();
if (available) {
message = 'Biométrie disponible sur cet appareil.';
color = Colors.green.shade700;
} else {
message = 'Biométrie NON disponible ou non configurée.';
color = Colors.red.shade700;
}
} on PlatformException catch (e) {
available = false;
message = "Erreur de vérification: ${e.message}";
color = Colors.orange.shade700;
print("Erreur lors de la vérification de la disponibilité: ${e.message}");
} catch (e) {
available = false;
message = "Erreur inattendue: $e";
color = Colors.orange.shade700;
print("Erreur inattendue: $e");
}
if (!mounted) return;
setState(() {
_biometricAvailable = available;
_authStatus = message; // Mettre à jour le statut initial
_statusColor = color; // Mettre à jour la couleur initiale
});
}
// Tente d'authentifier l'utilisateur.
Future<void> _authenticate() async {
String message;
Color color;
try {
final bool authenticated = await EmpreinteFaceidGroupe05.authenticate(
'Veuillez utiliser votre empreinte digitale ou votre visage.',
);
if (authenticated) {
message = 'Authentification réussie !';
color = Colors.green.shade700;
} else {
message = 'Authentification échouée.';
color = Colors.red.shade700;
}
} on PlatformException catch (e) {
if (e.code == 'BIOMETRIC_ERROR' || e.code == 'BIOMETRIC_UNAVAILABLE' || e.code == 'UNSUPPORTED_ANDROID_VERSION' || e.code == 'ACTIVITY_NOT_ATTACHED') {
message = "Erreur: ${e.message}";
color = Colors.orange.shade700;
} else {
message = "Erreur inattendue: ${e.message}";
color = Colors.deepOrange.shade700;
}
} catch (e) {
message = "Une erreur s'est produite: $e";
color = Colors.deepOrange.shade700;
}
if (!mounted) return;
setState(() {
_authStatus = message;
_statusColor = color;
});
}
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false, // Cache le bandeau "DEBUG"
home: Scaffold(
// Utilisation d'un dégradé pour l'arrière-plan
body: Container(
decoration: BoxDecoration(
gradient: LinearGradient(
colors: [Colors.blue.shade800, Colors.blue.shade400],
begin: Alignment.topLeft,
end: Alignment.bottomRight,
),
),
child: Center(
child: SingleChildScrollView( // Permet le défilement si le contenu est trop grand
padding: const EdgeInsets.all(24.0),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.stretch, // Étire les éléments horizontalement
children: <Widget>[
// Icône de l'application
Icon(
Icons.fingerprint_rounded,
size: 120,
color: Colors.white.withOpacity(0.9),
),
const SizedBox(height: 20),
// Titre de l'application
Text(
'Authentification Biométrique',
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 32,
fontWeight: FontWeight.bold,
color: Colors.white,
shadows: [
Shadow(
blurRadius: 10.0,
color: Colors.black.withOpacity(0.3),
offset: const Offset(3.0, 3.0),
),
],
),
),
const SizedBox(height: 40),
// Carte pour le statut de disponibilité
Card(
elevation: 10,
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(20)),
color: Colors.white.withOpacity(0.95),
child: Padding(
padding: const EdgeInsets.all(20.0),
child: Column(
children: [
Icon(
_biometricAvailable ? Icons.lock_open_rounded : Icons.lock_outline_rounded,
size: 60,
color: _biometricAvailable ? Colors.green.shade600 : Colors.red.shade600,
),
const SizedBox(height: 15),
Text(
_biometricAvailable
? 'Biométrie prête à l\'emploi !'
: 'Biométrie non configurée ou indisponible.',
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 20,
fontWeight: FontWeight.bold,
color: _biometricAvailable ? Colors.green.shade800 : Colors.red.shade800,
),
),
const SizedBox(height: 10),
Text(
_authStatus, // Affiche le message détaillé de disponibilité ou d'erreur
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 16,
color: _statusColor,
fontStyle: FontStyle.italic,
),
),
],
),
),
),
const SizedBox(height: 40),
// Bouton d'authentification avec un style amélioré
ElevatedButton.icon(
onPressed: _biometricAvailable ? _authenticate : null, // Désactive le bouton si la biométrie n'est pas disponible
icon: const Icon(Icons.security, size: 28),
label: const Text(
'Lancer l\'Authentification',
style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold),
),
style: ElevatedButton.styleFrom(
foregroundColor: Colors.blue.shade900, // Couleur du texte et icône
backgroundColor: Colors.white, // Couleur de fond du bouton
padding: const EdgeInsets.symmetric(horizontal: 30, vertical: 18),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(30), // Bordures très arrondies
),
elevation: 15, // Ombre plus prononcée
shadowColor: Colors.black.withOpacity(0.5), // Couleur de l'ombre
// Effet de survol/pression
// overlayColor: MaterialStateProperty.all(Colors.blue.shade100),
),
),
const SizedBox(height: 20),
// Texte du statut d'authentification
AnimatedSwitcher( // Ajoute une petite animation lors du changement de texte
duration: const Duration(milliseconds: 500),
transitionBuilder: (Widget child, Animation<double> animation) {
return ScaleTransition(scale: animation, child: child);
},
child: Text(
_authStatus,
key: ValueKey<String>(_authStatus), // Clé pour l'animation
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 20,
fontStyle: FontStyle.italic,
color: _statusColor,
fontWeight: FontWeight.w600,
),
),
),
],
),
),
),
),
),
);
}
}