flutter指纹识别

 


一  : 引入local_auth: ^0.6.1

二  :  添加权限Androidmanifest.xml 

   <uses-permission android:name="android.permission.USE_FINGERPRINT"/>

三:修改主题

android:theme="@style/Theme.AppCompat"

四 : 代码展示以及汉化(注意需要最新的flutter_sdk)

import 'package:flutter/material.dart';
import 'package:local_auth/local_auth.dart';
import 'package:local_auth/auth_strings.dart';

void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);
  final String title;
  @override
  _MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {

  @override
  Widget build(BuildContext context) {
    var localAuth = LocalAuthentication();

    return Scaffold(
      appBar: AppBar(
        title: Text("指纹识别认证演示"),
      ),
      body:Center(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                RaisedButton(
                    onPressed: ()async {
                      try{
                        bool canCheckBiometrics =
                        await localAuth.canCheckBiometrics;
                           print(canCheckBiometrics);
                      }catch(e){
                        print(e);
                      }
                    },
                     child: Text("检测是否支持生物识别"),
                ),
                  SizedBox(height: 30),
                  RaisedButton(
                    onPressed: () async{
                         try{
                           List<BiometricType> availableBiometrics =
                           await localAuth.getAvailableBiometrics();
                           print(availableBiometrics);
                         }catch(e){
                           print(e);
                         }

                    },
                     child: Text("获取生物识别技术列表"),
                ),
                SizedBox(height: 30),
                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);
                    } catch (e) {
                      print(e);
                    }

                  },
                  child: Text("指纹生物识别"),
                )
              ],
            ),
      )
 
    );
  }
}
已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 数字20 设计师:CSDN官方博客 返回首页