gpt4 book ai didi

flutter - 如何在 Flutter 中设置本地身份验证?

转载 作者:行者123 更新时间:2023-12-05 04:35:52 24 4
gpt4 key购买 nike

我已将 firebase 登录连接到我的测试应用程序,并想在那里使用本地身份验证。我看过很多基于 local_auth 包的示例,但它们只显示了如何在本地进行身份验证,但我想在身份验证后使用电子邮件和密码。

为了更清晰,我从媒体上读了一篇帖子,上面写着:

** 当用户在首次登录/注册后决定使用上述方法激活身份验证时,他/她实际上说将我的登录凭据保存在本地主机中,没有注册指纹和面部 ID 的人无法访问。因此,当用户登录并决定使用此方法时,它基本上会从本地存储的位置获取登录凭据。 **

但是我该如何在 flutter 中实现它呢?

最佳答案

您需要像这个示例一样存储您的凭据。请看看


  dependencies:
flutter_secure_storage: ^5.0.2

import 'dart:async';
import 'dart:io';
import 'dart:math';

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_secure_storage/flutter_secure_storage.dart';

void main() {
runApp(const MaterialApp(home: ItemsWidget()));
}

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

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

enum _Actions { deleteAll }
enum _ItemActions { delete, edit, containsKey }

class _ItemsWidgetState extends State<ItemsWidget> {
final _storage = const FlutterSecureStorage();
final _accountNameController =
TextEditingController(text: 'flutter_secure_storage_service');

List<_SecItem> _items = [];

@override
void initState() {
super.initState();

_accountNameController.addListener(() => _readAll());
_readAll();
}

Future<void> _readAll() async {
final all = await _storage.readAll(
iOptions: _getIOSOptions(), aOptions: _getAndroidOptions());
setState(() {
_items = all.entries
.map((entry) => _SecItem(entry.key, entry.value))
.toList(growable: false);
});
}

void _deleteAll() async {
await _storage.deleteAll(
iOptions: _getIOSOptions(), aOptions: _getAndroidOptions());
_readAll();
}

void _addNewItem() async {
final String key = _randomValue();
final String value = _randomValue();

await _storage.write(
key: key,
value: value,
iOptions: _getIOSOptions(),
aOptions: _getAndroidOptions());
_readAll();
}

IOSOptions _getIOSOptions() => IOSOptions(
accountName: _getAccountName(),
);

AndroidOptions _getAndroidOptions() => const AndroidOptions(
encryptedSharedPreferences: true,
);

String? _getAccountName() =>
_accountNameController.text.isEmpty ? null : _accountNameController.text;

@override
Widget build(BuildContext context) => Scaffold(
appBar: AppBar(
title: const Text('Plugin example app'),
actions: <Widget>[
IconButton(
key: const Key('add_random'),
onPressed: _addNewItem,
icon: const Icon(Icons.add)),
PopupMenuButton<_Actions>(
key: const Key('popup_menu'),
onSelected: (action) {
switch (action) {
case _Actions.deleteAll:
_deleteAll();
break;
}
},
itemBuilder: (BuildContext context) =>
<PopupMenuEntry<_Actions>>[
const PopupMenuItem(
key: Key('delete_all'),
value: _Actions.deleteAll,
child: Text('Delete all'),
),
])
],
),
body: Column(
children: [
if (!kIsWeb && Platform.isIOS)
Padding(
padding: const EdgeInsets.symmetric(horizontal: 16),
child: TextFormField(
controller: _accountNameController,
decoration:
const InputDecoration(labelText: 'kSecAttrService'),
),
),
Expanded(
child: ListView.builder(
itemCount: _items.length,
itemBuilder: (BuildContext context, int index) => ListTile(
trailing: PopupMenuButton(
key: Key('popup_row_$index'),
onSelected: (_ItemActions action) =>
_performAction(action, _items[index], context),
itemBuilder: (BuildContext context) =>
<PopupMenuEntry<_ItemActions>>[
PopupMenuItem(
value: _ItemActions.delete,
child: Text(
'Delete',
key: Key('delete_row_$index'),
),
),
PopupMenuItem(
value: _ItemActions.edit,
child: Text(
'Edit',
key: Key('edit_row_$index'),
),
),
PopupMenuItem(
value: _ItemActions.containsKey,
child: Text(
'Contains Key',
key: Key('contains_row_$index'),
),
),
]),
title: Text(
_items[index].value,
key: Key('title_row_$index'),
),
subtitle: Text(
_items[index].key,
key: Key('subtitle_row_$index'),
),
),
),
),
],
),
);

Future<void> _performAction(
_ItemActions action, _SecItem item, BuildContext context) async {
switch (action) {
case _ItemActions.delete:
await _storage.delete(
key: item.key,
iOptions: _getIOSOptions(),
aOptions: _getAndroidOptions());
_readAll();

break;
case _ItemActions.edit:
final result = await showDialog<String>(
context: context,
builder: (context) => _EditItemWidget(item.value));
if (result != null) {
await _storage.write(
key: item.key,
value: result,
iOptions: _getIOSOptions(),
aOptions: _getAndroidOptions());
_readAll();
}
break;
case _ItemActions.containsKey:
final result = await _storage.containsKey(key: item.key);
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
content: Text('Contains Key: $result'),
// backgroundColor: Colors.green,
duration: const Duration(seconds: 4),
));
break;
}
}

String _randomValue() {
final rand = Random();
final codeUnits = List.generate(20, (index) {
return rand.nextInt(26) + 65;
});

return String.fromCharCodes(codeUnits);
}
}

class _EditItemWidget extends StatelessWidget {
_EditItemWidget(String text)
: _controller = TextEditingController(text: text);

final TextEditingController _controller;

@override
Widget build(BuildContext context) {
return AlertDialog(
title: const Text('Edit item'),
content: TextField(
key: const Key('title_field'),
controller: _controller,
autofocus: true,
),
actions: <Widget>[
TextButton(
key: const Key('cancel'),
onPressed: () => Navigator.of(context).pop(),
child: const Text('Cancel')),
TextButton(
key: const Key('save'),
onPressed: () => Navigator.of(context).pop(_controller.text),
child: const Text('Save')),
],
);
}
}

class _SecItem {
_SecItem(this.key, this.value);

final String key;
final String value;
}

关于flutter - 如何在 Flutter 中设置本地身份验证?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70942533/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com