Home > Software design >  Open app when button is clicked I want to open the Gmail app when I click the button. I am using the
Open app when button is clicked I want to open the Gmail app when I click the button. I am using the

Time:05-10

I want to open the Gmail app when I click the button. I am using the url launcher.

         `InkWell(
          child: Row(mainAxisSize: MainAxisSize.min, children: const [
            SizedBox(
              width: 30.0,
              height: 60.0,
            ),
            Text(' "/Open Email/" ',
                style: TextStyle(
                  color: Colors.black,
                )),
          ]),
          onTap: () {
            const url = 'https://mail.google.com/mail/u/0/#inbox';
            launchURL(url);
          }),`

When I click this its open the web instead of the app

CodePudding user response:

A utils class for sending email, this class can be used for opening whats app, call, message, etc.

import 'package:url_launcher/url_launcher.dart' as UL;

class Utils {
  static Future<void> sendEmail(
      {String email, String subject = "", String body = ""}) async {
    String mail = "mailto:$email?subject=$subject&body=${Uri.encodeFull(body)}";
    if (await UL.canLaunch(mail)) {
      await UL.launch(mail);
    } else {
      throw Exception("Unable to open the email");
    }
  }
}

Call the method from any class with a click of a button.

import 'utils.dart';

void onOpenMailClicked() async {
  try {
       await Utils.sendEmail(
       email: "[email protected]",
       subject: "Optional",
       );
       } catch (e) {
        debugPrint("sendEmail failed ${e}");
       }
}

You need to provide queries for android on the manifest file.

<manifest 
  xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.example.example">
       
  <queries>
    <intent>
      <action android:name="android.intent.action.SEND" />
      <data android:mimeType="image/jpeg" />
      </intent>
  </queries>
   <application .............

CodePudding user response:

Uri gmailUrl = Uri.parse('mailto:[email protected]?subject=Greetings&body=Hello World');

InkWell(
          child: Row(mainAxisSize: MainAxisSize.min, children: const [
            SizedBox(
              width: 30.0,
              height: 60.0,
            ),
            Text(' "/Open Email/" ',
                style: TextStyle(
                  color: Colors.black,
                )),
          ]),
          onTap: () {
            _launch(gmailUrl);
          }),

and it’s _launch function:

    Future<void> _launch(Uri url) async {
    await canLaunchUrl(url)
        ? await launchUrl(url)
        : _showSnackBar('could_not_launch_this_app'.tr());
      }

CodePudding user response:

you should change

const url = 'https://mail.google.com/mail/u/0/#inbox';

to

const url = 'mailto:${your_receiver_email}';
  • Related