contactEmail function

Future<void> contactEmail({
  1. required BuildContext context,
  2. required Uri emailUrl,
  3. required String subject,
  4. required String body,
})

Launch the user's email client with the given emailUrl in the to field.

The subject and body of the email will also be pre-populated with the corresponding fields.

Implementation

Future<void> contactEmail({
  required BuildContext context,
  required Uri emailUrl,
  required String subject,
  required String body,
}) async {
  final container = ProviderScope.containerOf(context, listen: false);

  final appName = await container.read(appNameProvider.future);
  final version = await container.read(completeVersionProvider.future);

  final contactUrl = emailUrl.replace(
    query: encodeQueryParameters(<String, String>{
      'subject': subject,
      'body': '$body\n\n$appName\n$version',
    }),
  );

  if (await canLaunchUrl(contactUrl)) {
    await launchUrl(contactUrl);
  } else {
    if (!context.mounted) return;

    await showDialog(
      context: context,
      builder: (context) => AlertDialog(
        content: Text.rich(
          TextSpan(
            children: [
              TextSpan(text: context.sharedL10n.contactUsAt),
              const TextSpan(text: ' '),
              TextSpan(
                text: contactUrl.path,
                style: const TextStyle(fontWeight: FontWeight.w700),
              ),
            ],
          ),
        ),
        actions: [
          TextButton(
            onPressed: () => Navigator.pop(context),
            child: Text(MaterialLocalizations.of(context).closeButtonLabel),
          ),
        ],
      ),
    );
  }
}