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.
project_telephony/lib/ui/exclude/exclude_contacts_page.dart

129 lines
3.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 'package:shared_preferences/shared_preferences.dart';
2 years ago
import 'package:velocity_x/velocity_x.dart';
2 years ago
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<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;
@override
void initState() {
super.initState();
_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);
2 years ago
if(full?.phones.length==0){
numbers.add('');
}else{
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 {
2 years ago
2 years ago
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));
}
}
if (status.contains(true)) {
2 years ago
BaseModel res = await apiClient
.request(API.exclude.add, data: {'exclude': excludeList});
if (res.code == 0) {
2 years ago
BotToast.showText(text: res.msg);
2 years ago
Get.back();
Get.back();
2 years ago
}
} else {
2 years ago
BotToast.showText(text: "还未选中手机号");
}
},
isOpacity: status.contains(true),
isBorder: true,
child: profileBuilder,
);
}
}