如何在Flutter中为我的代码添加指纹?

时间:2019-05-30 08:32:40

标签: android flutter dart

如何在Flutter代码中添加指纹   需要步骤

Local auth

我会用它

1 个答案:

答案 0 :(得分:1)

1)打开pubspec.yaml并添加:

  local_auth: ^0.3.0

2)打开AndroidManiFest.xml并添加:

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

3)将此添加到您的班级中

import 'dart:async';

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



class FinPage extends StatefulWidget {
  @override
  FinPageState createState() => FinPageState();
}

class FinPageState extends State<FinPage> {
  final LocalAuthentication auth = LocalAuthentication();
  bool _canCheckBiometrics;
  List<BiometricType> _availableBiometrics;
  String _authorized = 'Not Authorized';

  Future<void> _checkBiometrics() async {
    bool canCheckBiometrics;
    try {
      canCheckBiometrics = await auth.canCheckBiometrics;
    } on PlatformException catch (e) {
      print(e);
    }
    if (!mounted) return;

    setState(() {
      _canCheckBiometrics = canCheckBiometrics;
    });
  }

  Future<void> _getAvailableBiometrics() async {
    List<BiometricType> availableBiometrics;
    try {
      availableBiometrics = await auth.getAvailableBiometrics();
    } on PlatformException catch (e) {
      print(e);
    }
    if (!mounted) return;

    setState(() {
      _availableBiometrics = availableBiometrics;
    });
  }

  Future<void> _authenticate() async {
    bool authenticated = false;
    try {
      authenticated = await auth.authenticateWithBiometrics(
          localizedReason: 'Scan your fingerprint to authenticate',
          useErrorDialogs: true,
          stickyAuth: false);
    } on PlatformException catch (e) {
      print(e);
    }
    if (!mounted) return;

    setState(() {
      _authorized = authenticated ? 'Authorized' : 'Not Authorized';
    });
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        home: Scaffold(
          appBar: AppBar(
            title: const Text('Plugin example app'),
          ),
          body: ConstrainedBox(
              constraints: const BoxConstraints.expand(),
              child: Column(
                  mainAxisAlignment: MainAxisAlignment.spaceAround,
                  children: <Widget>[
                    Text('Can check biometrics: $_canCheckBiometrics\n'),
                    RaisedButton(
                      child: const Text('Check biometrics'),
                      onPressed: _checkBiometrics,
                    ),
                    Text('Available biometrics: $_availableBiometrics\n'),
                    RaisedButton(
                      child: const Text('Get available biometrics'),
                      onPressed: _getAvailableBiometrics,
                    ),
                    Text('Current State: $_authorized\n'),
                    RaisedButton(
                      child: const Text('Authenticate'),
                      onPressed: _authenticate,
                    )
                  ])),
        ));
  }
}