'How to make a phone call from a flutter app
I try to make a phone call from my Flutter app. With the following code:
UrlLauncher.launch('tel: xxxxxxxx');
I found this Function on the GitHub flutter repo: https://github.com/flutter/flutter/issues/4856
But this doesn't work for me. Is this Function still in Flutter and in which package? Or is there a better option to do a phone call from my app?
Solution 1:[1]
Call the launch
method from url_launcher package:
launch("tel://214324234");
Here's the complete code:
import 'package:flutter/material.dart';
import 'package:url_launcher/url_launcher.dart';
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return new MaterialApp(
title: 'Flutter Demo',
home: new Home(),
);
}
}
class Home extends StatelessWidget {
Home({Key key}) : super(key: key);
@override
Widget build(BuildContext context) => new Scaffold(
appBar: new AppBar(
title: new Text("View"),
),
body: new Center(
child: new FlatButton(
onPressed: () => launch("tel://21213123123"),
child: new Text("Call me")),
),
);
}
void main() {
runApp(
new MyApp(),
);
}
Also you can import it and then use
import 'package:url_launcher/url_launcher.dart' as UrlLauncher;
UrlLauncher.launch("tel://21213123123")
Be sure to include an entry for it in the pubspec.yaml file, in the dependencies section:
url_launcher: ^1.0.2
Solution 2:[2]
You should add this in your pubspec.yaml => url_launcher: ^5.0.2 then you click Packages get .
in your code you add the import : import 'package:url_launcher/url_launcher.dart' as UrlLauncher; Hope it works =)
import 'package:url_launcher/url_launcher.dart' as UrlLauncher;
UrlLauncher.launch('tel:+${p.phone.toString()}')
//if mail
UrlLauncher.launch('mailto:${p.email}'),
Solution 3:[3]
If you are not getting any action on the click of the button, So this is happening because of this. So this is happening because you might not have add tel:// before the number.
Do it this way
Full code is given below
launch(('tel://${mobile_no}')); //launch(('tel://99999xxxxx'));
1) in pubspec.yaml
dependencies:
flutter:
sdk: flutter
url_launcher: ^5.4.10
2) Import wherever you want to use
import 'package:url_launcher/url_launcher.dart';
3) final you call
onPressed: () {
launch(('tel://${item.mobile_no}'));
},
Solution 4:[4]
This worked for me
use this plugin
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:flutter/services.dart';
import 'package:flutter_phone_direct_caller/flutter_phone_direct_caller.dart';
void main() => runApp(new MyApp());
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => new _MyAppState();
}
class _MyAppState extends State<MyApp> {
TextEditingController _numberCtrl = new TextEditingController();
@override
void initState() {
super.initState();
_numberCtrl.text = "085921191121";
}
@override
Widget build(BuildContext context) {
return new MaterialApp(
home: new Scaffold(
appBar: new AppBar(
title: const Text('Plugin example app'),
),
body: new Column(
children:<Widget>[
Padding(
padding: EdgeInsets.all(8.0),
child: TextField(
controller: _numberCtrl,
decoration: InputDecoration(
labelText: "Phone Number"
),
keyboardType: TextInputType.number,
),
),
RaisedButton(
child: Text("Test Call"),
onPressed: () async{
FlutterPhoneDirectCaller.callNumber(_numberCtrl.text);
},
)
]
),
),
);
}
}
Solution 5:[5]
I am able to make a phone call by bringing up the system phone app and CONNECT A CALL:
Here's what you need to do:
pubspec.yaml add package:
intent:
main.dart:
import 'package:flutter/material.dart';
import 'package:intent/intent.dart' as android_intent;
import 'package:intent/action.dart' as android_action;
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return (Scaffold(
body: Center(
child: RaisedButton(
onPressed: _launchURL,
child: Text('Dial a number'),
)
),
));
}
}
_launchURL() async {
// Replace 12345678 with your tel. no.
android_intent.Intent()
..setAction(android_action.Action.ACTION_CALL)
..setData(Uri(scheme: "tel", path: "12345678"))
..startActivity().catchError((e) => print(e));
}
Then, after running this app and click the "Dial a number", the System Phone App will bring up and make a call. (Unlike url_launcher, you don't need to press the Green Call button in the System Phone App)
Solution 6:[6]
url_launcher is universal package for launching url, dialing number and sending mail.
- Add
url_launcher: ^5.5.2
to pubspec.yaml file and runflutter pub get
- Import package
import 'package:url_launcher/url_launcher.dart';
- Define function:
void launchUrl(String url) async {
if (await canLaunch(url)) {
launch(url);
} else {
throw "Could not launch $url";
}
}
- Call your universal function for different purposes:
//for launching url
launchUrl("HTTP://example.com");
// for dial phone number
launchUrl("tel:+99364921507");
// for sending email
launchUrl("mailto:[email protected]?subject=Meeting&body=Can we meet via Google Meet");
Solution 7:[7]
To launch the device's dialer, the following code can also be used with exception handling:
Future<void> launchPhoneDialer(String contactNumber) async {
final Uri _phoneUri = Uri(
scheme: "tel",
path: contactNumber
);
try {
if (await canLaunch(_phoneUri.toString()))
await launch(_phoneUri.toString());
} catch (error) {
throw("Cannot dial");
}
}
Solution 8:[8]
Just url_launcher: ^ latest Version
in Pubspec.yamal
Note: Before Pub get or Upgrade delete Pubspec.lock some time it gives unwanted problems.
Import package import 'package:url_launcher/url_launcher.dart';
//for launching URL
launchUrl("HTTP://website.com");
// for dial phone number
launchUrl("tel:+91963852741");
// for sending email
launchUrl("mailto:[email protected]?subject=Meeting&body=Can we meet via Google Meet");
Solution 9:[9]
If you using url_launcher and your phone number have plus symbol like "+1111111111" for ios you should use Uri class
final Uri phoneUrl = Uri(
scheme: 'tel',
path: '+11111111111',
);
if (await canLaunch(phoneUrl.toString())) {
await launch(phoneUrl.toString());
} else {
throw "Can't phone that number.";
}
Solution 10:[10]
You can call directly by this package flutter_phone_direct_caller
Create a function and pass the mobile number value :
_callNumber(String mobile) async {
await FlutterPhoneDirectCaller.callNumber(mobile);
}
Solution 11:[11]
You can do it by two ways:-
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 can 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: ^1.0.0
flutter_phone_direct_caller: ^1.0.1
url_launcher: ^5.7.10
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);
}
You can implement it in your button as follows
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';
}
}
and to raised button:-
RaisedButton(
child: Text("Call"),
onPressed: () {
_launchPhoneURL(textEditingController.text);
},
)
IF you need advanced features you can use "flutter_voip_kit", it is very new library, I have also not used it but it looks promising at first galance.. ?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow