You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

150 lines
4.9 KiB

2 years ago
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';
2 years ago
import '../../constants/api.dart';
import '../../model/exclude_phone_model.dart';
import '../../model/hive/phone_model.dart';
2 years ago
import '../../model/network/api_client.dart';
import '../../model/network/base_model.dart';
import '../../utils/hive_store.dart';
2 years ago
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<ExcludeContactsPage> {
List<Contact>? contacts;
2 years ago
List<String> numbers = [];
2 years ago
List<String> numbers1 = [];
2 years ago
List<bool> status = [];
var flag = true;
List<PhoneModel> messList = [];
List<PhoneNum> numList = [];
2 years ago
@override
void initState() {
super.initState();
2 years ago
// _viewLoading();
2 years ago
}
Future _viewLoading() async {
2 years ago
// final SharedPreferences prefs = await SharedPreferences.getInstance();
2 years ago
contacts = await FlutterContacts.getContacts();
for (var element in contacts!) {
2 years ago
final full = await FlutterContacts.getContact(element.id);
if (full?.phones.length == 0) {
2 years ago
numbers.add('');
} else {
2 years ago
numbers.add(full!.phones.first.number);
}
2 years ago
status.add(false);
}
2 years ago
// await prefs.setStringList("addressList", numbers);
// print(contacts?.length);
2 years ago
setState(() {});
}
@override
Widget build(BuildContext context) {
2 years ago
var profileBuilder = FutureBuilder(
future: _viewLoading(),
2 years ago
builder: (BuildContext context, AsyncSnapshot<dynamic> snapshot) {
if (snapshot.connectionState == ConnectionState.waiting && flag) {
flag = false;
return Container();
} else {
return ListView.builder(
2 years ago
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);
});
2 years ago
}
},
);
return ScaffoldThemeWidget(
title: "从通讯录添加",
bottom: "添加",
2 years ago
onTap: () async {
List<Exclude> 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));
2 years ago
}
}
if (status.contains(true)) {
if (widget.qf) {
messList = await HiveStore.dataBox?.get("ml").cast<PhoneModel>();
// numList = HiveStore.dataBox?.get("pl").cast<PhoneNum>();
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});
2 years ago
print(excludeList);
if (res.code == 0) {
BotToast.showText(text: res.msg);
Get.back();
Get.back();
2 years ago
}else{
BotToast.showText(text: res.msg);
}
2 years ago
}
} else {
2 years ago
BotToast.showText(text: "还未选中手机号");
}
},
isOpacity: status.contains(true),
isBorder: true,
child: profileBuilder,
);
}
}