How can I dial the phone from Flutter?
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';
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.
You can use the url_launcher widget (https://pub.dev/packages/url_launcher)
Add this to your package's pubspec.yaml file:
dependencies: url_launcher: ^5.7.10
Install it:
$ flutter pub get
Import it
import 'package:url_launcher/url_launcher.dart';
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'; }
}
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.