Home > Mobile >  How to call a function from ElevatedButton onPressed - Flutter
How to call a function from ElevatedButton onPressed - Flutter

Time:10-06

I'm new to flutter, and I'm tried to add a call function and email function to Elevated Button. When someone clicks the call button, the call should go to the phone number on it. And when the email button is clicked, there should be an opportunity to send the email.

Here goes my code file:

 Center(
              child: Row(
                mainAxisAlignment: MainAxisAlignment.center,
                crossAxisAlignment: CrossAxisAlignment.center,
                children: [
                  Padding(
                    padding: const EdgeInsets.all(8.0),
                    child: SizedBox(
                      width: 164,
                      height: 105,
                      child: ElevatedButton(
                          onPressed: () {},
                          style: ElevatedButton.styleFrom(
                            primary: const Color.fromRGBO(189, 31, 45, 1),
                            onPrimary: Colors.grey,
                            shape: RoundedRectangleBorder(
                              borderRadius: BorderRadius.circular(13),
                            ),
                          ),
                          child: Column(
                              mainAxisAlignment: MainAxisAlignment.center,
                              crossAxisAlignment: CrossAxisAlignment.center,
                              children: <Widget>[
                                Padding(
                                  padding: const EdgeInsets.all(8.0),
                                  child: Image.asset(
                                    'assets/images/phone-call.png',
                                    width: 39,
                                    height: 39,
                                  ),
                                ),
                                const Text(" 94112802471",
                                    style: TextStyle(
                                        color: Colors.white,
                                        fontSize: 13,
                                        fontFamily: 'VisbyCF'))
                              ])),
                    ),
                  ),
                  Padding(
                    padding: const EdgeInsets.all(18),
                    child: SizedBox(
                      width: 164,
                      height: 105,
                      child: ElevatedButton(
                          onPressed: () {},
                          style: ElevatedButton.styleFrom(
                            primary: Color.fromARGB(255, 245, 245, 245),
                            onPrimary: const Color.fromRGBO(189, 31, 45, 1),
                            shape: RoundedRectangleBorder(
                              borderRadius: BorderRadius.circular(12),
                            ),
                          ),
                          child: Column(
                              mainAxisAlignment: MainAxisAlignment.center,
                              crossAxisAlignment: CrossAxisAlignment.center,
                              children: <Widget>[
                                Padding(
                                  padding: const EdgeInsets.all(5.0),
                                  child: Image.asset(
                                    'assets/images/Layer 2.png',
                                    width: 39,
                                    height: 39,
                                  ),
                                ),
                                const Text("[email protected]",
                                    style: TextStyle(
                                        color:
                                            Color.fromARGB(227, 146, 146, 146),
                                        fontSize: 13,
                                        fontFamily: 'VisbyCF'))
                              ])),
                    ),
                  ),
                ],
              ),
            ),

here I have used two elevated buttons, the call button, and the email button

CodePudding user response:

With url_launcher package you can achieve this:

for open call do this:

onPressed: () async{

    final Uri launchUri = Uri(
      scheme: 'tel',
      path:' 94112802471',
    );
    if (!await launchUrl(launchUri)) {
       throw 'Could not launch $launchUri';
    }
},

for send email do this:

onPressed: () async{
    
 final Uri params = Uri(
   scheme: 'mailto',
   path: '[email protected]',
   query: '...', // <--- add subject and body here
 );

 var url = params.toString();
 if (!await launchUrl(url)) {
   throw 'Could not launch $url';
 }
},

Not: don't forget the configurations that mentions in package website.

  • Related