fix: 修复iOS端通讯录点击获取手机号问题

This commit is contained in:
DaisyWu 2025-05-28 11:03:09 +08:00
parent c66b994c12
commit 879f42bd81
2 changed files with 49 additions and 113 deletions

View File

@ -32,13 +32,7 @@ class SendElectronicKeyView extends StatefulWidget {
class _SendElectronicKeyViewState extends State<SendElectronicKeyView> class _SendElectronicKeyViewState extends State<SendElectronicKeyView>
with AutomaticKeepAliveClientMixin { with AutomaticKeepAliveClientMixin {
late FlutterContactPicker _contactPicker;
@override
void initState() {
super.initState();
_contactPicker = FlutterContactPicker();
}
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
@ -224,81 +218,44 @@ class _SendElectronicKeyViewState extends State<SendElectronicKeyView>
alignment: Alignment.center, alignment: Alignment.center,
child: InkWell( child: InkWell(
onTap: () async { onTap: () async {
try { final Contact? currentContact =
final PermissionStatus status = await logic.state.contactPicker.selectContact();
await Permission.contacts.request(); if (currentContact != null) {
if (status.isGranted) { setState(() {
// logic.state.contact = currentContact;
final currentRoute = ModalRoute.of(context);
final Contact? currentContact = //
await _contactPicker.selectContact(); if (currentContact.phoneNumbers != null &&
currentContact.phoneNumbers!.isNotEmpty) {
// //
if (currentRoute?.isCurrent == true && mounted) { String phoneNumber = currentContact.phoneNumbers![0];
// GetX //
if (Get.isRegistered<SendElectronicKeyViewLogic>( phoneNumber =
tag: widget.type)) { phoneNumber.replaceAll(RegExp(r'[\s\(\)\-]'), '');
final logic = Get.find<SendElectronicKeyViewLogic>( // ++
tag: widget.type); if (phoneNumber.startsWith('+')) {
phoneNumber = '+' +
if (currentContact != null) { phoneNumber
// .substring(1)
if (currentContact.phoneNumbers != null && .replaceAll(RegExp(r'[^\d]'), '');
currentContact.phoneNumbers!.isNotEmpty) { } else {
String phoneNumber = phoneNumber =
currentContact.phoneNumbers![0]; phoneNumber.replaceAll(RegExp(r'[^\d]'), '');
//
phoneNumber = phoneNumber.replaceAll(
RegExp(r'[\s\(\)\-]'), '');
// ++
if (phoneNumber.startsWith('+')) {
phoneNumber = '+' +
phoneNumber
.substring(1)
.replaceAll(RegExp(r'[^\d]'), '');
} else {
phoneNumber = phoneNumber.replaceAll(
RegExp(r'[^\d]'), '');
}
logic.state.emailOrPhoneController.text =
phoneNumber;
logic.state.emailOrPhone.value = phoneNumber;
}
//
if (currentContact.fullName != null &&
currentContact.fullName!.isNotEmpty) {
logic.state.keyNameController.text =
currentContact.fullName!;
}
// UI
logic.update();
}
} }
logic.state.emailOrPhoneController.text = phoneNumber;
} else {
logic.state.emailOrPhoneController.text = '';
} }
} else {
// //
if (mounted) { if (currentContact.fullName != null &&
ScaffoldMessenger.of(context).showSnackBar( currentContact.fullName!.isNotEmpty) {
SnackBar( logic.state.keyNameController.text =
content: Text('需要通讯录权限才能选择联系人'.tr), currentContact.fullName!;
duration: const Duration(seconds: 2), } else {
), logic.state.keyNameController.text = '';
);
} }
} });
} catch (e) {
print('Error selecting contact: $e');
if (mounted) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text('获取通讯录失败,请检查权限设置'.tr),
duration: const Duration(seconds: 2),
),
);
}
} }
}, },
), ),
@ -684,16 +641,16 @@ class _SendElectronicKeyViewState extends State<SendElectronicKeyView>
alignment: Alignment.center, alignment: Alignment.center,
child: InkWell( child: InkWell(
onTap: () async { onTap: () async {
try { final Contact? currentContact =
final PermissionStatus status = await logic.state.contactPicker.selectContact();
await Permission.contacts.request(); if (currentContact != null) {
if (status.isGranted) { setState(() {
final Contact? currentContact = logic.state.contact = currentContact;
await _contactPicker.selectContact();
if (currentContact != null && mounted) {
// //
if (currentContact.phoneNumbers != null && if (currentContact.phoneNumbers != null &&
currentContact.phoneNumbers!.isNotEmpty) { currentContact.phoneNumbers!.isNotEmpty) {
//
String phoneNumber = currentContact.phoneNumbers![0]; String phoneNumber = currentContact.phoneNumbers![0];
// //
phoneNumber = phoneNumber =
@ -709,7 +666,8 @@ class _SendElectronicKeyViewState extends State<SendElectronicKeyView>
phoneNumber.replaceAll(RegExp(r'[^\d]'), ''); phoneNumber.replaceAll(RegExp(r'[^\d]'), '');
} }
logic.state.emailOrPhoneController.text = phoneNumber; logic.state.emailOrPhoneController.text = phoneNumber;
logic.state.emailOrPhone.value = phoneNumber; } else {
logic.state.emailOrPhoneController.text = '';
} }
// //
@ -717,34 +675,12 @@ class _SendElectronicKeyViewState extends State<SendElectronicKeyView>
currentContact.fullName!.isNotEmpty) { currentContact.fullName!.isNotEmpty) {
logic.state.keyNameController.text = logic.state.keyNameController.text =
currentContact.fullName!; currentContact.fullName!;
} else {
logic.state.keyNameController.text = '';
} }
});
// UI
logic.update();
}
} else {
//
if (mounted) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text('需要通讯录权限才能选择联系人'.tr),
duration: const Duration(seconds: 2),
),
);
}
} }
} catch (e) { },
print('Error selecting contact: $e');
if (mounted) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
content: Text('获取通讯录失败,请检查权限设置'.tr),
duration: const Duration(seconds: 2),
),
);
}
}
},
), ),
) )
else else

View File

@ -15,8 +15,8 @@ class SendElectronicKeyViewState {
TextEditingController realNameController = TextEditingController(); // TextEditingController realNameController = TextEditingController(); //
TextEditingController idCardController = TextEditingController(); // TextEditingController idCardController = TextEditingController(); //
// final FlutterContactPicker contactPicker = FlutterContactPicker(); final FlutterContactPicker contactPicker = FlutterContactPicker();
// late Contact contact; late Contact contact;
RxBool isRemoteUnlock = false.obs; // RxBool isRemoteUnlock = false.obs; //
RxBool isAuthentication = false.obs; // RxBool isAuthentication = false.obs; //