50

I am building a Flutter app, and I'd like to dial a phone number in response to a button tap. What's the best way to do this?

Thanks!

Seth Ladd
  • 112,095
  • 66
  • 196
  • 279

8 Answers8

86

This method will open the dialer :

_launchCaller() async {
    const url = "tel:1234567";   
    if (await canLaunch(url)) {
       await launch(url);
    } else {
      throw 'Could not launch $url';
    }   
}

EDIT:

In case anybody facing errors:

Add url_launcher: in the pubspec.yaml & run flutter get

Also import 'package:url_launcher/url_launcher.dart';

Augustin R
  • 7,089
  • 3
  • 26
  • 54
Rajesh
  • 3,562
  • 7
  • 24
  • 43
  • 2
    This should be the correct & accepted answer. This is working perfectly fine. Same reference was found at : https://pub.dartlang.org/packages/url_launcher – Pawan Feb 12 '19 at 16:21
  • Don't forget to add queries/permissions. Look through "Configuration" section. https://pub.dev/documentation/url_launcher/latest/ – Galti May 05 '22 at 10:19
37

Typically, to interact with the underlying platform, you have to write platform specific code and communicate with the same using platform channels. However, Flutter provides some points of integration with the platform out of the box. To dial the phone for instance, you can use the UrlLauncher.launch API with the tel scheme to dial the phone.

Something like UrlLauncher.launch("tel://<phone_number>"); should work fine on all platforms.

Do note that this will not work in the simulators. So make sure you are using an actual device to test this.

Buzzy
  • 3,618
  • 4
  • 30
  • 41
  • 1
    Is more low-level phone activity possible too? I'm thinking about creating a full replacement dialer app and I'm wondering whether it could be done in flutter. – retorquere May 08 '17 at 21:54
  • 4
    Link to `UrlLauncher` does not work anymore, the function seems to have been moved to a new package: https://pub.dartlang.org/packages/url_launcher – Renato May 04 '18 at 16:59
  • 4
    "Do note that this will not work in the simulators." - thank you very much! – DmitryKanunnikoff Dec 30 '18 at 15:06
  • Don't forget to add queries/permissions. Look through "Configuration" section. https://pub.dev/documentation/url_launcher/latest/ – Galti May 05 '22 at 10:19
9

You can use the url_launcher widget (https://pub.dev/packages/url_launcher)

  1. Add this to your package's pubspec.yaml file: dependencies: url_launcher: ^5.7.10

  2. Install it: $ flutter pub get

  3. Import it import 'package:url_launcher/url_launcher.dart';

  4. Inside your class, define this method so you can call from any action in your code:

     Future<void> _makePhoneCall(String url) async {
     if (await canLaunch(url)) {
       await launch(url);
     } else {
       throw 'Could not launch $url';
     }
    

    }

  5. inside the build widget:

     IconButton(icon: new Icon(Icons.phone),
        onPressed: () 
        {
           setState(() {
              _makePhoneCall('tel:0597924917');
           });
        },
      ),
    

Note 1: you should write the phone number with prefix 'tel': 'tel:0123456789'

Note 2: sometimes it will not work well until you close the app in your mobile and reopen it, so flutter can inject the code of the new widget successfully.

7

Use url_launcher.

import 'package:url_launcher/url_launcher.dart';
    
openDialPad(String phoneNumber) async {
    Uri url = Uri(scheme: "tel", path: phoneNumber);
    if (await canLaunchUrl(url)) {
      await launchUrl(url);
    } else {
       print("Can't open dial pad.");
    }
}

Note: Don't forgot to update info.plist and AndroidManifest.xml as per the documentation.

Chintan Shah
  • 1,744
  • 2
  • 26
  • 28
4

Follow the below steps:

  1. add the url_launcher: Latest version dependency in your pubspec.yaml

  2. Go to \android\app\src\main\AndroidManifest.xml.

  3. add the queries lines before the <application, like this :

    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
         package="com.example.flutter">
         <queries>
             <intent>
                 <action android:name="android.intent.action.DIAL" />
                 <data android:scheme="tel" />
             </intent>
         </queries>
        <application ...
    

For more queries, follow this more queries

4.Add the dialer function, and set the url, final url ="tel:$phoneNumber"; like this:

  Future<void> dialNumber(
      {required String phoneNumber, required BuildContext context}) async {
    final url = "tel:$phoneNumber";
    if (await canLaunch(url)) {
      await launch(url);
    } else {
      ShowSnackBar.showSnackBar(context, "Unable to call $phoneNumber");
    }

    return;
  }
  1. Done
3

Its too easy

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

void main() {
  runApp(Scaffold(
    body: Center(
      child: RaisedButton(
        onPressed: _callNumber,
        child: Text('Call Number'),
      ),
    ),
  ));
}

_callNumber() async{
  const number = '08592119XXXX'; //set the number here
  bool res = await FlutterPhoneDirectCaller.callNumber(number);
}
Ray Zion
  • 610
  • 10
  • 11
3

URL launcher has updated their dependency so here is the updated way to do the job. it will work for android ios

final Uri launchUri = Uri(
  scheme: 'tel',
  path: number,
);
await launchUrl(launchUri);

Who has not used this plugin just imoprt this plugin.

UrlLauncher

Kapil Bansal
  • 326
  • 2
  • 10
2

This is what worked for me as of January 2023

NB: The major difference from the other answers above is my #4

ALSO: You can now use test this in Emulator

Steps:

1.Add url_launcher: latest to pubspec.yaml

2.Configuration:

For IOS: Add tel scheme passed to canLaunchUrl as LSApplicationQueriesSchemes entries in your Info.plist file.

Like this:

<key>LSApplicationQueriesSchemes</key>
<array>
  <string>tel</string>
</array>

For Android: Add tel scheme passed to canLaunchUrl as queries entries in your AndroidManifest.xml [ie \android\app\src\main\AndroidManifest.xml] file.

Just like this:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.flutApp">
     <queries>
         <intent>
             <action android:name="android.intent.action.VIEW" />
             <data android:scheme="tel" />
         </intent>
     </queries>
   <application

3.Import url_launcher to your file: ie import 'package:url_launcher/url_launcher.dart';

4.Code

Future<void> _dialNumber(String phoneNumber) async {
final Uri launchUri = Uri(
scheme: 'tel',
path: phoneNumber,
);
await launchUrl(launchUri);
}

Source: url_launcher

Diraph
  • 147
  • 8