How to Open Phone Dialer with Number in Flutter
In today’s world, mobile devices are used by almost everyone for communication and other purposes. In mobile applications, it is important to have a feature to make phone calls from the application itself. In this blog post, we will discuss how to open the phone dialer with a specific number in a Flutter application.
How to Open Phone Dialer with Number in Flutter
Flutter is a popular framework for building mobile applications with a single codebase for both iOS and Android. It has a rich set of APIs that can be used to build complex applications easily.
To open the phone dialer with a specific number in a Flutter application, we need to use the url_launcher
package. This package provides a simple way to launch URLs and make phone calls directly from the application.
How to Open Phone Dialer with Number in Flutter
Let’s get started with a simple example.
- First, we need to add the
url_launcher
package to the dependencies in thepubspec.yaml
file.
dependencies: url_launcher: ^6.0.3
- After adding the package, we need to import it in our Dart file.
import 'package:url_launcher/url_launcher.dart';
- To make a phone call, we can use the
launch
method provided by theurl_launcher
package. This method takes a string parameter that represents the phone number.
void makePhoneCall() async { const phoneNumber = 'tel:+1234567890'; if (await canLaunch(phoneNumber)) { await launch(phoneNumber); } else { throw 'Could not launch $phoneNumber'; } }
In the above code, we have created a function named makePhoneCall
that uses the launch
method to open the phone dialer with the specified phone number. The tel:
prefix is used to indicate that the string represents a phone number.
- Finally, we can call the
makePhoneCall
function in our Flutter application.
RaisedButton( onPressed: () { makePhoneCall(); }, child: Text('Make a Phone Call'), )
The complete code should look like this:
How to Open Phone Dialer with Number in Flutter
import 'package:flutter/material.dart'; import 'package:url_launcher/url_launcher.dart'; void main() => runApp(MyApp()); class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( title: 'Flutter Phone Dialer Example', home: Scaffold( appBar: AppBar( title: Text('Flutter Phone Dialer Example'), ), body: Center( child: RaisedButton( onPressed: () { makePhoneCall(); }, child: Text('Make a Phone Call'), ), ), ), ); } } void makePhoneCall() async { const phoneNumber = 'tel:+1234567890'; if (await canLaunch(phoneNumber)) { await launch(phoneNumber); } else { throw 'Could not launch $phoneNumber'; } }
In the above code, we have created a RaisedButton
widget that calls the makePhoneCall
function when pressed.
That’s it! Now when the user presses the button, the phone dialer will open with the specified phone number.
In conclusion, opening the phone dialer with a specific number in a Flutter application is a simple task that can be accomplished using the url_launcher
package. By following the steps outlined in this blog post, you can easily implement this feature in your Flutter application.