copyImage method
Copies the image located at imagePath
to the clipboard.
For web platform, imagePath
should be a URL.
For other platforms, imagePath
should be a local file path.
Implementation
@override
Future<void> copyImage(String imagePath) async {
// Read the image from the URL and convert it to a byte array
final response = await html.HttpRequest.request(imagePath,
responseType: 'blob', method: 'GET', mimeType: 'image/png');
final reader = html.FileReader();
final completer = Completer<String>();
reader.onLoadEnd.listen((event) {
completer.complete(reader.result as String);
});
reader.readAsDataUrl(response.response as html.Blob);
final base64String = await completer.future;
// Get the Base64 encoded byte array
final base64Data = base64String.split(',').last;
final byteArray = base64Decode(base64Data);
// Convert the Base64 data to a Blob
html.Blob blob = html.Blob([byteArray], 'image/png');
// Define the clipboardCopy function
clipboardCopy(html.Blob imageBlob) {
final clipboardItemCtor = js.context['ClipboardItem'];
final clipboardItem = js.JsObject(clipboardItemCtor, [
js.JsObject.jsify({'image/png': imageBlob})
]);
js.context['navigator']['clipboard']
.callMethod('write', [js.JsArray()..add(clipboardItem)]);
}
// Call the clipboardCopy function
clipboardCopy(blob);
}