How to Open Phone Dialer with Number in Flutter

In this example, we are going to show you how to open a phone call dialer with a number in Flutter. You can open the phone dialer directly from the contact pages of your app. This will make it easy for users to call to your services. 

First, add url_launcher package to your project by adding the following lines in pubspec.yaml file.

dependencies:
  flutter:
    sdk: flutter
  url_launcher: ^6.1.5

Now, import the package to the script:

import 'package:url_launcher/url_launcher.dart';

Uri phoneno = Uri.parse('tel:+97798345348734');
if (await launchUrl(phoneno)) {
    //dialer opened
}else{
    //dailer is not opened
}

Replace the number with your own number.

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(
      home: Home(),
    );
  }
}

class Home extends StatefulWidget{
  @override
  _HomeState createState() => _HomeState();
}

class _HomeState extends State<Home> { 

  @override
  Widget build(BuildContext context) { 
    return  Scaffold(
          appBar: AppBar(
            title: Text("Open Phone Dialer"),
            backgroundColor: Colors.redAccent,
          ),
          body: Container(
            padding: EdgeInsets.only(top:20, left:20, right:20),
            alignment: Alignment.topCenter,
            child: Column(
              children: [
                 ElevatedButton(
                  onPressed: ()async{
                      Uri phoneno = Uri.parse('tel:+97798345348734');
                      if (await launchUrl(phoneno)) {
                          //dialer opened
                      }else{
                          //dailer is not opened
                      }
                  }, 
                  child: Text("Call Us Now")
                )
            ],)
          )
       );
  }
}

In this way, you can open phone call dialer with number in Flutter. 

No any Comments on this Article


Please Wait...