You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter实现点击按钮选择邮件应用发送邮件的方案咨询

实现Flutter邮件应用选择功能的方案

你可以通过两种方式实现点击按钮后显示邮件应用列表供用户选择:


方案一:利用系统自带应用选择器(Android优先)

Android系统中,若希望强制唤起应用选择器(即使已设置默认邮件应用),可以使用flutter_intent包发送带有选择器的Intent:

  1. 添加依赖到pubspec.yaml:
dependencies:
  flutter_intent: ^2.0.0
  1. 修改发送邮件的代码:
import 'package:flutter_intent/flutter_intent.dart';

Future<void> sendEmailWithChooser(BuildContext context) async {
  try {
    final intent = FlutterIntent(
      action: 'android.intent.action.SENDTO',
      data: 'mailto:$mailvar',
      // 可选:添加邮件主题和内容
      arguments: {
        'android.intent.extra.SUBJECT': '你的邮件主题',
        'android.intent.extra.TEXT': '你的邮件内容',
      },
    );
    // 唤起系统选择器,指定提示文本
    await intent.launchChooser('选择邮件应用');
  } catch (e) {
    if (context.mounted) {
      showSnackBar(
        context: context,
        type: SnackBarType.error
      );
    }
  }
}

注:此方案仅支持Android;iOS系统中,使用url_launcher打开mailto链接时,若存在多个邮件应用,系统会自动弹出选择器,无需额外配置。


方案二:自定义应用选择界面(跨平台)

如果需要跨平台统一的选择界面,可以使用device_apps包获取所有支持mailto协议的应用,再通过对话框让用户选择:

  1. 添加依赖到pubspec.yaml:
dependencies:
  device_apps: ^2.2.0
  1. 实现获取邮件应用列表和选择逻辑:
import 'package:device_apps/device_apps.dart';

// 获取所有支持mailto协议的应用
Future<List<Application>> getEmailApps() async {
  final apps = await DeviceApps.getAppsForScheme('mailto');
  // 过滤出带图标应用,提升UI体验
  return apps.where((app) => app is ApplicationWithIcon).toList().cast<Application>();
}

// 弹出选择对话框并启动选中应用
Future<void> showEmailAppChooser(BuildContext context) async {
  final emailApps = await getEmailApps();
  
  if (emailApps.isEmpty) {
    if (context.mounted) {
      showSnackBar(
        context: context,
        type: SnackBarType.error
      );
    }
    return;
  }

  if (context.mounted) {
    final selectedApp = await showDialog<Application>(
      context: context,
      builder: (context) => AlertDialog(
        title: const Text('选择邮件应用'),
        content: SingleChildScrollView(
          child: ListBody(
            children: emailApps.map((app) {
              return ListTile(
                leading: app is ApplicationWithIcon
                    ? Image.memory(app.icon, width: 40, height: 40)
                    : const Icon(Icons.email),
                title: Text(app.appName),
                onTap: () => Navigator.pop(context, app),
              );
            }).toList(),
          ),
        ),
      ),
    );

    if (selectedApp != null) {
      // 启动选中应用并打开邮件界面
      await DeviceApps.openApp(selectedApp.packageName, uri: 'mailto:$mailvar');
    }
  }
}

iOS额外配置

在Info.plist中添加以下配置,允许查询应用:

<key>LSApplicationQueriesSchemes</key>
<array>
  <string>mailto</string>
</array>

内容的提问来源于stack exchange,提问作者HAYT

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.05 04:10:08