contacts_service 0.4.6 contacts_service: ^0.4.6 copied to clipboard
A Flutter plugin to retrieve and manage contacts on Android and iOS devices.
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:contacts_service_example/contacts_list_page.dart';
import 'package:contacts_service_example/contacts_picker_page.dart';
void main() => runApp(ContactsExampleApp());
// iOS only: Localized labels language setting is equal to CFBundleDevelopmentRegion value (Info.plist) of the iOS project
// Set iOSLocalizedLabels=false if you always want english labels whatever is the CFBundleDevelopmentRegion value.
const iOSLocalizedLabels = false;
class ContactsExampleApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: HomePage(),
routes: <String, WidgetBuilder>{
'/add': (BuildContext context) => AddContactPage(),
'/contactsList': (BuildContext context) => ContactListPage(),
'/nativeContactPicker': (BuildContext context) => ContactPickerPage(),
},
);
}
}
class HomePage extends StatefulWidget {
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
@override
void initState() {
super.initState();
_askPermissions();
}
Future<void> _askPermissions() async {
PermissionStatus permissionStatus = await _getContactPermission();
if (permissionStatus != PermissionStatus.granted) {
_handleInvalidPermissions(permissionStatus);
}
}
Future<PermissionStatus> _getContactPermission() async {
PermissionStatus permission = await PermissionHandler()
.checkPermissionStatus(PermissionGroup.contacts);
if (permission != PermissionStatus.granted &&
permission != PermissionStatus.disabled) {
Map<PermissionGroup, PermissionStatus> permissionStatus =
await PermissionHandler()
.requestPermissions([PermissionGroup.contacts]);
return permissionStatus[PermissionGroup.contacts] ??
PermissionStatus.unknown;
} else {
return permission;
}
}
void _handleInvalidPermissions(PermissionStatus permissionStatus) {
if (permissionStatus == PermissionStatus.denied) {
throw PlatformException(
code: "PERMISSION_DENIED",
message: "Access to location data denied",
details: null);
} else if (permissionStatus == PermissionStatus.disabled) {
throw PlatformException(
code: "PERMISSION_DISABLED",
message: "Location data is not available on device",
details: null);
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Contacts Plugin Example')),
body: SafeArea(
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
RaisedButton(
child: const Text('Contacts list'),
onPressed: () => Navigator.pushNamed(context, '/contactsList'),
),
RaisedButton(
child: const Text('Native Contacts picker'),
onPressed: () =>
Navigator.pushNamed(context, '/nativeContactPicker'),
),
],
),
),
);
}
}