0

i am working on a program with flutter desktop, that screenshots a widget, and saves it to the desktop. i have been able to take the screenshot, my issue is how to save the screenshot to my desktop using path_provider package

Here is a code i wrote to get the path

 Future<String> getFilePath() async {
Directory appDocumentsDirectory =
    await getApplicationDocumentsDirectory(); // 1
String appDocumentsPath = appDocumentsDirectory.path; // 2
String filePath = '$appDocumentsPath/image.png'; // 3

return filePath;
}  

Here is a code for the screenshot logic, i wrapped my main code in a Screenshot widget

  ElevatedButton(
              onPressed: () {
               
                screenshotController
                    .capture(delay: Duration(milliseconds: 10))
                    .then((capturedImage) async {
                  ShowCapturedWidget(context, capturedImage!);
                }).catchError((onError) {
                  print(onError);
                });
              },
              child: Text(_message)),
        ],
      ),
    ),
  ),
);
}

Future<dynamic> ShowCapturedWidget(
  BuildContext context, Uint8List capturedImage) {
return showDialog(
  useSafeArea: false,
  context: context,
  builder: (context) => Scaffold(
    appBar: AppBar(
      title: Text("Captured widget screenshot"),
    ),
    body: Center(
        child: capturedImage != null
            ? Image.memory(capturedImage)
            : Container()),
  ),
);
}

How do i save the capturedImage to my desktop, or how do i send it automatically as a mail.

  • Have a look at this [thread](https://stackoverflow.com/questions/68181858/how-to-save-file-selected-from-filepicker-windows-in-disk-flutter-desktop) – princesanjivy Sep 24 '21 at 12:59
  • There are two questions here. How to send as attachment to email needs clarity; which pub packages to use. I have answered both with some assumptions below. – Nathaniel Johnson Sep 24 '21 at 13:34

1 Answers1

0

Assuming you are using the package screenshot and flutter_email_sender

if (capturedImage != null) {
  final imagePath = await File(await getFilePath()).create();
  await imagePath.writeAsBytes(capturedImage);
  final Email email = Email(
    body: 'Email body',
    subject: 'Email subject',
    recipients: ['example@example.com'],
    cc: ['cc@example.com'],
    bcc: ['bcc@example.com'],
    attachmentPaths: [imagePath],
    isHTML: false,
  );

  await FlutterEmailSender.send(email);
}
Nathaniel Johnson
  • 4,731
  • 1
  • 42
  • 69