flutter_contacts_plus 1.3.0 flutter_contacts_plus: ^1.3.0 copied to clipboard
Flutter plugin to read, create, update, delete and observe native contacts on Android and iOS, with group support, vCard support, and contact permission handling
import 'package:flutter/material.dart';
import 'package:flutter_contacts_plus/flutter_contacts_plus.dart';
void main() => runApp(FlutterContactsExample());
class FlutterContactsExample extends StatefulWidget {
@override
_FlutterContactsExampleState createState() => _FlutterContactsExampleState();
}
class _FlutterContactsExampleState extends State<FlutterContactsExample> {
List<Contact>? _contacts;
bool _permissionDenied = false;
@override
void initState() {
super.initState();
_fetchContacts();
}
Future _fetchContacts() async {
if (!await FlutterContacts.requestPermission(readonly: true)) {
setState(() => _permissionDenied = true);
} else {
final contacts = await FlutterContacts.getContacts();
setState(() => _contacts = contacts);
}
}
@override
Widget build(BuildContext context) => MaterialApp(
home: Scaffold(
appBar: AppBar(title: Text('flutter_contacts_plus_example')),
body: _body()));
Widget _body() {
if (_permissionDenied) return Center(child: Text('Permission denied'));
if (_contacts == null) return Center(child: CircularProgressIndicator());
return ListView.builder(
itemCount: _contacts!.length,
itemBuilder: (context, i) => ListTile(
title: Text(_contacts![i].displayName),
onTap: () async {
final fullContact =
await FlutterContacts.getContact(_contacts![i].id);
await Navigator.of(context).push(
MaterialPageRoute(builder: (_) => ContactPage(fullContact!)));
}));
}
}
class ContactPage extends StatelessWidget {
final Contact contact;
ContactPage(this.contact);
@override
Widget build(BuildContext context) => Scaffold(
appBar: AppBar(title: Text(contact.displayName)),
body: Column(children: [
Text('First name: ${contact.name.first}'),
Text('Last name: ${contact.name.last}'),
Text(
'Phone number: ${contact.phones.isNotEmpty ? contact.phones.first.number : '(none)'}'),
Text(
'Email address: ${contact.emails.isNotEmpty ? contact.emails.first.address : '(none)'}'),
]));
}