contant_right_page.dart
11.3 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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.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/provider/contant_show_model.dart';
import 'package:one_key/res/colors.dart';
import 'package:one_key/utils/dialog_util.dart';
import 'package:one_key/utils/misc.dart';
import 'package:one_key/widgets/MyDivider.dart';
import 'package:one_key/widgets/page_exception.dart';
import 'package:one_key/widgets/v_empty_view.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> {
// PageState _pageState = PageState.Loading;
List<Contact> _contacts;
bool isNeed = false;
bool goSetting = false;
bool userNo = false;
ContantShowModel contantShowModel;
@override
void initState() {
super.initState();
WidgetsBinding.instance.addPostFrameCallback((_) {
_openAddressBook();
});
}
// Future<void> _askPermissions() async {
// PermissionStatus permissionStatus = await _getContactPermission();
// if (permissionStatus != PermissionStatus.granted) {
// _handleInvalidPermissions(permissionStatus);
// }
// }
_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<PermissionStatus> _getContactPermission() async {
// PermissionStatus permission = await PermissionHandler()
// .checkPermissionStatus(PermissionGroup.contacts);
// if (permission != PermissionStatus.granted &&
// permission != PermissionStatus.disabled) {
// Map<PermissionGroup, PermissionStatus> permissionStatus =
// await PermissionHandler()
// .requestPermissions([PermissionGroup.contacts]);
// return permissionStatus[PermissionGroup.contacts] ??
// PermissionStatus.unknown;
// } else {
// return permission;
// }
// }
// void _handleInvalidPermissions(PermissionStatus permissionStatus) {
// if (permissionStatus == PermissionStatus.denied) {
// throw PlatformException(
// code: "PERMISSION_DENIED",
// message: "Access to location data denied",
// details: null);
// } else if (permissionStatus == PermissionStatus.disabled) {
// throw PlatformException(
// code: "PERMISSION_DISABLED",
// message: "Location data is not available on device",
// details: null);
// }
// }
Future<void> refreshContacts() async {
// Load without thumbnails initially.
List<Contact> contacts = (await ContactsService.getContacts(
withThumbnails: false, iOSLocalizedLabels: false))
.toList();
// var contacts = (await ContactsService.getContactsForPhone("8554964652"))
// .toList();
setState(() {
_contacts = contacts;
if (_contacts.length > 0) {
contantShowModel.contacts = [];
for (int i = 0; i < _contacts.length; i++) {
UserHistoryEntity item = UserHistoryEntity(
'',
'',
_contacts[i].displayName,
'',
_contacts[i].phones.first.value,
'');
item.pinyin = PinyinHelper.getPinyin(_contacts[i].displayName)
.replaceAll(' ', '');
item.pinyinList =
PinyinHelper.getPinyin(_contacts[i].displayName).split(' ');
item.pinyinFirst = '';
for (int j = 0; j < item.pinyinList.length; j++) {
String letter = item.pinyinList[j];
if (letter.length > 0) {
item.pinyinFirst += item.pinyinList[j].substring(0, 1);
} else {
item.pinyinFirst += '';
}
}
contantShowModel.contacts.add(item);
}
contantShowModel.contactsShow = contantShowModel.contacts;
contantShowModel.pageStateRight = PageState.Success;
} else {
contantShowModel.pageStateRight = PageState.Empty;
}
});
// Lazy load thumbnails after rendering initial contacts.
// for (final contact in contacts) {
// ContactsService.getAvatar(contact).then((avatar) {
// if (avatar == null) return; // Don't redraw if no change.
// setState(() => contact.avatar = avatar);
// });
// }
}
_openContactForm() async {
try {
var contact =
await ContactsService.openContactForm(iOSLocalizedLabels: false);
refreshContacts();
} on FormOperationException catch (e) {
switch (e.errorCode) {
case FormOperationErrorCode.FORM_OPERATION_CANCELED:
case FormOperationErrorCode.FORM_COULD_NOT_BE_OPEN:
case FormOperationErrorCode.FORM_OPERATION_UNKNOWN_ERROR:
default:
print(e.errorCode);
}
}
}
// /// app生命周期
// @override
// void didChangeAppLifecycleState(AppLifecycleState state) async {
// switch (state) {
// case AppLifecycleState.resumed: // 应用程序可见,前台
// if (!Platform.isAndroid) {
// if (isNeed) {
// isNeed = false;
// if (goSetting) {
// _getContactPemisoin();
// }
// }
// } else {
// if (goSetting) {
// _getContactPemisoin();
// }
// }
// break;
// case AppLifecycleState.paused:
// print('paused');
// // if (!Platform.isAndroid) {
// // if (!(await checkLocationPermission())) {
// // //没有定位权限
// isNeed = true;
// // } else {
// // isNeed = false;
// // }
// // }
// break;
// default:
// break;
// }
// }
/// 程序进入前台刷新位置权限(来回切换位置权限,及时刷新页面)
_getContactPemisoin() async {
if (await checkContactPermission()) {
userNo = false;
}
if (!userNo) {
goSetting = false;
_openAddressBook();
}
}
@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 ?? 0,
itemBuilder: (BuildContext context, int index) {
UserHistoryEntity c = contantShowModel.contactsShow[index];
return InkWell(
onTap: () {
if (c.phone != null && c.phone != '') {
DialogUtil.goPhone(c.phone);
}
},
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
index == 0
? MyDivider(
double.infinity,
ScreenUtil().setWidth(20),
color: MyColours.color_f5f5f5,
)
: Container(),
Container(
width: double.infinity,
padding: EdgeInsets.symmetric(
horizontal: ScreenUtil().setWidth(40),
vertical: ScreenUtil().setWidth(30)),
height: ScreenUtil().setWidth(74 * 2),
decoration: BoxDecoration(
border: Border(
top: index == 0
? BorderSide(
width: ScreenUtil().setWidth(2),
color: MyColours.color_E0E0E0)
: BorderSide(
width: 0, color: Colors.transparent),
bottom: BorderSide(
width: ScreenUtil().setWidth(2),
color: MyColours.color_E0E0E0))),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
c.rname == null || c.rname == ''
? ''
: c.rname,
style: TextStyle(
color: MyColours.color_333333,
fontSize: ScreenUtil().setSp(32))),
VEmptyView(10),
Text(
c.phone == null || c.phone == ''
? ''
: c.phone,
style: TextStyle(
color: MyColours.color_999999,
fontSize: ScreenUtil().setSp(30)))
],
),
)
]));
});
}
});
}
}