// For logging purposes import 'dart:developer'; // Flutter UI framework import 'package:flutter/material.dart'; // For asynchronous programming import 'dart:async'; // For platform-specific services import 'package:flutter/services.dart'; // Plugin for making direct phone calls import 'package:direct_phone_call/direct_phone_call.dart'; void main() { // Entry point of the app runApp(const MyApp()); } // Main application widget class MyApp extends StatefulWidget { const MyApp({super.key}); @override State<MyApp> createState() => _MyAppState(); } // State class for MyApp class _MyAppState extends State<MyApp> { // Key for form validation final GlobalKey<FormState> _formKey = GlobalKey(); // Controller for phone number input final TextEditingController _phoneNoController = TextEditingController(); // Instance of the phone call plugin final _directPhoneCallPlugin = DirectPhoneCall(); @override void dispose() { super.dispose(); // Dispose resources _phoneNoController.dispose(); // Dispose the controller } // Method to make a phone call Future<void> _makeACall() async { // Dismiss the keyboard FocusScope.of(context).unfocus(); // Validate the form if (_formKey.currentState!.validate()) { bool isCalled = false; try { // Attempt to make the phone call isCalled = await _directPhoneCallPlugin.callNumber( number: _phoneNoController.text); } catch (e) { // Log any exceptions log('Exception : ${e}'); } } } @override Widget build(BuildContext context) { return MaterialApp( // Disable debug banner debugShowCheckedModeBanner: false, home: Scaffold( appBar: AppBar( title: const Text('Make a call'), backgroundColor: Colors.green, // App bar text color foregroundColor: Colors.white, ), body: Form( key: _formKey, // Assign form key child: Padding( padding: const EdgeInsets.all(16.0), child: Column( children: [ const SizedBox( height: 24, ), TextFormField( controller: _phoneNoController, // Assign controller style: const TextStyle( fontSize: 12, fontWeight: FontWeight.w500, ), validator: (value) { // Validation logic if (value == null || value.isEmpty) { // Error for empty input return 'Please fill phone number'; } if (value.isNotEmpty && value.length < 7) { // Error for invalid number return 'Invalid number'; } // Input is valid return null; }, keyboardType: TextInputType.phone, // Set keyboard type textInputAction: TextInputAction.done, // Set action button decoration: InputDecoration( fillColor: Colors.white, // Input field background color hintText: 'Phone number', // Placeholder text hintStyle: const TextStyle( color: Colors.black26, fontSize: 12, fontWeight: FontWeight.w400, ), prefixIcon: Icon( Icons.local_phone_rounded, color: Colors.green.shade800, size: 18, ), // Phone icon contentPadding: EdgeInsets.zero, // Remove padding border: OutlineInputBorder( borderRadius: BorderRadius.circular(10), // Rounded border 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.green, width: 1.5, ), ), ), onFieldSubmitted: (val) => _makeACall(), // Call method on submit ), const SizedBox( height: 12, ), // Add vertical spacing ElevatedButton( onPressed: () => _makeACall(), // Call method on button press style: ButtonStyle( backgroundColor: WidgetStatePropertyAll( Colors.green.shade900, ), // Button background color minimumSize: const WidgetStatePropertyAll( Size(100, 35), ), // Button size ), child: Container( child: const Text( 'Call', // Button text style: TextStyle( color: Colors.white, // Text color fontSize: 13, // Text size ), ), ), ), ], ), ), ), ), ); } }