mirror of
https://github.com/nisrulz/flutter-examples.git
synced 2025-11-08 12:39:17 +00:00
New Example - Biometrics (#119)
This commit is contained in:
34
biometrics/lib/biometrics_verifier.dart
Normal file
34
biometrics/lib/biometrics_verifier.dart
Normal file
@@ -0,0 +1,34 @@
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:local_auth/local_auth.dart';
|
||||
|
||||
class BiometricsVerifier {
|
||||
late LocalAuthentication auth;
|
||||
|
||||
BiometricsVerifier() {
|
||||
auth = LocalAuthentication();
|
||||
}
|
||||
|
||||
Future<void> _ableToAuthenticate() async {
|
||||
bool supported = await auth.canCheckBiometrics;
|
||||
bool entryExists = await auth.isDeviceSupported();
|
||||
if (!supported) throw "Device doesn't support Biometrics.";
|
||||
if (!entryExists) throw "Biometric entry not found in phone.";
|
||||
}
|
||||
|
||||
Future<void> verifyBiometrics(String? prompt) async {
|
||||
await _ableToAuthenticate();
|
||||
late bool didAuthenticate;
|
||||
try {
|
||||
didAuthenticate = await auth.authenticate(
|
||||
localizedReason: prompt ?? 'Please authenticate with biometrics',
|
||||
options: const AuthenticationOptions(
|
||||
stickyAuth: true,
|
||||
biometricOnly: true,
|
||||
),
|
||||
);
|
||||
} on PlatformException {
|
||||
throw "Platform Exception : Biometrics Failed !";
|
||||
}
|
||||
if (!didAuthenticate) throw "Biometrics Failed";
|
||||
}
|
||||
}
|
||||
99
biometrics/lib/main.dart
Normal file
99
biometrics/lib/main.dart
Normal file
@@ -0,0 +1,99 @@
|
||||
import 'package:biometrics/biometrics_verifier.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
void main() => runApp(Biometrics());
|
||||
|
||||
class Biometrics extends StatelessWidget {
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return MaterialApp(
|
||||
title: 'Biometrics',
|
||||
theme: ThemeData(
|
||||
appBarTheme: const AppBarTheme(elevation: 0),
|
||||
),
|
||||
home: Home(),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
class Home extends StatefulWidget {
|
||||
@override
|
||||
State<Home> createState() => _HomeState();
|
||||
}
|
||||
|
||||
class _HomeState extends State<Home> {
|
||||
late bool isVerified;
|
||||
|
||||
late BiometricsVerifier verifier;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
isVerified = false;
|
||||
verifier = BiometricsVerifier();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
return Scaffold(
|
||||
appBar: AppBar(),
|
||||
body: SizedBox(
|
||||
width: double.infinity,
|
||||
child: Column(
|
||||
mainAxisAlignment: MainAxisAlignment.center,
|
||||
children: [
|
||||
Text(
|
||||
isVerified ? 'Verification Complete' : 'Unverified',
|
||||
style: const TextStyle(
|
||||
fontSize: 16,
|
||||
fontWeight: FontWeight.bold,
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 10),
|
||||
isVerified
|
||||
? TextButton(
|
||||
onPressed: () {
|
||||
setState(() {
|
||||
isVerified = false;
|
||||
});
|
||||
},
|
||||
child: const Text('Unverify'),
|
||||
)
|
||||
: TextButton(
|
||||
onPressed: () async {
|
||||
try {
|
||||
await verifier
|
||||
.verifyBiometrics('Please enter your fingerprint');
|
||||
// ---- Add your logic after finger print verification here
|
||||
// ---
|
||||
// ---
|
||||
setState(() {
|
||||
isVerified = true;
|
||||
});
|
||||
} catch (e) {
|
||||
// ---- Verification Failed
|
||||
showDialog(
|
||||
context: context,
|
||||
builder: (c) => AlertDialog(
|
||||
title: const Text('Error !'),
|
||||
content: Text(e.toString()),
|
||||
actions: [
|
||||
TextButton(
|
||||
onPressed: () {
|
||||
Navigator.of(context).pop();
|
||||
},
|
||||
child: const Text('Ok'),
|
||||
)
|
||||
],
|
||||
),
|
||||
);
|
||||
}
|
||||
},
|
||||
child: const Text('Verify with Fingerprint'),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user