direct_phone_call 1.0.0 direct_phone_call: ^1.0.0 copied to clipboard
Flutter plugin to call a number directly, without going to phone dialer, from app
example/lib/main.dart
import 'dart:developer';
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:flutter/services.dart';
import 'package:direct_phone_call/direct_phone_call.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
final GlobalKey<FormState> _formKey = GlobalKey();
final TextEditingController _phoneNoController = TextEditingController();
final _directPhoneCallPlugin = DirectPhoneCall();
@override
void initState() {
super.initState();
}
@override
void dispose() {
super.dispose();
_phoneNoController.dispose();
}
// Platform messages are asynchronous, so we initialize in an async method.
Future<void> _makeACall() async {
FocusScope.of(context).unfocus();
if(_formKey.currentState!.validate()) {
bool isCalled = false;
try {
isCalled = await _directPhoneCallPlugin.callNumber(
number: _phoneNoController.text);
}
catch (e) {
log('Exception : ${e}');
}
}
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Make a call'),
),
body: Container(
height: 200,
decoration: BoxDecoration(
color: Colors.blue.withAlpha(10),
borderRadius: BorderRadius.circular(30),
),
padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 16,),
margin: const EdgeInsets.symmetric(horizontal: 12, vertical: 16,),
child: Form(
key: _formKey,
child: Column(
children: [
const SizedBox(height: 24,),
TextFormField(
controller: _phoneNoController,
style: const TextStyle(
fontSize: 12,
fontWeight: FontWeight.w500,
),
validator: (value){
if(value == null || value.isEmpty){
return 'Please fill phone number';
}
if(value.isNotEmpty && value.length < 7){
return 'Invalid number';
}
return null;
},
keyboardType: TextInputType.phone,
textInputAction: TextInputAction.done,
decoration: InputDecoration(
fillColor: Colors.white,
hintText: 'Phone number',
hintStyle: const TextStyle(
color: Colors.black26,
fontSize: 12,
fontWeight: FontWeight.w400,
),
prefixIcon: Icon(Icons.local_phone_rounded, color: Colors.green.shade800, size: 18,),
contentPadding: EdgeInsets.zero,
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(10),
borderSide: const BorderSide(color: Colors.black38,),
),
enabledBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(10),
borderSide: const BorderSide(color: Colors.black38,),
),
focusedBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(10),
borderSide: const BorderSide(color: Colors.blueAccent, width: 1.5,),
),
errorBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(10),
borderSide: BorderSide(color: Colors.red.shade800,),
),
focusedErrorBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(10),
borderSide: BorderSide(color: Colors.red.shade800, width: 1.5,),
),
),
onFieldSubmitted: (val) => _makeACall(),
),
const SizedBox(height: 12,),
ElevatedButton(
onPressed: () => _makeACall(),
style: ButtonStyle(
backgroundColor: WidgetStatePropertyAll(Colors.green.shade900,),
minimumSize: const WidgetStatePropertyAll(Size(100, 35),),
),
child: Container(
child: const Text('Call',
style: TextStyle(
color: Colors.white,
fontSize: 13,
),
),
),
),
],
),
),
),
),
);
}
}