contacts_service 0.0.6

  • README.md
  • CHANGELOG.md
  • Example
  • Installing
  • Versions
  • 91

contacts_service

pub package A Flutter plugin to access and manage the device's contacts.

Usage

To use this plugin, add contacts_service as a dependency in your pubspec.yaml file.

Make sure you add the following permissions to your Android Manifest

<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_CONTACTS" />

On iOS, make sure to set NSContactsUsageDescription in your Info.plist

<key>NSContactsUsageDescription</key>
<string>This app requires contacts access to function properly.</string>

Example

// Import package
import 'package:contacts_service/contacts_service.dart';

// Get all contacts
Iterable<Contact> contacts = await ContactsService.getContacts();

// Get contacts matching a string
Iterable<Contact> johns = await ContactsService.getContacts(query : "john");

// Add a contact
// The contact must have a firstName / lastName to be successfully addded
await ContactsService.addContact(newContact);

//Delete a contact
await ContactsService.deleteContact(contact);

Todo

  • [ ] update contact
  • [ ] add withTumbnails optional parameter in getContacts method

Contributions

Contributions are welcome! If you find a bug or want a feature, please fill an issue.

If you want to contribute code please create a pull request.

Credits

Heavily inspired from rt2zz's react native plugin

[0.0.6] - April 13th, 2018

  • Add contact thumbnails

[0.0.5] - April 5th, 2018

  • Fix with dart2 compatibility

[0.0.4] - February 1st, 2018.

  • Implement deleteContact(Contact c) for Android and iOS

[0.0.3] - January 31st, 2018.

  • Implement addContact(Contact c) for Android and iOS

[0.0.2] - January 30th, 2018.

  • Now retrieving contacts' prefixes and suffixes

[0.0.1] - January 30th, 2018.

  • All contacts can be retrieved
  • Contacts matching a string can be retrieved

example/lib/main.dart

import 'package:flutter/material.dart';
import 'package:contacts_service/contacts_service.dart';

void main() => runApp(new DemoApp());

class DemoApp extends StatelessWidget{
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      routes: <String, WidgetBuilder>{
        '/add': (BuildContext context) => new _AddContactPage()
      },
      home: new MyApp()
    );
  }
}

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => new _MyAppState();
}

class _MyAppState extends State<MyApp> {
  Iterable<Contact> _contacts;

  @override
  initState() {
    super.initState();
    initPlatformState();
  }

  initPlatformState() async {
    var contacts = await ContactsService.getContacts();
    setState(() {_contacts = contacts;});
  }

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(title: new Text('Contacts plugin example')),
      floatingActionButton: new FloatingActionButton(
        child: new Icon(Icons.add),
        onPressed: (){Navigator.of(context).pushNamed("/add");}
      ),
      body: new SafeArea(
        child: _contacts != null?
          new ListView.builder(
            itemCount: _contacts?.length ?? 0,
            itemBuilder: (BuildContext context, int index) {
              Contact c = _contacts?.elementAt(index);
              return new ListTile(
                onTap: () { Navigator.of(context).push(new MaterialPageRoute(builder: (BuildContext context) => new _ContactDetails(c)));},
                leading: (c.avatar != null && c.avatar.length > 0) ?
                new CircleAvatar(backgroundImage: new MemoryImage(c.avatar)):
                new CircleAvatar(child:  new Text(c.displayName?.length > 1 ? c.displayName?.substring(0, 2) : "")),
                title: new Text(c.displayName ?? ""),
              );
            },
          ):
          new Center(child: new CircularProgressIndicator()),
      ),
    );
  }
}

class _ContactDetails extends StatelessWidget{

  _ContactDetails(this._contact);
  final Contact _contact;

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
        appBar: new AppBar(
          title: new Text(_contact.displayName ?? ""),
          actions: <Widget>[new FlatButton(child:new Icon(Icons.delete), onPressed: (){ContactsService.deleteContact(_contact);})]
        ),
        body: new SafeArea(
          child: new ListView(
            children: <Widget>[
              new ListTile(title: new Text("Name"),trailing: new Text(_contact.givenName ?? "")),
              new ListTile(title: new Text("Middle name"),trailing: new Text(_contact.middleName ?? "")),
              new ListTile(title: new Text("Family name"),trailing: new Text(_contact.familyName ?? "")),
              new ListTile(title: new Text("Prefix"),trailing: new Text(_contact.prefix ?? "")),
              new ListTile(title: new Text("Suffix"),trailing: new Text(_contact.suffix ?? "")),
              new ListTile(title: new Text("Company"),trailing: new Text(_contact.company ?? "")),
              new ListTile(title: new Text("Job"),trailing: new Text(_contact.jobTitle ?? "")),
              new _AddressesTile(_contact.postalAddresses),
              new ItemsTile("Phones", _contact.phones),
              new ItemsTile("Emails", _contact.emails)
            ],
          ),
        )
    );
  }
}

class _AddressesTile extends StatelessWidget{

  _AddressesTile(this._addresses);
  final Iterable<PostalAddress> _addresses;

  Widget build(BuildContext context){
    return new Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: <Widget>[
          new ListTile(title : new Text("Addresses")),
          new Column(
              children: _addresses.map((a) => new Padding(
                padding : const EdgeInsets.symmetric(horizontal: 16.0),
                child: new Column(
                  children: <Widget>[
                    new ListTile(title : new Text("Street"), trailing: new Text(a.street)),
                    new ListTile(title : new Text("Postcode"), trailing: new Text(a.postcode)),
                    new ListTile(title : new Text("City"), trailing: new Text(a.city)),
                    new ListTile(title : new Text("Region"), trailing: new Text(a.region)),
                    new ListTile(title : new Text("Country"), trailing: new Text(a.country)),
                  ],
                ),
              )).toList()
          )
        ]
    );
  }
}

