Make Call in Flutter

In the event that you are developing an application, you may need to include a button that allows you to make direct phone. when user add this functionality in your application to call direct of other user of Mobile application


Step-by-Step Implementation

Step 1: Create a New Project in Android Studio (File >new flutter project).


Step 2: Adding material package

Import method the runApp method in the main function call first while run the application.

 import 'package:flutter/material.dart';

 void main() {

     runApp(RunMyApp());

 }

Step 3: Creating a stateless widget 

 We can create a stateless widget that contains MaterialApp widget,AppBar,etc.

 class RunMyApp extends StatelessWidget {

     const RunMyApp({super.key});  

    @override

    Widget build(BuildContext context) {

    return MaterialApp(home);

     }

 }

Step 4: Add a package in flutter dependencies. What you need to do is place it under the dependencies: inside the pubspec.yaml file.
 dependencies:
flutter:
sdk: flutter
url_launcher:
Step 5: Below code write when doing the call of other in a flutter.

 makingPhoneCall()

    async {

    var url = Uri.parse("tel:1111111111");
   if (await canLaunchUrl(url)) {
   await launchUrl(url);
   } else {
   throw 'Could not launch $url';
   }
 }

Step 6: Final code 
 import 'package:flutter/material.dart';
import 'package:untitled/screen/Background.dart';
import 'package:url_launcher/url_launcher.dart';

void main() {
runApp(HomeScreencall());
}

_makingPhoneCall() async {
var url = Uri.parse("tel:1111111111");
if (await canLaunchUrl(url)) {
await launchUrl(url);
} else {
throw 'Could not launch $url';
}
}

class HomeScreencall extends StatelessWidget {
const HomeScreencall({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
// AppBar
body: SafeArea(
child: Center(
child: Column(
children: [
Container(
height: 50.0,
)
,Container(child: Image.asset(
"assets/images/company_logo.png",
width:100
),)
,Container(
height: 50.0,
),//Container
const Text(
'Welcome to \n CandidRoot Solutions!',
textAlign: TextAlign.center,
style: TextStyle(
fontSize: 20.0,
color: Colors.blue,
fontWeight: FontWeight.bold,
),
),//Text
Container(
height: 20.0,
),

Container(
height: 20.0,
),

ElevatedButton(
onPressed: _makingPhoneCall,

style: ButtonStyle(
padding: MaterialStateProperty.all(const EdgeInsets.all(10.0)),
textStyle: MaterialStateProperty.all(
const TextStyle(color: Colors.black),
),
),
child: const Text('Click to call'),
), // ElevatedButton


],
),
),
),
),
);
}
}

 

Step 7: Output of above example 


Happy coding!

365Bloggy June 11, 2024
Share this post
Tags
SUBSCRIBE THIS FORM


Archive