fix: 修复iOS端通讯录点击获取手机号问题
This commit is contained in:
parent
c66b994c12
commit
879f42bd81
@ -32,13 +32,7 @@ class SendElectronicKeyView extends StatefulWidget {
|
||||
|
||||
class _SendElectronicKeyViewState extends State<SendElectronicKeyView>
|
||||
with AutomaticKeepAliveClientMixin {
|
||||
late FlutterContactPicker _contactPicker;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_contactPicker = FlutterContactPicker();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
@ -224,81 +218,44 @@ class _SendElectronicKeyViewState extends State<SendElectronicKeyView>
|
||||
alignment: Alignment.center,
|
||||
child: InkWell(
|
||||
onTap: () async {
|
||||
try {
|
||||
final PermissionStatus status =
|
||||
await Permission.contacts.request();
|
||||
if (status.isGranted) {
|
||||
// 保存当前路由
|
||||
final currentRoute = ModalRoute.of(context);
|
||||
final Contact? currentContact =
|
||||
await logic.state.contactPicker.selectContact();
|
||||
if (currentContact != null) {
|
||||
setState(() {
|
||||
logic.state.contact = currentContact;
|
||||
|
||||
final Contact? currentContact =
|
||||
await _contactPicker.selectContact();
|
||||
|
||||
// 检查路由是否仍然存在且组件是否挂载
|
||||
if (currentRoute?.isCurrent == true && mounted) {
|
||||
// 检查 GetX 控制器是否仍然存在
|
||||
if (Get.isRegistered<SendElectronicKeyViewLogic>(
|
||||
tag: widget.type)) {
|
||||
final logic = Get.find<SendElectronicKeyViewLogic>(
|
||||
tag: widget.type);
|
||||
|
||||
if (currentContact != null) {
|
||||
// 处理手机号
|
||||
if (currentContact.phoneNumbers != null &&
|
||||
currentContact.phoneNumbers!.isNotEmpty) {
|
||||
String phoneNumber =
|
||||
currentContact.phoneNumbers![0];
|
||||
// 移除所有空格、括号、连字符等特殊字符
|
||||
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();
|
||||
}
|
||||
// 处理手机号
|
||||
if (currentContact.phoneNumbers != null &&
|
||||
currentContact.phoneNumbers!.isNotEmpty) {
|
||||
// 获取第一个手机号并清理格式
|
||||
String phoneNumber = currentContact.phoneNumbers![0];
|
||||
// 移除所有空格、括号、连字符等特殊字符
|
||||
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;
|
||||
} else {
|
||||
logic.state.emailOrPhoneController.text = '';
|
||||
}
|
||||
} else {
|
||||
// 权限被拒绝,显示提示
|
||||
if (mounted) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(
|
||||
content: Text('需要通讯录权限才能选择联系人'.tr),
|
||||
duration: const Duration(seconds: 2),
|
||||
),
|
||||
);
|
||||
|
||||
// 处理姓名
|
||||
if (currentContact.fullName != null &&
|
||||
currentContact.fullName!.isNotEmpty) {
|
||||
logic.state.keyNameController.text =
|
||||
currentContact.fullName!;
|
||||
} 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,
|
||||
child: InkWell(
|
||||
onTap: () async {
|
||||
try {
|
||||
final PermissionStatus status =
|
||||
await Permission.contacts.request();
|
||||
if (status.isGranted) {
|
||||
final Contact? currentContact =
|
||||
await _contactPicker.selectContact();
|
||||
if (currentContact != null && mounted) {
|
||||
final Contact? currentContact =
|
||||
await logic.state.contactPicker.selectContact();
|
||||
if (currentContact != null) {
|
||||
setState(() {
|
||||
logic.state.contact = currentContact;
|
||||
|
||||
// 处理手机号
|
||||
if (currentContact.phoneNumbers != null &&
|
||||
currentContact.phoneNumbers!.isNotEmpty) {
|
||||
// 获取第一个手机号并清理格式
|
||||
String phoneNumber = currentContact.phoneNumbers![0];
|
||||
// 移除所有空格、括号、连字符等特殊字符
|
||||
phoneNumber =
|
||||
@ -709,7 +666,8 @@ class _SendElectronicKeyViewState extends State<SendElectronicKeyView>
|
||||
phoneNumber.replaceAll(RegExp(r'[^\d]'), '');
|
||||
}
|
||||
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) {
|
||||
logic.state.keyNameController.text =
|
||||
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
|
||||
|
||||
@ -15,8 +15,8 @@ class SendElectronicKeyViewState {
|
||||
TextEditingController realNameController = TextEditingController(); //真实姓名输入框
|
||||
TextEditingController idCardController = TextEditingController(); //身份证号输入框
|
||||
|
||||
// final FlutterContactPicker contactPicker = FlutterContactPicker();
|
||||
// late Contact contact;
|
||||
final FlutterContactPicker contactPicker = FlutterContactPicker();
|
||||
late Contact contact;
|
||||
|
||||
RxBool isRemoteUnlock = false.obs; //是否允许远程开锁
|
||||
RxBool isAuthentication = false.obs; //是否可以实名认证
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user