raw_contacts 0.0.1 raw_contacts: ^0.0.1 copied to clipboard
Manage Contact lists in flutter
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:raw_contacts/raw_contacts.dart';
void main() => runApp(MyApp());
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
Iterable<Contact> _contacts;
String _accountType = "com.hub_games.langless";
GlobalKey<ScaffoldState> _key = new GlobalKey<ScaffoldState>();
@override
void initState() {
initPlatformState();
super.initState();
}
// Platform messages are asynchronous, so we initialize in an async method.
Future<void> initPlatformState() async {
var response = await RawContacts.getContacts(filter: _accountType);
setState(() {
_contacts = response;
});
// If the widget was removed from the tree while the asynchronous platform
// message was in flight, we want to discard the reply rather than calling
// setState to update our non-existent appearance.
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
key: _key,
appBar: AppBar(
title: const Text('Plugin example app'),
),
body: ListView(
children: _contacts != null
? _contacts
.map((test) => ListTile(
title: Text(test.displayName),
subtitle: Text(test.emails.elementAt(0).value),
))
.toList()
: <Widget>[Container()],
),
floatingActionButton: FloatingActionButton(
onPressed: () async {
var response = await RawContacts.createContact(
accountType: _accountType,
newContact: Contact(
displayName: "Test Bot",
emails: <KeyValuePair>[
KeyValuePair(
key: "main",
value: "jjoya@hub-games.com",
),
],
),
);
if (response) {
_key.currentState.showSnackBar(
SnackBar(
content: Text("Account Created!"),
duration: Duration(seconds: 1),
),
);
} else {
_key.currentState.showSnackBar(
SnackBar(
content: Text("Error"),
duration: Duration(seconds: 1),
backgroundColor: Colors.red,
),
);
}
},
child: Icon(Icons.person),
),
));
}
}