|
| 1 | +// Copyright 2013 The Flutter Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +// ignore_for_file: public_member_api_docs |
| 6 | + |
| 7 | +import 'dart:async'; |
| 8 | + |
| 9 | +import 'package:flutter/material.dart'; |
| 10 | +import 'package:flutter/services.dart'; |
| 11 | +import 'package:local_auth_platform_interface/local_auth_platform_interface.dart'; |
| 12 | +import 'package:local_auth_windows/local_auth_windows.dart'; |
| 13 | + |
| 14 | +void main() { |
| 15 | + runApp(const MyApp()); |
| 16 | +} |
| 17 | + |
| 18 | +class MyApp extends StatefulWidget { |
| 19 | + const MyApp({Key? key}) : super(key: key); |
| 20 | + |
| 21 | + @override |
| 22 | + State<MyApp> createState() => _MyAppState(); |
| 23 | +} |
| 24 | + |
| 25 | +class _MyAppState extends State<MyApp> { |
| 26 | + _SupportState _supportState = _SupportState.unknown; |
| 27 | + bool? _deviceSupportsBiometrics; |
| 28 | + List<BiometricType>? _enrolledBiometrics; |
| 29 | + String _authorized = 'Not Authorized'; |
| 30 | + bool _isAuthenticating = false; |
| 31 | + |
| 32 | + @override |
| 33 | + void initState() { |
| 34 | + super.initState(); |
| 35 | + LocalAuthPlatform.instance.isDeviceSupported().then( |
| 36 | + (bool isSupported) => setState(() => _supportState = isSupported |
| 37 | + ? _SupportState.supported |
| 38 | + : _SupportState.unsupported), |
| 39 | + ); |
| 40 | + } |
| 41 | + |
| 42 | + Future<void> _checkBiometrics() async { |
| 43 | + late bool deviceSupportsBiometrics; |
| 44 | + try { |
| 45 | + deviceSupportsBiometrics = |
| 46 | + await LocalAuthPlatform.instance.deviceSupportsBiometrics(); |
| 47 | + } on PlatformException catch (e) { |
| 48 | + deviceSupportsBiometrics = false; |
| 49 | + print(e); |
| 50 | + } |
| 51 | + if (!mounted) { |
| 52 | + return; |
| 53 | + } |
| 54 | + |
| 55 | + setState(() { |
| 56 | + _deviceSupportsBiometrics = deviceSupportsBiometrics; |
| 57 | + }); |
| 58 | + } |
| 59 | + |
| 60 | + Future<void> _getEnrolledBiometrics() async { |
| 61 | + late List<BiometricType> availableBiometrics; |
| 62 | + try { |
| 63 | + availableBiometrics = |
| 64 | + await LocalAuthPlatform.instance.getEnrolledBiometrics(); |
| 65 | + } on PlatformException catch (e) { |
| 66 | + availableBiometrics = <BiometricType>[]; |
| 67 | + print(e); |
| 68 | + } |
| 69 | + if (!mounted) { |
| 70 | + return; |
| 71 | + } |
| 72 | + |
| 73 | + setState(() { |
| 74 | + _enrolledBiometrics = availableBiometrics; |
| 75 | + }); |
| 76 | + } |
| 77 | + |
| 78 | + Future<void> _authenticate() async { |
| 79 | + bool authenticated = false; |
| 80 | + try { |
| 81 | + setState(() { |
| 82 | + _isAuthenticating = true; |
| 83 | + _authorized = 'Authenticating'; |
| 84 | + }); |
| 85 | + authenticated = await LocalAuthPlatform.instance.authenticate( |
| 86 | + localizedReason: 'Let OS determine authentication method', |
| 87 | + authMessages: <AuthMessages>[const WindowsAuthMessages()], |
| 88 | + options: const AuthenticationOptions( |
| 89 | + useErrorDialogs: true, |
| 90 | + stickyAuth: true, |
| 91 | + ), |
| 92 | + ); |
| 93 | + setState(() { |
| 94 | + _isAuthenticating = false; |
| 95 | + }); |
| 96 | + } on PlatformException catch (e) { |
| 97 | + print(e); |
| 98 | + setState(() { |
| 99 | + _isAuthenticating = false; |
| 100 | + _authorized = 'Error - ${e.message}'; |
| 101 | + }); |
| 102 | + return; |
| 103 | + } |
| 104 | + if (!mounted) { |
| 105 | + return; |
| 106 | + } |
| 107 | + |
| 108 | + setState( |
| 109 | + () => _authorized = authenticated ? 'Authorized' : 'Not Authorized'); |
| 110 | + } |
| 111 | + |
| 112 | + Future<void> _authenticateWithBiometrics() async { |
| 113 | + bool authenticated = false; |
| 114 | + try { |
| 115 | + setState(() { |
| 116 | + _isAuthenticating = true; |
| 117 | + _authorized = 'Authenticating'; |
| 118 | + }); |
| 119 | + authenticated = await LocalAuthPlatform.instance.authenticate( |
| 120 | + localizedReason: |
| 121 | + 'Scan your fingerprint (or face or whatever) to authenticate', |
| 122 | + authMessages: <AuthMessages>[const WindowsAuthMessages()], |
| 123 | + options: const AuthenticationOptions( |
| 124 | + useErrorDialogs: true, |
| 125 | + stickyAuth: true, |
| 126 | + biometricOnly: true, |
| 127 | + ), |
| 128 | + ); |
| 129 | + setState(() { |
| 130 | + _isAuthenticating = false; |
| 131 | + _authorized = 'Authenticating'; |
| 132 | + }); |
| 133 | + } on PlatformException catch (e) { |
| 134 | + print(e); |
| 135 | + setState(() { |
| 136 | + _isAuthenticating = false; |
| 137 | + _authorized = 'Error - ${e.message}'; |
| 138 | + }); |
| 139 | + return; |
| 140 | + } |
| 141 | + if (!mounted) { |
| 142 | + return; |
| 143 | + } |
| 144 | + |
| 145 | + final String message = authenticated ? 'Authorized' : 'Not Authorized'; |
| 146 | + setState(() { |
| 147 | + _authorized = message; |
| 148 | + }); |
| 149 | + } |
| 150 | + |
| 151 | + Future<void> _cancelAuthentication() async { |
| 152 | + await LocalAuthPlatform.instance.stopAuthentication(); |
| 153 | + setState(() => _isAuthenticating = false); |
| 154 | + } |
| 155 | + |
| 156 | + @override |
| 157 | + Widget build(BuildContext context) { |
| 158 | + return MaterialApp( |
| 159 | + home: Scaffold( |
| 160 | + appBar: AppBar( |
| 161 | + title: const Text('Plugin example app'), |
| 162 | + ), |
| 163 | + body: ListView( |
| 164 | + padding: const EdgeInsets.only(top: 30), |
| 165 | + children: <Widget>[ |
| 166 | + Column( |
| 167 | + mainAxisAlignment: MainAxisAlignment.center, |
| 168 | + children: <Widget>[ |
| 169 | + if (_supportState == _SupportState.unknown) |
| 170 | + const CircularProgressIndicator() |
| 171 | + else if (_supportState == _SupportState.supported) |
| 172 | + const Text('This device is supported') |
| 173 | + else |
| 174 | + const Text('This device is not supported'), |
| 175 | + const Divider(height: 100), |
| 176 | + Text( |
| 177 | + 'Device supports biometrics: $_deviceSupportsBiometrics\n'), |
| 178 | + ElevatedButton( |
| 179 | + onPressed: _checkBiometrics, |
| 180 | + child: const Text('Check biometrics'), |
| 181 | + ), |
| 182 | + const Divider(height: 100), |
| 183 | + Text('Enrolled biometrics: $_enrolledBiometrics\n'), |
| 184 | + ElevatedButton( |
| 185 | + onPressed: _getEnrolledBiometrics, |
| 186 | + child: const Text('Get enrolled biometrics'), |
| 187 | + ), |
| 188 | + const Divider(height: 100), |
| 189 | + Text('Current State: $_authorized\n'), |
| 190 | + if (_isAuthenticating) |
| 191 | + ElevatedButton( |
| 192 | + onPressed: _cancelAuthentication, |
| 193 | + child: Row( |
| 194 | + mainAxisSize: MainAxisSize.min, |
| 195 | + children: const <Widget>[ |
| 196 | + Text('Cancel Authentication'), |
| 197 | + Icon(Icons.cancel), |
| 198 | + ], |
| 199 | + ), |
| 200 | + ) |
| 201 | + else |
| 202 | + Column( |
| 203 | + children: <Widget>[ |
| 204 | + ElevatedButton( |
| 205 | + onPressed: _authenticate, |
| 206 | + child: Row( |
| 207 | + mainAxisSize: MainAxisSize.min, |
| 208 | + children: const <Widget>[ |
| 209 | + Text('Authenticate'), |
| 210 | + Icon(Icons.perm_device_information), |
| 211 | + ], |
| 212 | + ), |
| 213 | + ), |
| 214 | + ElevatedButton( |
| 215 | + onPressed: _authenticateWithBiometrics, |
| 216 | + child: Row( |
| 217 | + mainAxisSize: MainAxisSize.min, |
| 218 | + children: <Widget>[ |
| 219 | + Text(_isAuthenticating |
| 220 | + ? 'Cancel' |
| 221 | + : 'Authenticate: biometrics only'), |
| 222 | + const Icon(Icons.fingerprint), |
| 223 | + ], |
| 224 | + ), |
| 225 | + ), |
| 226 | + ], |
| 227 | + ), |
| 228 | + ], |
| 229 | + ), |
| 230 | + ], |
| 231 | + ), |
| 232 | + ), |
| 233 | + ); |
| 234 | + } |
| 235 | +} |
| 236 | + |
| 237 | +enum _SupportState { |
| 238 | + unknown, |
| 239 | + supported, |
| 240 | + unsupported, |
| 241 | +} |
0 commit comments