Flutter 通讯录联系人选择器,同时支持Android和iOS。
在调用获取联系人方法之前需要先获取获取权限,
可以打开Native通讯录选择联系人,也可以返回通讯录列表,自己构建UI。
easy_contact_picker: ^0.0.2
import 'package:easy_contact_picker/easy_contact_picker.dart';
<!-- 读取联系人 -->
<uses-permission android:name="android.permission.READ_CONTACTS"/>
Privacy - Contacts Usage Description
Future<List<Contact>> selectContacts() async {
final List result =
await _channel.invokeMethod('selectContactList');
if (result == null) {
return null;
}
List<Contact> contacts = new List();
result.forEach((f){
contacts.add(new Contact.fromMap(f));
});
return contacts;
}
Contact _contact = new Contact(fullName: "", phoneNumber: "");
final EasyContactPicker _contactPicker = new EasyContactPicker();
_getContactData() async{
Contact contact = await _contactPicker.selectContactWithNative();
setState(() {
_contact = contact;
});
}
Future<Contact> selectContactWithNative() async {
final Map<dynamic, dynamic> result =
await _channel.invokeMethod('selectContactNative');
if (result == null) {
return null;
}
return new Contact.fromMap(result);
}
List<Contact> _list = new List();
final EasyContactPicker _contactPicker = new EasyContactPicker();
_getContactData() async{
List<Contact> list = await _contactPicker.selectContacts();
setState(() {
_list = list;
});
}