How To Make A Phone Call From Flutter Android App

admin_img Posted By Bajarangi soft , Posted On 02-11-2020

Depending on the type of app you are developing, you might want the user to quickly make a call or send a message or email by pressing a button in your app. In order to do all the above mentioned we’re going to need to import a single library which is called url_launcher, you might have used it already to launch websites on safari or chrome from your flutter app.

How To Make A Phone Call From Flutter Android App

Phone Call From
Step 1:  We cannot directly remove the time stamp from Phone Call From but using the intl.dart package we can easily filter the date stamp from time stamp. So open your flutter project’s pubspec.yaml in code

dependencies:
  flutter:
    sdk: flutter
  url_launcher: ^4.1.0
 

Step 2: After done saving the pubspec.yaml file, Open your flutter project root folder in Command Prompt or Terminal and execute flutter pub get command. 

flutter pub get

Step 3: Open your project’s main.dart file and import material.dart and url_launcher: ^4.1.0. dart package.

import 'package:url_launcher/url_launcher.dart';


Complete Code For  Phone Call From In Flutter
main.dart
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(
      debugShowCheckedModeBanner: false,
      title: 'Flutter Demo',
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  final String phone = 'tel:+2347012345678';

  _callPhone() async {
    if (await canLaunch(phone)) {
      await launch(phone);
    } else {
      throw 'Could not Call Phone';
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
          backgroundColor: Colors.orange[900],
          title: Text('Call Phone from App'
          )),
      body: Center(
          child: RaisedButton(
            onPressed: _callPhone,
            child: Text('Phone Call',style: TextStyle(color: Colors.white),),
            color: Colors.orange[900],
          )),
    );
  }
}

Related Post