3

I have a text field in which user enters a number and a Raisedbutton.By clicking that button user should be able to make call on that number . How to write onPressed() function for that buton??

Above this code I have my main class in which Home() class is getting called

import 'package:flutter/material.dart';

class Home extends StatefulWidget{
  @override
  State<StatefulWidget> createState() {
    return HomeState();
  }
}
class HomeState extends State<Home> {
  TextEditingController numcontroller = new TextEditingController();

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      home: Scaffold(
        appBar: AppBar(
          centerTitle: true,
          backgroundColor: Colors.deepPurple,
          title: Text('Calling App'),
        ),
        body: Column(
          children: <Widget>[
            Container(
              margin: EdgeInsets.only(top: 60.0, left: 10.0, right: 10.0),
              child: TextField(
                  controller: numcontroller,
                  decoration: InputDecoration(
                      labelText: 'Enter Phone Number',
                      border: OutlineInputBorder(
                          borderSide: BorderSide(
                            color: Colors.deepPurple,
                          )
                      )
                  )
              ),
            ),
            Container(
              height: 45.0,
              width: 90.0,
              margin: EdgeInsets.only(top: 40, left: 10, right: 10.0),
              child: RaisedButton(
                  color: Colors.deepPurple,
                  elevation: 7.0,
                  child: Text(
                    'Call',
                    style: TextStyle(
                      color: Colors.white,
                      fontSize: 20.0,
                    ),
                  ),
                  onPressed: () {
                    _calling();
                  }
              ),
            )
          ],
        ),
      ),
    );
  }

  void _calling(){
  }
}

suppose I have entered a number 94********.then on pressing that button the call should be connected to this number .If busy or switch off then should show any alert message.

LorenzOliveto
  • 7,796
  • 1
  • 20
  • 47
user11706645
  • 31
  • 1
  • 2

2 Answers2

1

First import the URL launcher dependency

  url_launcher: ^5.2.5

Then your calling function should look like this

_calling() async {
const url = 'tel:+12345678';
if (await canLaunch(url)) {
  await launch(url);
} else {
  throw 'Could not launch $url';
}

}

Ilo Calistus
  • 2,005
  • 3
  • 19
  • 23
0

if you want to make actually a phone call from the application then you need CALL_PHONE permission that is marked as a sensitive permission and your app will not be compatible with google play policy.

you can open the phone app instead, first install url_launcher in pubspec.yaml

dependencies:
  url_launcher: ^5.0.3

then

void _calling(){
    _launchURL(numcontroller.text);
}


_launchURL(String phone) async {

  const url = 'tel:$phone';
  if (await canLaunch(url)) {
    await launch(url);
  } else {
    throw 'Could not launch $url';
  }
}
Sami Kanafani
  • 14,244
  • 6
  • 45
  • 41