Flutter生物识别认证插件simple_biometric的使用
Flutter生物识别认证插件simple_biometric的使用
Simple Biometric 是一个用于在Flutter应用中实现生物识别认证的插件。它提供了简单易用的API,支持Android和iOS平台,并允许定制认证对话框。
特性
- 简单易用的生物识别认证API。
- 支持Android和iOS平台。
- 可定制的认证对话框,包括标题、描述和取消按钮文本。
初步配置
对于Android:
- 在主Android文件中添加以下导入和类:
import io.flutter.embedding.android.FlutterFragmentActivity; class MainActivity: FlutterFragmentActivity() { }
- 在
AndroidManifest.xml
中添加权限:<uses-permission android:name="android.permission.USE_BIOMETRIC"/>
对于iOS:
- 在
Info.plist
文件中添加以下键值对:<key>NSFaceIDUsageDescription</key> <string>This app requires facial authentication to access certain functionalities.</string> <key>NSTouchIDUsageDescription</key> <string>This app requires fingerprint authentication to access certain functionalities.</string>
使用方法
导入包
import 'package:simple_biometric/simple_biometric.dart';
Android特定:检查生物识别硬件是否可用
在尝试认证之前,请先检查生物识别硬件是否可用:
final isBiometricAvailable = await _simpleBiometric.isAndroidBiometricHardwareAvailable();
iOS特定:检查生物识别类型
在iOS上,可以检查可用的生物识别类型(Face ID或Touch ID):
IOSBiometricType iosBiometricType = await SimpleBiometric.getIOSBiometricType();
if(iosBiometricType == IOSBiometricType.faceId) {
// Face ID 可用
} else if (iosBiometricType == IOSBiometricType.touchId) {
// Touch ID 可用
}
使用生物识别进行认证
通过调用showBiometricPrompt
方法并提供可选的自定义参数来进行认证:
try{
BiometricStatusResult? result = await SimpleBiometric.showBiometricPrompt(
title: 'Biometric Authentication',
description: 'Authenticate using your biometric credential',
cancelText: 'Cancel',
);
switch (result) {
case BiometricStatusResult.authSuccess:
log('Authentication successful');
break;
// 其他情况处理...
default:
if (Platform.isIOS && _biometricType.isNotEmpty) {
log('Authentication config unavailable');
_showConfigBiometricDialog();
}
break;
}
} catch (e) {
}
示例Demo
下面是一个完整的示例应用程序,演示了如何使用Simple Biometric插件:
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:simple_biometric/simple_biometric.dart';
import 'package:simple_biometric/enums.dart';
void main() {
runApp(const LoginApp());
}
class LoginApp extends StatelessWidget {
const LoginApp({super.key});
[@override](/user/override)
Widget build(BuildContext context) {
return const MaterialApp(
title: 'Biometric Login',
home: LoginPage(),
);
}
}
class LoginPage extends StatefulWidget {
const LoginPage({super.key});
[@override](/user/override)
State<LoginPage> createState() => _LoginPageState();
}
class _LoginPageState extends State<LoginPage> {
final TextEditingController _usernameController = TextEditingController();
final TextEditingController _passwordController = TextEditingController();
final SimpleBiometric _simpleBiometric = SimpleBiometric();
bool _isBiometricAvailable = false;
bool _biometricEnabled = false;
String _biometricType = "";
[@override](/user/override)
void initState() {
super.initState();
_initBiometricType();
}
Future<void> _initBiometricType() async {
if (Platform.isAndroid) {
setState(() {
_biometricType = "";
});
}
if (Platform.isIOS) {
final value = await _simpleBiometric.getIOSBiometricType();
setState(() {
_biometricType = value == IOSBiometricType.faceId ? "Face Id" : value == IOSBiometricType.touchId ? "Touch ID" : "";
});
}
await _checkBiometricAvailability();
}
Future<void> _checkBiometricAvailability() async {
if (Platform.isAndroid) {
final isBiometricAvailable = await _simpleBiometric.isAndroidBiometricHardwareAvailable();
setState(() {
_isBiometricAvailable = isBiometricAvailable;
});
}
if (Platform.isIOS) {
setState(() {
_isBiometricAvailable = _biometricType.isNotEmpty;
});
}
}
Future<void> _authenticate() async {
if (_biometricEnabled) {
try {
final result = await _simpleBiometric.showBiometricPrompt(
title: 'Biometric Authentication',
description: 'Authenticate using your biometric credential',
cancelText: 'Cancel',
);
_handleAuthenticationResult(result);
} catch (e) {
// error handling
}
}
}
void _handleAuthenticationResult(BiometricStatusResult? result) {
switch (result) {
case BiometricStatusResult.authSuccess:
debugPrint('Authentication successful');
break;
// 其他情况处理...
default:
if (Platform.isIOS && _biometricType.isNotEmpty) {
debugPrint('Authentication config unavailable');
_showConfigBiometricDialog();
}
break;
}
}
void _showConfigBiometricDialog() {
if (context.mounted) {
showDialog(
context: context,
builder: (context) => AlertDialog(
title: const Text('No Biometrics Available'),
content: const Text('Please go to settings and set up your biometric authentication.'),
actions: [
TextButton(
onPressed: () => Navigator.of(context).pop(),
child: const Text('OK'),
),
],
),
);
}
}
[@override](/user/override)
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Biometric Login'),
),
body: Padding(
padding: const EdgeInsets.all(16.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
TextField(
controller: _usernameController,
decoration: const InputDecoration(labelText: 'Username'),
),
TextField(
controller: _passwordController,
decoration: const InputDecoration(labelText: 'Password'),
obscureText: true,
),
if (_isBiometricAvailable)
CheckboxListTile(
title: Text('Enable ${_biometricType.isNotEmpty ? _biometricType : "biometric"} access'),
value: _biometricEnabled,
onChanged: (value) {
setState(() {
_biometricEnabled = value!;
});
},
),
ElevatedButton(
onPressed: _authenticate,
child: const Text('Login'),
),
],
),
),
);
}
}
更多关于Flutter生物识别认证插件simple_biometric的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html
1 回复