Implementing Phone calls In Flutter apps
Flutter is really a fantastic tool to build mobile apps. Flutter is a free and open-source tool to develop mobile, desktop, web apps with a single code base. The technical architecture of flutter apps is a much easier, clean, and precise code structure. The community of flutter developer is constantly growing, helping us to provide a better experience and by developing various plugins. Plugins are great, help us to implement various functionality with lesser code and time.
The plugin page provides us an insight into the package quality. We must always prefer to use the package that has better maintenance, continuous improvement, good responses, and Flutter Favourite.
To implement the phone call functionality we need to use the bellow package:
url_launcher | Flutter Package
Flutter plugin for launching a URL on Android and iOS. Supports web, phone, SMS, and email schemes.pub.dev
flutter_phone_direct_caller | Flutter Package
A simple plugin to call a number directly from the app, without going to the phone dialer. Permission request is handled by…pub.dev
- url_launcher package will be used to implement phone call using the default app of your mobile phone. This package will auto direct the phone to the default phone call making app and open the dialer screen to make the call.
- flutter_phone_direct_caller package is not the best package but we will learn how to implement direct phone calls directly from our phone without the intermediate dialer.
Install the following dependencies inside your pubspec.yaml
file:
dependencies:
flutter:
sdk: flutter
cupertino_icons: ^latest version
flutter_phone_direct_caller: ^latest version
url_launcher: ^latest version
Making Phone Calls using the flutter_phone_direct_caller:
plugin
To implement the phone call using this plugin is really very easy. This package provides us FlutterPhoneDirectCaller
class that provides us callNumber()
method that takes a phone number.
_callNumber(String phoneNumber) async {
String number = phoneNumber;
await FlutterPhoneDirectCaller.callNumber(number);
}
RaisedButton:
RaisedButton(
child: Text("Call"),
onPressed: () {
_callNumber(textEditingController.text);
},
)
Making Phone Calls using the url_launcher:
plugin
This package provides us launch(URL)
method that takes an URL with schema. Scheme is very essential as it directs the URL. In the case of phone calls, we use ‘tel:’
schema.
_launchPhoneURL(String phoneNumber) async {
String url = 'tel:' + phoneNumber;
if (await canLaunch(url)) {
await launch(url);
} else {
throw 'Could not launch $url';
}
}
Here we have made a simple method that takes a String phoneNumber, this method will check whether the URL is correct or not if yes then the launch
method will launch it else it will throw an error.
Pass this method inside the onPressed
property of the above RaisedButton
RaisedButton(
child: Text("Call"),
onPressed: () {
_launchPhoneURL(textEditingController.text);
},
)
Full code:
import 'package:flutter/material.dart';
import 'package:flutter_phone_direct_caller/flutter_phone_direct_caller.dart';
import 'package:url_launcher/url_launcher.dart';
void main() {
runApp(MaterialApp(home: MyApp()));
}
class MyApp extends StatelessWidget {
TextEditingController textEditingController = TextEditingController();
@override
Widget build(BuildContext context) {
return Scaffold(
body: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Container(
child: TextFormField(
keyboardType: TextInputType.phone,
controller: textEditingController,
onSaved: (phoneNumber) {
textEditingController.text = phoneNumber;
},
),
),
RaisedButton(
child: Text("_launchPhoneURL"),
onPressed: () {
_launchPhoneURL(textEditingController.text);
},
),
RaisedButton(
child: Text("_callNumber"),
onPressed: () {
_callNumber(textEditingController.text);
},
)
],
),
);
}
}
_callNumber(String phoneNumber) async {
String number = phoneNumber;
await FlutterPhoneDirectCaller.callNumber(number);
}
_launchPhoneURL(String phoneNumber) async {
String url = 'tel:' + phoneNumber;
if (await canLaunch(url)) {
await launch(url);
} else {
throw 'Could not launch $url';
}
}
Demo:
Thanks for reading this article ❤
If I got something wrong? Let me know in the comments. I would love to improve.
Clap 👏 If this article helps you.
If we got something wrong? Let me know in the comments. we would love to improve.
FlutterDevs team of Flutter developers to build high-quality and functionally-rich apps. Hire a flutter developer for your cross-platform Flutter mobile app project on an hourly or full-time basis as per your requirement! You can connect with us on Facebook, GitHub, Twitter, and LinkedIn for any flutter related queries.
We welcome feedback and hope that you share what you’re working on using #FlutterDevs. We truly enjoy seeing how you use Flutter to build beautiful, interactive web experiences!.