单击按钮时打开应用程序 我想在单击按钮时打开 Gmail 应用程序。我正在使用 url 启动器

问题描述 投票:0回答:5

我想在单击按钮时打开 Gmail 应用程序。我正在使用 url 启动器。

         `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);
          }),`

当我单击此按钮时,它会打开网络而不是应用程序

flutter flutter-layout flutter-dependencies
5个回答
2
投票

对于 Android 使用

Andriod_intent_plus
,对于 iOS 使用
url_launcher
,您可以实现此目的。

if (Platform.isAndroid) {
   final AndroidIntent intent = AndroidIntent(
           action: 'android.intent.action.MAIN',
           category: 'android.intent.category.APP_EMAIL',
  );
  
  intent.launch().catchError((e) {});
} else if (Platform.isIOS) {
  launch('message://').catchError((e) {});
}

1
投票

你应该改变

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

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

1
投票

一个用于发送电子邮件的utils类,该类可用于打开whats应用程序、通话、消息等

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");
    }
  }
}

单击按钮即可从任何类调用该方法。

import 'utils.dart';

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

您需要在清单文件上提供 android 的查询。

<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 .............

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

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);
          }),

它的

_launch
功能:

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

0
投票

您可以在本机代码中使用此代码,而无需使用任何包:

在MainActivity.kt中

package com.example.you_app

import android.content.Intent
import android.os.Bundle
import android.view.View
import android.graphics.Color
import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugin.common.MethodChannel

class MainActivity : FlutterActivity() {
    private val CHANNEL = "com.example.you_app"

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        window.statusBarColor = Color.WHITE
        window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
    }

    override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
        super.configureFlutterEngine(flutterEngine)

        MethodChannel(flutterEngine.dartExecutor.binaryMessenger, CHANNEL)
            .setMethodCallHandler { call, result ->
                if (call.method == "openGmail") {
                    val success = openGmailApp()
                    if (success) {
                        result.success(null)
                    } else {
                        result.error("GMAIL_NOT_FOUND", "Gmail app is not installed", null)
                    }
                } else {
                    result.notImplemented()
                }
            }
    }

    private fun openGmailApp(): Boolean {
        return try {
            val intent = Intent(Intent.ACTION_MAIN)
            intent.addCategory(Intent.CATEGORY_APP_EMAIL)
            startActivity(intent)
            true
        } catch (anfe: android.content.ActivityNotFoundException) {
            false
        }
    }
}

在您的 Flutter 代码中:

class EmailVerificationPage extends StatelessWidget {
  const EmailVerificationPage({super.key});
  static const _platform = MethodChannel('com.example.you_app');

  Future<void> _openMailApp() async {
    try {
      await _platform.invokeMethod('openGmail');
    } on PlatformException catch (e) {
      debugPrint("Failed to open mail app: ${e.message}");
    }
  }

  @override
  Widget build(BuildContext context) {
    return YourButton(
       title: "OPEN EMAIL APP",
       backgroundColor: CustomColors.primaryRed,
       onPressed: _openMailApp,
    );
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.