class ItemsTile extends StatelessWidget{

  ItemsTile(this._title, this._items);
  Iterable<Item> _items;
  String _title;

  @override
  Widget build(BuildContext context) {
    return new Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: <Widget>[
          new ListTile(title : new Text(_title)),
          new Column(
            children: _items.map((i) => new Padding(
              padding : const EdgeInsets.symmetric(horizontal: 16.0),
              child: new ListTile(title: new Text(i.label ?? ""), trailing: new Text(i.value ?? "")))).toList()
          )
        ]
    );
  }
}

class _AddContactPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => new _AddContactPageState();
}

class _AddContactPageState extends State<_AddContactPage>{

  Contact contact = new Contact();
  PostalAddress address = new PostalAddress(label: "Home");
  final GlobalKey<FormState> _formKey = new GlobalKey<FormState>();

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        title: new Text("Add a contact"),
        actions: <Widget>[
          new FlatButton(
            onPressed: (){
              _formKey.currentState.save();
              contact.postalAddresses = [address];
              ContactsService.addContact(contact);
              Navigator.of(context).pop();
            },
            child: new Icon(Icons.save, color: Colors.white)
          )
        ],
      ),
      body: new Container(
        padding: new EdgeInsets.all(12.0),
        child: new Form(
          key: _formKey,
          child: new ListView(
            children: <Widget>[
              new TextFormField(decoration: const InputDecoration(labelText: 'First name'), onSaved: (v) => contact.givenName = v),
              new TextFormField(decoration: const InputDecoration(labelText: 'Middle name'), onSaved: (v) => contact.middleName = v),
              new TextFormField(decoration: const InputDecoration(labelText: 'Last name'), onSaved: (v) => contact.familyName = v),
              new TextFormField(decoration: const InputDecoration(labelText: 'Prefix'), onSaved: (v) => contact.prefix = v),
              new TextFormField(decoration: const InputDecoration(labelText: 'Suffix'), onSaved: (v) => contact.suffix = v),
              new TextFormField(
                  decoration: const InputDecoration(labelText: 'Phone'),
                  onSaved: (v) => contact.phones = [new Item(label: "mobile", value: v)],
                  keyboardType: TextInputType.phone
              ),
              new TextFormField(
                  decoration: const InputDecoration(labelText: 'E-mail'),
                  onSaved: (v) => contact.emails = [new Item(label: "work", value: v)],
                  keyboardType: TextInputType.emailAddress
              ),
              new TextFormField(decoration: const InputDecoration(labelText: 'Company'), onSaved: (v) => contact.company = v),
              new TextFormField(decoration: const InputDecoration(labelText: 'Job'), onSaved: (v) => contact.jobTitle = v),
              new TextFormField(decoration: const InputDecoration(labelText: 'Street'), onSaved: (v) => address.street = v),
              new TextFormField(decoration: const InputDecoration(labelText: 'City'), onSaved: (v) => address.city = v),
              new TextFormField(decoration: const InputDecoration(labelText: 'Region'), onSaved: (v) => address.region = v),
              new TextFormField(decoration: const InputDecoration(labelText: 'Postal code'), onSaved: (v) => address.postcode = v),
              new TextFormField(decoration: const InputDecoration(labelText: 'Country'), onSaved: (v) => address.country = v),
            ],
          )
        ),
      ),
    );
  }
}

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  contacts_service: "^0.0.6"

2. Install it

You can install packages from the command line:

with Flutter:


$ flutter packages get

Alternatively, your editor might support packages get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:contacts_service/contacts_service.dart';
        
Version Uploaded Documentation Archive
0.0.6 Apr 13, 2018 Go to the documentation of contacts_service 0.0.6 Download contacts_service 0.0.6 archive
0.0.5 Apr 5, 2018 Go to the documentation of contacts_service 0.0.5 Download contacts_service 0.0.5 archive
0.0.4 Feb 1, 2018 Go to the documentation of contacts_service 0.0.4 Download contacts_service 0.0.4 archive
0.0.3 Jan 31, 2018 Go to the documentation of contacts_service 0.0.3 Download contacts_service 0.0.3 archive
0.0.2 Jan 30, 2018 Go to the documentation of contacts_service 0.0.2 Download contacts_service 0.0.2 archive
0.0.1 Jan 30, 2018 Go to the documentation of contacts_service 0.0.1 Download contacts_service 0.0.1 archive

Analysis

We analyzed this package on Apr 23, 2018, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.0.0-dev.49.0
  • pana: 0.10.6
  • Flutter: 0.3.2

Scores

Popularity:
Describes how popular the package is relative to other packages. [more]
82 / 100
Health:
Code health derived from static analysis. [more]
99 / 100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100 / 100
Overall score:
Weighted score of the above. [more]
91
Learn more about scoring.

Platforms

Detected platforms: Flutter

References Flutter, and has no conflicting libraries.

Suggestions

  • Package is pre-v1 release.

    While there is nothing inherently wrong with versions of 0.*.*, it usually means that the author is still experimenting with the general direction API.

  • Fix analysis and formatting issues.

    Analysis or formatting checks reported 1 hint.

    Run flutter format to format lib/contacts_service.dart.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=1.19.0 <2.0.0
flutter 0.0.0
Transitive dependencies
collection 1.14.6 1.14.9
meta 1.1.2
sky_engine 0.0.99
typed_data 1.1.5
vector_math 2.0.6