首页 > 编程语言 >在Flutter中制作指纹认证应用程序

在Flutter中制作指纹认证应用程序

时间:2023-09-28 15:34:04浏览次数:45  
标签:return canCheckBiometric 指纹 will auth authenticated Flutter 应用程序 availableBiomet

在Flutter中制作指纹认证应用程序_应用程序

本文主要展示如何在 Flutter 中为 android 应用程序实现指纹认证系统

现在许多手机都配备了指纹传感器,这使得用户登录和本地身份验证更容易,而且比使用密码更安全。

设置我们的项目

在我们开始编写应用程序之前,我们需要先设置一些东西。

我们需要做的第一件事是在我们的 pubspec.yaml 文件中添加 local_auth 依赖项

所以对于我的项目,我使用了这个版本,但你可能会使用最近的版本,所以我建议你检查这个链接,看看你可以使用哪个版本:

pub.dev/packages/local_auth/install

另外,请确保您的 SDK 版本与库版本兼容。

现在我们需要在 AndroidManifest.xml 文件中添加用户权限。

在Flutter中制作指纹认证应用程序_应用程序_02

image-20210717214224400

在Flutter中制作指纹认证应用程序_应用程序_03

image-20210717214246778

在我们的示例中,我们只会为 Android 手机实现此功能,对于 IOS 则不一样,但是您可以通过访问以下链接中的文档来了解如何执行此

操作。

编码

现在让我们打开 main.dart 文件并开始编写应用程序。对于布局,我刚刚添加了一个按钮,允许我们进行身份验证,并添加了 3 个文本,

它将为我们提供一些信息,它不会是什么花哨的东西。在这里我不打算给你展示布局的代码,但我会在文章的最后给你项目的完整源代

码,以便你可以查看。

现在我们开始导入重要的包,所以在Material Package之后导入这两个包

import 'package:local_auth/local_auth.dart';
import 'package:flutter/services.dart';

现在我们将添加 4 个主要变量

LocalAuthentication auth = LocalAuthentication();
  bool _canCheckBiometric;
  List<BiometricType> _availableBiometric;
  String autherized = "Not autherized";

让我向你解释每个人的作用

  • auth 对象将为我们提供使用指纹进行身份验证所需的主要功能
  • _canCheckBiometric 是一个布尔值,它会告诉我们是否有生物识别传感器
  • _availableBiometric 是一个对象列表,它将为我们提供设备中可用的不同生物特征,例如指纹或 faceID
  • autherized是一个字符串,它会告诉我们我们是否通过身份验证

现在我们将开始编写 3 个函数

//checking bimetrics
  //this function will check the sensors and will tell us
  // if we can use them or not
  Future<void> _checkBiometric() async{
    bool canCheckBiometric;
    try{
      canCheckBiometric = await auth.canCheckBiometrics;
    } on PlatformException catch(e){
      print(e);
    }
    if(!mounted) return;

    setState(() {
      _canCheckBiometric = canCheckBiometric;
    });
  }

  //this function will get all the available biometrics inside our device
  //it will return a list of objects, but for our example it will only
  //return the fingerprint biometric
  Future<void> _getAvailableBiometrics() async{
    List<BiometricType> availableBiometric;
    try{
      availableBiometric = await auth.getAvailableBiometrics();
    } on PlatformException catch(e){
      print(e);
    }
    if(!mounted) return;

    setState(() {
      _availableBiometric = availableBiometric;
    });
  }

  //this function will open an authentication dialog
  // and it will check if we are authenticated or not
  // so we will add the major action here like moving to another activity
  // or just display a text that will tell us that we are authenticated
  Future<void> _authenticate() async{
    bool authenticated = false;
    try{
      authenticated = await auth.authenticateWithBiometrics(
          localizedReason: "Scan your finger print to authenticate",
          useErrorDialogs: true,
        stickyAuth: false
      );
    } on PlatformException catch(e){
      print(e);
    }
    if(!mounted) return;

    setState(() {
       autherized = authenticated ? "Autherized success" : "Failed to authenticate";
    });
  }

请注意,如果您尚未在模拟器或设备中设置指纹安全性,则将返回对象列表的第二个函数可能会返回一个空列表,因此要修复该问题,请

打开手机设置,转到安全性并添加指纹认证,然后重新启动应用程序,您将看到检测到指纹。

现在我们已经创建了我们需要的所有函数和变量,让我们调用它们。因此,第 2 个函数将在 InitState 函数中调用,该函数将在呈现应用

程序布局之前检查生物特征,并在我们按下按钮时调用身份验证函数。

现在我们已经完成了应用程序的主要部分,让我向您展示完整的源代码。

import 'package:flutter/material.dart';
import 'package:local_auth/local_auth.dart';
import 'package:flutter/services.dart';
void main() => runApp(MaterialApp(
  
  home: AuthApp(),
));

class AuthApp extends StatefulWidget {
  @override
  _AuthAppState createState() => _AuthAppState();
}

class _AuthAppState extends State<AuthApp> {
  LocalAuthentication auth = LocalAuthentication();
  bool _canCheckBiometric;
  List<BiometricType> _availableBiometric;
  String authorized = "Not authorized";

  //checking bimetrics
  //this function will check the sensors and will tell us
  // if we can use them or not
  Future<void> _checkBiometric() async{
    bool canCheckBiometric;
    try{
      canCheckBiometric = await auth.canCheckBiometrics;
    } on PlatformException catch(e){
      print(e);
    }
    if(!mounted) return;

    setState(() {
      _canCheckBiometric = canCheckBiometric;
    });
  }

