2

我正在使用 Flutter local_auth插件,生物识别工作正常,但 Pincode/pattern 不提供身份验证。我发现如果我从手机上移除指纹,那么 Pincode 和模式验证就可以工作,但我需要输入 2 次。该库似乎是正确的,但无法正确掌握这种奇怪行为的原因。任何人都可以建议吗?

此外,我们是否可以使用自定义 UI 进行身份验证,就像在 WhatsApp 中一样?

local_auth_api.dart:

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

class LocalAuthApi {
  static final _auth = LocalAuthentication();
  
  static const iosStrings = IOSAuthMessages(
    cancelButton: 'cancel',
    goToSettingsButton: 'settings',
    goToSettingsDescription: 'Please set up your Touch ID.',
    lockOut: 'Please reenable your Touch ID');

  static const androidStrings = AndroidAuthMessages(
    cancelButton: 'cancel',
    goToSettingsButton: 'settings',
    goToSettingsDescription: 'Please set up your Touch ID.',
    signInTitle: 'User Authorization Required',
  );
    
  static Future<bool> hasBiometrics() async {
    try {
      return await _auth.canCheckBiometrics;
    } on PlatformException catch (e) {
      return false;
    }
  }

  static Future<List<BiometricType>> getBiometrics() async {
    try {
      return await _auth.getAvailableBiometrics();
    } on PlatformException catch (e) {
      return <BiometricType>[];
    }
  }

  static Future<bool> authenticate() async {
    try {
      return await _auth.authenticate(
        localizedReason: 'Scan your Fingerprint to Authenticate',
        useErrorDialogs: true,
        sensitiveTransaction: true,
        stickyAuth: true,
        iOSAuthStrings: iosStrings,
        androidAuthStrings: androidStrings
      );
    } on PlatformException catch (e) {
      print(e);
      return false;
    }
  }
}

lock_screen.dart

import 'package:xyz/resources/constants.dart';
import 'package:xyz/src/services/local_auth_api.dart';
import 'package:flutter/material.dart';

class LockScreen extends StatefulWidget{
  const LockScreen({Key? key}) : super(key: key);

  @override
  _LockScreenState createState() => _LockScreenState();
}

class _LockScreenState extends State<LockScreen>{
  @override
  void initState() {
    super.initState();
    authenticate();
  }

  authenticate() async {
    bool isAuthenticated = false;
    while(true){
      isAuthenticated = await LocalAuthApi.authenticate();
      if (isAuthenticated) {
        print(isAuthenticated);
        break;
      }
    }
    print("unlocked");
  }

  @override
  Widget build(BuildContext context) {
    Size size = MediaQuery.of(context).size;
    double screenHeight = size.height;
    double screenWidth = size.width;
    return Scaffold(
      backgroundColor: primaryColor,
      body: Center(
      )
    );
  }
}

回复:

E/BiometricFragment(19568): Not launching prompt. Client activity was null.
4

0 回答 0