How to Open Native Contact Book to Pick Phone Number in Flutter

In this example, we are going to show you the way to open native contact or address book and pick single contact from the list. These kinds of features are required while making utility apps related to contacts, SMS, phone number dialer. See the example below and learn to pick single contact from the native contact book.

First, add contact_picker Flutter package in your dependency by adding following lines in pubspec.yaml file. 

dependencies:
  flutter:
    sdk: flutter
  contact_picker: ^0.0.2

Import contact_picker Flutter package in your script:

import 'package:contact_picker/contact_picker.dart';

//init ContactPicker
final ContactPicker contactPicker = new ContactPicker();

//open contact picker
Contact contact = await contactPicker.selectContact();

//get phone number with label
String phone = contact.phoneNumber.toString();

//get phone number only
String number = contact.phoneNumber.number;

//get name only
String name = contact.fullName;

import 'package:contact_picker/contact_picker.dart';
import 'package:flutter/material.dart';
void main() {
  runApp(MyApp());
}

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

class _MyAppState extends State<MyApp> {

  String number, name;
  final ContactPicker contactPicker = new ContactPicker();

  @override
  void initState() {
    number = "";
    name = "";
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        title: "Test App",
        home: Scaffold( 
            appBar: AppBar( 
              title: Text("Single Contact Picker"),
              backgroundColor: Colors.redAccent
            ),
            body: Container( 
                  padding: EdgeInsets.all(20),
                   color: Colors.purple.shade50,
                   child: Column(children: [
                       Text("Phone Number: $number"),
                       Text("Name: $name"),
                       Divider(),
                       Container(
                         child: ElevatedButton( 
                           onPressed: () async {
                             Contact contact = await contactPicker.selectContact();
                             if(contact != null){
                                number = contact.phoneNumber.number;
                                name = contact.fullName;
                                setState(() {
                                  
                                });
                             }
                           },
                           child: Text("Pick Contact"),
                         ),
                       )
                   ],),
                ),
            )
    );
  }
}

App UI part Native Contact Book

In this way, you can make single contact picker from native address book in Flutter App.

1 Commet on this Article

Bdjs

Nullsafety version ? 

2 years ago


Please Wait...