import 'package:bot_toast/bot_toast.dart'; import 'package:flutter/material.dart'; import 'package:flutter_contacts/flutter_contacts.dart'; import 'package:flutter_screenutil/flutter_screenutil.dart'; import 'package:get/get.dart'; import 'package:project_telephony/ui/widget/scaffold_theme_widget.dart'; import '../../constants/api.dart'; import '../../model/exclude_phone_model.dart'; import '../../model/hive/phone_model.dart'; import '../../model/network/api_client.dart'; import '../../model/network/base_model.dart'; import '../../utils/hive_store.dart'; class ExcludeContactsPage extends StatefulWidget { final bool qf; final int index; const ExcludeContactsPage({Key? key, this.qf = false,this.index=0}) : super(key: key); @override _ExcludeContactsPageState createState() => _ExcludeContactsPageState(); } class _ExcludeContactsPageState extends State { List? contacts; List numbers = []; List numbers1 = []; List status = []; var flag = true; List messList = []; List numList = []; @override void initState() { super.initState(); // _viewLoading(); } Future _viewLoading() async { // final SharedPreferences prefs = await SharedPreferences.getInstance(); contacts = await FlutterContacts.getContacts(); for (var element in contacts!) { final full = await FlutterContacts.getContact(element.id); if (full?.phones.length == 0) { numbers.add('无'); } else { numbers.add(full!.phones.first.number); } status.add(false); } // await prefs.setStringList("addressList", numbers); // print(contacts?.length); setState(() {}); } @override Widget build(BuildContext context) { var profileBuilder = FutureBuilder( future: _viewLoading(), builder: (BuildContext context, AsyncSnapshot snapshot) { if (snapshot.connectionState == ConnectionState.waiting && flag) { flag = false; return Container(); } else { return ListView.builder( itemCount: contacts!.length, itemBuilder: (context, i) { return ListTile( title: Text( contacts![i].displayName, style: TextStyle( fontSize: 32.sp, fontWeight: FontWeight.w600, ), ), subtitle: Text( numbers[i], style: TextStyle( fontSize: 28.sp, fontWeight: FontWeight.w300, ), ), trailing: Checkbox( value: status[i], // 改变后的事件 onChanged: (value) { setState(() { status[i] = !status[i]; }); }, // 选中后的颜色 activeColor: Colors.blue, // 选中后对号的颜色 checkColor: Colors.white, ), ).paddingOnly(left: 20.w); }); } }, ); return ScaffoldThemeWidget( title: "从通讯录添加", bottom: "添加", onTap: () async { List excludeList = []; for (int i = 0; i < status.length; i++) { if (status[i] == true) { excludeList.add( Exclude(phone: numbers[i], remark: contacts![i].displayName)); // numList.add(PhoneNum(phone: numbers[i],name: contacts![i].displayName,state: false)); } } if (status.contains(true)) { if (widget.qf) { messList = await HiveStore.dataBox?.get("ml").cast(); // numList = HiveStore.dataBox?.get("pl").cast(); for (int i = 0; i < status.length; i++) { if (status[i] == true) { messList[widget.index].phoneList!.add(PhoneNum(phone: numbers[i],name: contacts![i].displayName,state: false)); } } HiveStore.dataBox?.put("ml", messList); print(HiveStore.dataBox?.get("ml")); Get.back(); // Get.back(); // _refreshController.callRefresh(); } else { BaseModel res = await apiClient .request(API.exclude.add, data: {'exclude': excludeList}); print(excludeList); if (res.code == 0) { BotToast.showText(text: res.msg); Get.back(); Get.back(); }else{ BotToast.showText(text: res.msg); } } } else { BotToast.showText(text: "还未选中手机号"); } }, isOpacity: status.contains(true), isBorder: true, child: profileBuilder, ); } }