fix: 修复iOS端通讯录点击获取手机号问题
This commit is contained in:
parent
c66b994c12
commit
879f42bd81
@ -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
|
||||||
|
|||||||
@ -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; //是否可以实名认证
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user