美文网首页Flutter教程网Flutter学习
Flutter获取手机通讯录联系人,支持iOS和Android

Flutter获取手机通讯录联系人,支持iOS和Android

作者: liuweilyy | 来源:发表于2019-08-27 12:09 被阅读0次

    easy_contact_picker

    Flutter 通讯录联系人选择器,同时支持Android和iOS,可以打开Native通讯录选择联系人,也可以返回通讯录列表,自己构建UI。

    用法

    添加这一行到pubspec.yaml

      easy_contact_picker: ^0.0.2
    

    引用

    import 'package:easy_contact_picker/easy_contact_picker.dart';
    

    权限添加

    Android

    <!-- 读取联系人 -->
    <uses-permission android:name="android.permission.READ_CONTACTS"/>
    

    iOS

    info.plist中添加读取通讯录权限

    Privacy - Contacts Usage Description
    

    Background Modes中 Background fetch 和 Remote notification打对勾

    示例

    调Native通讯录方法

    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;
      }
    

    返回通讯录联系人List

    Future<Contact> selectContactWithNative() async {
        final Map<dynamic, dynamic> result =
        await _channel.invokeMethod('selectContactNative');
        if (result == null) {
          return null;
        }
        return new Contact.fromMap(result);
      }
    

    需要获取权限,本示例使用的申请权限参考

     # 权限申请
      permission_handler: '^3.2.0'
    

    权限使用方法

    _openAddressBook() async{
        // 申请权限
        Map<PermissionGroup, PermissionStatus> permissions = await PermissionHandler().requestPermissions([PermissionGroup.contacts]);
    
        // 申请结果
        PermissionStatus permission = await PermissionHandler().checkPermissionStatus(PermissionGroup.contacts);
        //granted表示申请成功
        if (permission == PermissionStatus.granted){
    
        }
    
      }
    

    访问Native通讯录示例

    class HostPage extends StatefulWidget {
      @override
      _HostPageState createState() => _HostPageState();
    
    }
    
    class _HostPageState extends State<HostPage> with AutomaticKeepAliveClientMixin{
    
      Contact _contact = new Contact(fullName: "", phoneNumber: "");
      final EasyContactPicker _contactPicker = new EasyContactPicker();
    
      _openAddressBook() async{
        // 申请权限
        Map<PermissionGroup, PermissionStatus> permissions = await PermissionHandler().requestPermissions([PermissionGroup.contacts]);
    
        // 申请结果
        PermissionStatus permission = await PermissionHandler().checkPermissionStatus(PermissionGroup.contacts);
    
        if (permission == PermissionStatus.granted){
          _getContactData();
        }
    
      }
    
      _getContactData() async{
        Contact contact = await _contactPicker.selectContactWithNative();
        setState(() {
          _contact = contact;
        });
      }
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            title: Text("首页"),
          ),
          body: Column(
            children: <Widget>[
              Padding(
                padding: EdgeInsets.fromLTRB(13, 20, 13, 10),
                child: Row(
                  children: <Widget>[
                    Text("姓名:"),
                    Text(_contact.fullName)
                  ],
                ),
              ),
              Padding(
                padding: EdgeInsets.fromLTRB(13, 0, 13, 20),
                child: Row(
                  children: <Widget>[
                    Text("手机号:"),
                    Text(_contact.phoneNumber)
                  ],
                ),
              ),
              FlatButton(
                child: Text("打开通讯录"),
                onPressed: _openAddressBook,
              )
            ],
          ),
        );
      }
    
      @override
      // TODO: implement wantKeepAlive
      bool get wantKeepAlive => true;
    }
    

    获取通讯录联系人List示例

    class AddressBookPage extends StatefulWidget {
      @override
      _AddressBookPageState createState() => _AddressBookPageState();
    }
    
    class _AddressBookPageState extends State<AddressBookPage> with AutomaticKeepAliveClientMixin{
    
      List<Contact> _list = new List();
      final EasyContactPicker _contactPicker = new EasyContactPicker();
    
      @override
      void initState() {
        // TODO: implement initState
        super.initState();
    
        _openAddressBook();
      }
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            title: Text("通讯录"),
          ),
          body: ListView.builder(
            itemBuilder: (context, index){
              return _getItemWithIndex(_list[index]);
            },
            itemCount: _list.length,
          )
        );
      }
    
      Widget _getItemWithIndex(Contact contact){
        return Container(
          height: 45,
          padding: EdgeInsets.only(left: 13),
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            crossAxisAlignment: CrossAxisAlignment.start,
            children: <Widget>[
              Text(contact.fullName),
              Text(contact.phoneNumber,
                style: TextStyle(
                  color: Colors.grey,
                ),
              ),
            ],
          ),
        );
      }
    
      _openAddressBook() async{
        // 申请权限
        Map<PermissionGroup, PermissionStatus> permissions = await PermissionHandler().requestPermissions([PermissionGroup.contacts]);
    
        // 申请结果
        PermissionStatus permission = await PermissionHandler().checkPermissionStatus(PermissionGroup.contacts);
    
        if (permission == PermissionStatus.granted){
          _getContactData();
        }
    
      }
    
      _getContactData() async{
        List<Contact> list = await _contactPicker.selectContacts();
        setState(() {
          _list = list;
        });
      }
    
      @override
      // TODO: implement wantKeepAlive
      bool get wantKeepAlive => true;
    
    }
    

    完整例子请访问:
    git:https://github.com/liuweilyy/easy_contact_picker

    相关文章

      网友评论

        本文标题:Flutter获取手机通讯录联系人,支持iOS和Android

        本文链接:https://www.haomeiwen.com/subject/rmasectx.html