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 'package:shared_preferences/shared_preferences.dart'; import 'package:velocity_x/velocity_x.dart'; import '../../constants/api.dart'; import '../../model/exclude_phone_model.dart'; import '../../model/network/api_client.dart'; import '../../model/network/base_model.dart'; class ExcludeContactsPage extends StatefulWidget { const ExcludeContactsPage({Key? key}) : super(key: key); @override _ExcludeContactsPageState createState() => _ExcludeContactsPageState(); } class _ExcludeContactsPageState extends State { List? contacts; List numbers = []; List numbers1 = []; List status = []; var flag = true; @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)); } } if (status.contains(true)) { BaseModel res = await apiClient .request(API.exclude.add, data: {'exclude': excludeList}); if (res.code == 0) { BotToast.showText(text: res.msg); Get.back(); } } else { BotToast.showText(text: "还未选中手机号"); } }, isOpacity: status.contains(true), isBorder: true, child: profileBuilder, ); } }