flutter-指纹识别(local_auth)
flutter-指纹识别1 准备2 开始吧2.1 引入第三方库2.2 配置文件中添加配置2.2 开始编写1 准备指纹识别需要用到第三方库local_auth,地址 https://pub.flutter-io.cn/packages/local_auth需要有一台支持指纹的安卓手机2 开始吧2.1 引入第三方库pubspec.yaml文件中引入local_auth,如下:2.2 配置文件中添加配置
·
flutter-指纹识别
1 准备
指纹识别需要用到第三方库 local_auth,地址 https://pub.flutter-io.cn/packages/local_auth
需要有一台支持指纹的安卓手机
2 开始吧
2.1 引入第三方库
pubspec.yaml文件中引入local_auth,如下:
2.2 配置文件中添加配置
android\app\src\main\AndroidManifest.xml文件中添加如下配置:
<uses-permission android:name="android.permission.USE_FINGERPRINT"/>
如下:
2.3 开始编写
lib/pages目录下,新建dart,如下:
local_auth_page.dart 如下:
import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:local_auth/auth_strings.dart';
import 'package:local_auth/local_auth.dart';
import 'package:flutter_demo/provider/sdf_provider.dart';
import 'package:provider/provider.dart';
class LocalAuthPage extends StatefulWidget {
LocalAuthPage({Key key}) : super(key: key);
@override
_LocalAuthPageState createState() => _LocalAuthPageState();
}
class _LocalAuthPageState extends State<LocalAuthPage> {
var localAuth = LocalAuthentication();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('指纹识别demo'),
),
body: Container(
child: Column(
children: [
RaisedButton(
onPressed: () async {
try {
bool canCheckBiometrics =
await localAuth.canCheckBiometrics;
print(canCheckBiometrics);
} catch (e) {
print(3);
}
},
child: Text('检测是否支持生物识别'),
),
Divider(),
RaisedButton(
onPressed: () async {
try {
List<BiometricType> availableBiometrics =
await localAuth.getAvailableBiometrics();
print(availableBiometrics);
} catch (e) {
print(e);
}
},
child: Text('获取支持的生物识别列表'),
),
Divider(),
RaisedButton(
onPressed: () async {
//下面是汉化
const andStrings = const AndroidAuthMessages(
cancelButton: '取消',
goToSettingsButton: '去设置',
fingerprintNotRecognized: '指纹识别失败',
goToSettingsDescription: '请设置指纹.',
fingerprintHint: '指纹',
fingerprintSuccess: '指纹识别成功',
signInTitle: '指纹验证',
fingerprintRequiredTitle: '请先录入指纹!',
);
try {
bool didAuthenticate =
await localAuth.authenticateWithBiometrics(
localizedReason: '扫描指纹进行身份识别',
useErrorDialogs: false,
stickyAuth: true,
androidAuthStrings: andStrings);
print('didAuthenticate: $didAuthenticate');
} catch (e) {
if (e.code == 'NotEnrolled') {
Fluttertoast.showToast(
msg: '请先设置指纹', backgroundColor: Colors.grey);
}
}
},
child: Text('指纹识别'),
),
Divider(),
// 是否开启指纹登录
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
Text('开启指纹登录'),
Provider.of<SDFProvider>(context).fingerLoginEnable
? IconButton(
onPressed: () async {
await Provider.of<SDFProvider>(context,
listen: false)
.setFingerLoginEnable(false);
},
icon: Image.asset('assets/images/switch_on.png'),
)
: IconButton(
onPressed: () async {
await Provider.of<SDFProvider>(context,
listen: false)
.setFingerLoginEnable(true);
},
icon: Image.asset('assets/images/switch_off.png')),
],
)
],
),
));
}
}
页面如下:
更多推荐
已为社区贡献1条内容
所有评论(0)