contant_right_page.dart 6.19 KB
// 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> {
//   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 += 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;
//       }
//     });
//     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 ?? 0,
//             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,
//               );
//             });
//       }
//     });
//   }
// }