contant_right_page.dart
5.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:lpinyin/lpinyin.dart';
import 'package:one_key/constant/state.dart';
import 'package:one_key/models/user_history_entity.dart';
import 'package:one_key/pages/my/contant_item_page.dart';
import 'package:one_key/provider/contant_show_model.dart';
import 'package:one_key/utils/dialog_util.dart';
import 'package:one_key/utils/misc.dart';
import 'package:one_key/widgets/page_exception.dart';
import 'package:contacts_service/contacts_service.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:provider/provider.dart';
class ContantRightPage extends StatefulWidget {
ContantRightPage({Key? key}) : super(key: key);
@override
_ContantRightPageState createState() => _ContantRightPageState();
}
class _ContantRightPageState extends State<ContantRightPage> {
late List<Contact> _contacts;
bool isNeed = false;
bool goSetting = false;
bool userNo = false;
ContantShowModel? contantShowModel;
@override
void initState() {
super.initState();
WidgetsBinding.instance.addPostFrameCallback((_) {
_openAddressBook();
});
}
_openAddressBook() async {
if (!(await checkContactPermission())) {
PermissionStatus permissionStatus = await contactPermissionStatus();
if (permissionStatus == PermissionStatus.permanentlyDenied) {
//用户选择不在弹出权限提示框
print('never');
userNo = true;
setState(() {
contantShowModel!.pageStateRight = PageState.NoPermission;
});
// DialogUtil.openAlertDialog(
// context, '未获得通讯录权限', "请在手机的权限管理里,将通讯录权限开启", '去设置', 340.0,
// onTap: () {
// goSetting = true;
// openSetting();
// Navigator.pop(context, '');
// });
} else {
// 申请权限
bool canContacts = await requestContactPermission();
if (canContacts) {
refreshContacts();
} else {
setState(() {
contantShowModel!.pageStateRight = PageState.NoPermission;
});
//拒绝通讯录权限 , 苹果拒绝权限后,不再提示权限框,进入此处
// DialogUtil.openAlertDialog(
// context, '未获得通讯录权限', "请在手机的权限管理里,将通讯录权限开启。", '去设置', 340.0,
// onTap: () {
// goSetting = true;
// openSetting();
// Navigator.pop(context, '');
// });
}
}
} else {
refreshContacts();
}
}
Future <void> refreshContacts() async {
List<Contact> contacts = (await ContactsService.getContacts(
withThumbnails: false, iOSLocalizedLabels: false))
.toList();
setState(() {
_contacts = contacts;
if (_contacts.length > 0) {
contantShowModel!.contacts = [];
for (int i = 0; i < _contacts.length; i++) {
UserHistoryEntity item = UserHistoryEntity(
'',
'',
_contacts[i].displayName == null ? '' : _contacts[i].displayName,
'',
_contacts[i].phones == null
? ''
: _contacts[i].phones!.first.value!.replaceAll(' ', ''),
'','','');
item.pinyin = _contacts[i].displayName == null
? ''
: PinyinHelper.getPinyin(_contacts[i].displayName!)
.replaceAll(' ', '');
List<String> pinyinList = _contacts[i].displayName == null
? []
: PinyinHelper.getPinyin(_contacts[i].displayName!).split(' ');
item.pinyinFirst = '';
item.pinyinList = [];
String pinyinAll = ''; //组装全拼组合
for (int j = 0; j < pinyinList.length; j++) {
String letter = pinyinList[j];
pinyinAll += pinyinList[j];
item.pinyinList!.add(pinyinAll);
if (letter.length > 0) {
item.pinyinFirst = item.pinyinFirst! + pinyinList[j].substring(0, 1);
// item.pinyinFirst += pinyinList[j].substring(0, 1);
} else {
// item.pinyinFirst += '';
item.pinyinFirst = item.pinyinFirst! + '';
}
}
contantShowModel!.contacts.add(item);
}
contantShowModel!.contactsShow = contantShowModel!.contacts;
contantShowModel!.pageStateRight = PageState.Success;
} else {
contantShowModel!.pageStateRight = PageState.Empty;
}
});
await contantShowModel!.searchData(contantShowModel!.controller.text);
}
@override
Widget build(BuildContext context) {
contantShowModel = Provider.of<ContantShowModel>(context, listen: true);
return Consumer<ContantShowModel>(builder: (context, model, _) {
if (model.pageStateRight != PageState.Success) {
return ExceptionPage(
pageState: model.pageStateRight,
callback: () async {
if (await checkContactPermission()) {
_openAddressBook();
} else {
goSetting = true;
openSetting();
}
},
emptyMsg: "暂时没有联系人哦",
emptyImg: "empty_search",
showMsg: "加载中,请稍候...",
);
} else {
return ListView.builder(
itemCount: contantShowModel!.contactsShow.length,
itemBuilder: (BuildContext context, int index) {
UserHistoryEntity c = contantShowModel!.contactsShow[index];
return ContantItemPage(
onTap: () {
if (c.phone != null && c.phone != '') {
DialogUtil.goPhone(c.phone);
}
},
index: index,
model: contantShowModel,
data: contantShowModel!.contactsShow,
);
});
}
});
}
}