fix: 安卓选中通讯录失败问题排查
This commit is contained in:
parent
307a96d562
commit
2c316d427f
@ -29,6 +29,9 @@
|
|||||||
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
|
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
|
||||||
<!--允许读设备日志,用于问题排查-->
|
<!--允许读设备日志,用于问题排查-->
|
||||||
<uses-permission android:name="android.permission.READ_LOGS" />
|
<uses-permission android:name="android.permission.READ_LOGS" />
|
||||||
|
<!--联系人-->
|
||||||
|
<uses-permission android:name="android.permission.READ_CONTACTS" />
|
||||||
|
<uses-permission android:name="android.permission.WRITE_CONTACTS" />
|
||||||
|
|
||||||
<!--相机-->
|
<!--相机-->
|
||||||
<uses-permission android:name="android.permission.CAMERA" />
|
<uses-permission android:name="android.permission.CAMERA" />
|
||||||
|
|||||||
@ -20,6 +20,7 @@ import 'package:star_lock/tools/pickers/time_picker/model/pduration.dart';
|
|||||||
import 'package:star_lock/tools/regularExpression.dart';
|
import 'package:star_lock/tools/regularExpression.dart';
|
||||||
import 'package:star_lock/tools/showTipView.dart';
|
import 'package:star_lock/tools/showTipView.dart';
|
||||||
import 'package:star_lock/tools/submitBtn.dart';
|
import 'package:star_lock/tools/submitBtn.dart';
|
||||||
|
import 'package:permission_handler/permission_handler.dart';
|
||||||
|
|
||||||
class SendElectronicKeyView extends StatefulWidget {
|
class SendElectronicKeyView extends StatefulWidget {
|
||||||
SendElectronicKeyView({required this.type, Key? key}) : super(key: key);
|
SendElectronicKeyView({required this.type, Key? key}) : super(key: key);
|
||||||
@ -31,6 +32,14 @@ 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) {
|
||||||
super.build(context);
|
super.build(context);
|
||||||
@ -215,45 +224,81 @@ class _SendElectronicKeyViewState extends State<SendElectronicKeyView>
|
|||||||
alignment: Alignment.center,
|
alignment: Alignment.center,
|
||||||
child: InkWell(
|
child: InkWell(
|
||||||
onTap: () async {
|
onTap: () async {
|
||||||
final Contact? currentContact =
|
try {
|
||||||
await logic.state.contactPicker.selectContact();
|
final PermissionStatus status =
|
||||||
if (currentContact != null) {
|
await Permission.contacts.request();
|
||||||
logic.state.contact = currentContact;
|
if (status.isGranted) {
|
||||||
|
// 保存当前路由
|
||||||
|
final currentRoute = ModalRoute.of(context);
|
||||||
|
|
||||||
// 处理手机号
|
final Contact? currentContact =
|
||||||
if (currentContact.phoneNumbers != null &&
|
await _contactPicker.selectContact();
|
||||||
currentContact.phoneNumbers!.isNotEmpty) {
|
|
||||||
// 获取第一个手机号并清理格式
|
// 检查路由是否仍然存在且组件是否挂载
|
||||||
String phoneNumber = currentContact.phoneNumbers![0];
|
if (currentRoute?.isCurrent == true && mounted) {
|
||||||
// 移除所有空格、括号、连字符等特殊字符
|
// 检查 GetX 控制器是否仍然存在
|
||||||
phoneNumber =
|
if (Get.isRegistered<SendElectronicKeyViewLogic>(
|
||||||
phoneNumber.replaceAll(RegExp(r'[\s\(\)\-]'), '');
|
tag: widget.type)) {
|
||||||
// 如果号码以+开头,保留+号
|
final logic = Get.find<SendElectronicKeyViewLogic>(
|
||||||
if (phoneNumber.startsWith('+')) {
|
tag: widget.type);
|
||||||
phoneNumber = '+' +
|
|
||||||
phoneNumber
|
if (currentContact != null) {
|
||||||
.substring(1)
|
// 处理手机号
|
||||||
.replaceAll(RegExp(r'[^\d]'), '');
|
if (currentContact.phoneNumbers != null &&
|
||||||
} else {
|
currentContact.phoneNumbers!.isNotEmpty) {
|
||||||
phoneNumber =
|
String phoneNumber =
|
||||||
phoneNumber.replaceAll(RegExp(r'[^\d]'), '');
|
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();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
logic.state.emailOrPhoneController.text = phoneNumber;
|
|
||||||
} else {
|
} else {
|
||||||
logic.state.emailOrPhoneController.text = '';
|
// 权限被拒绝,显示提示
|
||||||
|
if (mounted) {
|
||||||
|
ScaffoldMessenger.of(context).showSnackBar(
|
||||||
|
SnackBar(
|
||||||
|
content: Text('需要通讯录权限才能选择联系人'.tr),
|
||||||
|
duration: const Duration(seconds: 2),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
} catch (e) {
|
||||||
// 处理姓名
|
print('Error selecting contact: $e');
|
||||||
if (currentContact.fullName != null &&
|
if (mounted) {
|
||||||
currentContact.fullName!.isNotEmpty) {
|
ScaffoldMessenger.of(context).showSnackBar(
|
||||||
logic.state.keyNameController.text =
|
SnackBar(
|
||||||
currentContact.fullName!;
|
content: Text('获取通讯录失败,请检查权限设置'.tr),
|
||||||
} else {
|
duration: const Duration(seconds: 2),
|
||||||
logic.state.keyNameController.text = '';
|
),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 更新UI
|
|
||||||
logic.update();
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
@ -639,17 +684,65 @@ class _SendElectronicKeyViewState extends State<SendElectronicKeyView>
|
|||||||
alignment: Alignment.center,
|
alignment: Alignment.center,
|
||||||
child: InkWell(
|
child: InkWell(
|
||||||
onTap: () async {
|
onTap: () async {
|
||||||
final Contact? currentContact =
|
try {
|
||||||
await logic.state.contactPicker.selectContact();
|
final PermissionStatus status =
|
||||||
logic.state.contact = currentContact!;
|
await Permission.contacts.request();
|
||||||
if (currentContact.phoneNumbers!.isNotEmpty) {
|
if (status.isGranted) {
|
||||||
logic.state.emailOrPhoneController.text = currentContact
|
final Contact? currentContact =
|
||||||
.phoneNumbers![0]
|
await _contactPicker.selectContact();
|
||||||
.replaceAll(RegExp(r'\s+\b|\b\s'), '');
|
if (currentContact != null && mounted) {
|
||||||
}
|
// 处理手机号
|
||||||
if (currentContact.fullName!.isNotEmpty) {
|
if (currentContact.phoneNumbers != null &&
|
||||||
logic.state.keyNameController.text =
|
currentContact.phoneNumbers!.isNotEmpty) {
|
||||||
currentContact.fullName!;
|
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();
|
||||||
|
}
|
||||||
|
} 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),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
@ -759,7 +852,7 @@ class _SendElectronicKeyViewState extends State<SendElectronicKeyView>
|
|||||||
child: Text(
|
child: Text(
|
||||||
'取消'.tr,
|
'取消'.tr,
|
||||||
style: TextStyle(
|
style: TextStyle(
|
||||||
color: Colors.black, fontSize: ScreenUtil().setSp(24)),
|
color: Colors.black, fontSize: 24.sp),
|
||||||
),
|
),
|
||||||
onPressed: () {
|
onPressed: () {
|
||||||
Navigator.pop(context);
|
Navigator.pop(context);
|
||||||
@ -802,7 +895,7 @@ class _SendElectronicKeyViewState extends State<SendElectronicKeyView>
|
|||||||
Text(
|
Text(
|
||||||
titleStr,
|
titleStr,
|
||||||
style: TextStyle(
|
style: TextStyle(
|
||||||
fontSize: ScreenUtil().setSp(20), color: Colors.black),
|
fontSize: 20.sp, color: Colors.black),
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
|
|||||||
@ -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