import 'package:flutter/material.dart';
import 'package:local_auth/local_auth.dart';
class BiometricLoginScreen extends StatefulWidget {
@override
_BiometricLoginScreenState createState() => _BiometricLoginScreenState();
}
class _BiometricLoginScreenState extends State<BiometricLoginScreen> {
final LocalAuthentication auth = LocalAuthentication();
String _message = 'Tap to authenticate';
Future<void> _authenticate() async {
bool authenticated = false;
try {
authenticated = await auth.authenticate(
localizedReason: 'Please authenticate to login',
options: const AuthenticationOptions(
biometricOnly: true,
stickyAuth: true,
),
);
} catch (e) {
setState(() {
_message = 'Error: ${e.toString()}';
});
return;
}
setState(() {
_message = authenticated ? 'Authentication successful!' : 'Authentication failed. Try again.';
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Biometric Login')),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
GestureDetector(
onTap: _authenticate,
child: const Icon(Icons.fingerprint, size: 100, color: Colors.blue),
),
const SizedBox(height: 20),
Text(_message, style: const TextStyle(fontSize: 18)),
const SizedBox(height: 40),
ElevatedButton(
onPressed: () {
// Placeholder for PIN login navigation
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('Navigate to PIN login screen')),
);
},
child: const Text('Login with PIN'),
),
],
),
),
);
}
}We use the local_auth package to access biometric features.
The _authenticate method tries to authenticate the user with biometrics only.
If authentication succeeds, we update the message to show success.
If it fails or an error occurs, we show an error message.
The fingerprint icon is wrapped in a GestureDetector so tapping it starts authentication.
The 'Login with PIN' button currently shows a message as a placeholder for navigation.