  //this function will get all the available biometrics inside our device
  //it will return a list of objects, but for our example it will only
  //return the fingerprint biometric
  Future<void> _getAvailableBiometrics() async{
    List<BiometricType> availableBiometric;
    try{
      availableBiometric = await auth.getAvailableBiometrics();
    } on PlatformException catch(e){
      print(e);
    }
    if(!mounted) return;

    setState(() {
      _availableBiometric = availableBiometric;
    });
  }

  //this function will open an authentication dialog
  // and it will check if we are authenticated or not
  // so we will add the major action here like moving to another activity
  // or just display a text that will tell us that we are authenticated
  Future<void> _authenticate() async{
    bool authenticated = false;
    try{
      authenticated = await auth.authenticateWithBiometrics(
          localizedReason: "Scan your finger print to authenticate",
          useErrorDialogs: true,
        stickyAuth: false
      );
    } on PlatformException catch(e){
      print(e);
    }
    if(!mounted) return;

    setState(() {
       authorized = authenticated ? "Autherized success" : "Failed to authenticate";
    });
  }

  @override
  void initState() {
    // TODO: implement initState
    _checkBiometric();
    _getAvailableBiometrics();
  }

  @override
  Widget build(BuildContext context) {
    return SafeArea(
      child: Scaffold(
        body: Column(
          children: <Widget>[
            Center(
              child: RaisedButton(
                onPressed: _authenticate,
                child:Text("Get Biometric"),
              ),
            ),
            Text("Can check biometric: $_canCheckBiometric"),
            Text("Available biometric: $_availableBiometric"),
            Text("Current State: $authorized"),
          ],
        ),
      ),
    );
  }
}

在Flutter中制作指纹认证应用程序_应用程序_04

标签:return,canCheckBiometric,指纹,will,auth,authenticated,Flutter,应用程序,availableBiomet
From: https://blog.51cto.com/u_16163480/7639977

相关文章

  • 解密防关联指纹浏览器:联盟营销领域的秘密武器
    联盟营销在今天的数字化时代越来越受欢迎。然而,联盟营销也面临着一些挑战,其中之一就是账号关联问题。本文将介绍如何利用防关联指纹浏览器来提升联盟营销的效果和安全性。一、什么是防关联指纹浏览器?防关联指纹浏览器是一种工具,它可以模拟不同的浏览器指纹特征,如用户代理、操作系统......
  • Flutter:桌面应用程序开发的新格局
    桌面应用开发的现状在过去,桌面应用程序的开发通常需要使用特定于操作系统的工具和语言,如C++、C#、Java等。这导致了高昂的开发成本和维护困难。尽管有一些跨平台桌面开发工具,如Electron和Qt,但它们在性能、用户体验和开发效率方面存在一些限制。Flutter的出现改变了这一格局,为桌面应......
  • 应用程序人机交互的一些反面教材
    无用的模态对话框下图是安信可开发的一款串口工具,当前是已插入USB串口并且软件为打开状态。当用户在上述情况下,主动拔除USB设备时,软件会弹出对话框,用户除了点击对话框中的OK或者右上角的X,对软件其余界面元素的任何操作都是无效的。并且点击对话框中的元素对用户是没有积极意义......
  • Jetpack Compose 和 Flutter 应该先学哪个呢?
    前言当谷歌第一次宣布JetpackCompose时,不少人认为这将是flutter的结束。毕竟,既然可以使用Google提供的本地工具,为什么还要使用跨平台框架呢?那我们来整体比较一下flutter和jetpackcompose,看看他们各自有什么特点。语言对比JetpackCompose是一个用于Android应用开发的用户界面......
  • Jetpack Compose 和 Flutter 应该先学哪个呢?
    前言当谷歌第一次宣布JetpackCompose时,不少人认为这将是flutter的结束。毕竟,既然可以使用Google提供的本地工具,为什么还要使用跨平台框架呢?那我们来整体比较一下flutter和jetpackcompose,看看他们各自有什么特点。语言对比JetpackCompose是一个用于Android应用开发的用户界面......
  • Android Flutter 混合开发高仿大厂App
    自上篇 Flutter10天高仿大厂App及小技巧积累总结 的续篇,这次更是干货满满。这篇文章将概述 Android组件化的架构搭建 及 Flutter 和 Android 如何混合开发 (整个App只有首页是用原生Android完成,其他页面都是引入之前的做好的Flutter页面) ,主宿主程序由Android搭建,采用......
  • IIS三种应用程序池回收方法
    转自:https://backend.devrank.cn/traffic-information/7082735106565228581......
  • 使用 Go 和 ADB 启动 Android 应用程序
    在移动应用程序开发中,有时我们需要自动启动Android应用程序以执行测试、截屏或其他自动化任务。本文将介绍如何使用Go编写一个程序,通过Android调试桥(ADB)来启动指定的Android应用程序。我们将提供完整的Go代码示例以及相应的说明。准备工作安装Go编程语言。你可以从Go......
  • 代码混淆和加固,保障应用程序的安全性
    ​ 前言iOS加固保护是直接针对iosipa二进制文件的保护技术,可以对iOSAPP中的可执行文件进行深度混淆、加密。使用任何工具都无法逆向、破解还原源文件。对APP进行完整性保护,防止应用程序中的代码及资源文件被恶意篡改。IpaGuard通过修改ipa文件中的macho文件中二进制数......
  • 代码混淆和加固,保障应用程序的安全性
    ​ 前言iOS加固保护是直接针对iosipa二进制文件的保护技术,可以对iOSAPP中的可执行文件进行深度混淆、加密。使用任何工具都无法逆向、破解还原源文件。对APP进行完整性保护,防止应用程序中的代码及资源文件被恶意篡改。IpaGuard通过修改ipa文件中的macho文件中二进制数......