How to make a phone call from a flutter app
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);
},
)
]
),
),
);
}
}
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}'),
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");
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