Rami Dhouib
Rami Dhouib

Reputation: 11

Use flutter share_plus to open share sheet and prefill email

I want to be able to open share sheet (with all share options) and when I click email. it is prefilled.target email should be prefilled.

static Future<void> exportFile({
    required BuildContext context,
    required String filename,
    required List<int> bytes,
    String? text,
    String? subject
  }) async {
    // This box is for sharePositionOrigin. This is the part that is needed for iPad. It does not affect any other devices.
    final box = context.findRenderObject() as RenderBox?;

    // Generate a String with the path.
    final directory = await getTemporaryDirectory();
    String filePath = '${directory.path}/$filename';

    // Generate the file.
    final file = File(filePath);

    // Let the callback write data to the file.
    await file.writeAsBytes(bytes, flush: true);

    // Share the newly created file. The values that are passed to parameters "text" and "subject" are
    // used in emails.
    await Share.shareXFiles(
        [XFile(file.path)],
        text: text,
        subject: subject,
        sharePositionOrigin: box!.localToGlobal(Offset.zero) & box.size
    );
  }

This does not prefill Target email , but prefills only subejct and text. Any idea?

Upvotes: 1

Views: 178

Answers (1)

Dev Adnani
Dev Adnani

Reputation: 86

Try This

String mailtoLink = 'mailto:$email?subject=${Uri.encodeComponent(subject ?? '')}&body=${Uri.encodeComponent(text ?? '')}';

await Share.shareXFiles(
        [XFile(file.path)],

        text: mailtoLink,
        subject: subject,
        sharePositionOrigin: box!.localToGlobal(Offset.zero) & box.size
    );

Upvotes: 0

Related Questions