Seth Ladd
Seth Ladd

Reputation: 120569

How can I dial the phone from Flutter?

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!

Upvotes: 60

Views: 64697

Answers (8)

Diraph
Diraph

Reputation: 327

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

Upvotes: 7

Chintan Shah
Chintan Shah

Reputation: 1764

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.

Upvotes: 8

Kapil Bansal
Kapil Bansal

Reputation: 354

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

Upvotes: 4

Swayamshree Mohanty
Swayamshree Mohanty

Reputation: 378

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

Upvotes: 6

Ray Zion
Ray Zion

Reputation: 678

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);
}

Upvotes: 3

Mohammed H. Hannoush
Mohammed H. Hannoush

Reputation: 581

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.

Upvotes: 9

Rajesh
Rajesh

Reputation: 4008

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';

Upvotes: 94

Buzzy
Buzzy

Reputation: 3677

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.

Upvotes: 38

Related Questions