click static method
- If the widget is in the widget tree, use this method.
if the fileName is not set, it sets the file name as "davinci".
you can define whether to openFilePreview or returnImageUint8List
openFilePreview is true by default.
Implementation
static Future click(GlobalKey key,
{String fileName = "davinci",
bool openFilePreview = true,
double pixelRatio = 3.0,
bool returnImageUint8List = false}) async {
assert(openFilePreview != returnImageUint8List);
try {
/// finding the widget in the current context by the key.
RenderRepaintBoundary boundary =
key.currentContext!.findRenderObject() as RenderRepaintBoundary;
/// the boundary is converted to Image.
final image = await boundary.toImage(pixelRatio: pixelRatio);
/// The raw image is converted to byte data.
final byteData = await (image.toByteData(format: ui.ImageByteFormat.png));
/// The byteData is converted to uInt8List image aka memory Image.
final u8Image = byteData!.buffer.asUint8List();
if (returnImageUint8List) {
return u8Image;
}
if (openFilePreview) {
_openImagePreview(u8Image, fileName);
}
} catch (e) {
/// if the above process is failed, the error is printed.
print(e);
}
}