How to make phone calls and send SMS via your Flutter app

How to make phone calls and send SMS via your Flutter app

Ayush Pawar
·Feb 25, 2022·

3 min read

Subscribe to my newsletter and never miss my upcoming articles

Play this article

Table of contents

  • Introduction
  • Project Setup
  • Coding the Logic
  • Conclusion

Introduction

In today's article, we will learn how to send SMS and make Phone calls directly via your Flutter app. This is a small but very useful feature that a lot of apps have in them. It is very easy to implement in Flutter but a lot of people don't know how to do it. So today, I will show you how to do it in a very simple manner.

Project Setup

To achieve our target, we will use a package called URL_launcher. URL launcher is a package that lets us open different URLs directly from our app. Now, you may think that phone calls and text messages are not any URLs. You are right, but the URL launcher package can also open your file directories and SMS and phone app. So cool, isn't it?

Add the dependency in your pubspec file like this.

dependencies:
  url_launcher: ^6.0.20

Now, import the package in the main.dart file or wherever you are coding your logic.

import 'package:url_launcher/url_launcher.dart';

We will also need to add a few lines of configuration code in our AndroidManifest.xml file. You can find the AndroidManifest.xml file in project/android/app/src/main/

<manifest>
 <application>
// already present code
 </application>
// add these lines
 <queries>
  <!-- If your app opens https URLs -->
  <intent>
    <action android:name="android.intent.action.VIEW" />
    <data android:scheme="https" />
  </intent>
  <!-- If your app makes calls -->
  <intent>
    <action android:name="android.intent.action.DIAL" />
    <data android:scheme="tel" />
  </intent>
  <!-- If your sends SMS messages -->
  <intent>
    <action android:name="android.intent.action.SENDTO" />
    <data android:scheme="smsto" />
  </intent>
  <!-- If your app sends emails -->
  <intent>
    <action android:name="android.intent.action.SEND" />
    <data android:mimeType="*/*" />
  </intent>
 </queries>
</manifest>

With this, we are done with our project setup. Now, let's code the logic.

Coding the Logic

First of all, create two text buttons. One should be to make a call and another should be to send a message. You can also go ahead and create a text field to let the user input the mobile number. I won't be doing that. I will just use a pre-defined phone number and SMS.

In the onPressed function of that button, we will add just one line of code which will do the magic. This is how your whole button and the onPressed would look like

TextButton(
onPressed:()async{
// if we are unable to launch the phone app, we will throw an error. This is what the below line means. launch is an in-built function provided by url_launcher
 if (!await launch('tel:+919876543210')) throw 'Could not launch phone app';
},
child: Text('Call'),)

Create an exact same button for the SMS feature and replace the 'tel' with 'sms'. This is what your code will look like.

TextButton(
onPressed:()async{
// if we are unable to launch the phone app, we will throw an error. This is what the below line means. launch is an in-built function provided by url_launcher.
 if (!await launch('sms:+919876543210')) throw 'Could not launch message app';
},
child: Text('SMS'),)

Now, when you tap the 'Call' button, you will be redirected to your dial pad with the phone number auto-filled. If you tap on the 'SMS' button, you will be redirected to your messaging app.

If you were successfully redirected, then Congratulations! you have learned how to make SMS and phone calls via your Flutter app. If not, then make sure you are adding the properties correctly in the manifest file.

If you are a visual learner, then check out this video tutorial on the same.

Conclusion

With this, we conclude our tutorial on how to make phone calls and SMS via your Flutter app. This was a very easy process and I hope you were successful in it. You can tell me in the comments if you want an article on some specific topic.

You can appreciate and support my blogs via. bmcoffee.png

Also, let's connect on Twitter. Follow CSwithIyush for more amazing tutorials, tips/tricks on Flutter & DSA.

Did you find this article valuable?

Support Ayush Pawar by becoming a sponsor. Any amount is appreciated!

See recent sponsors Learn more about Hashnode Sponsors
 
Share this