generateImageFromGlobalKey static method

Future<ImageElement?> generateImageFromGlobalKey(
  1. GlobalKey<State<StatefulWidget>> globalKey
)

to capture widget to image by GlobalKey in RenderRepaintBoundary

Implementation

static Future<ImageElement?> generateImageFromGlobalKey(wid.GlobalKey globalKey) async {
  try {
    /// boundary widget by GlobalKey
    rend.RenderRepaintBoundary? boundary = globalKey.currentContext?.findRenderObject() as rend.RenderRepaintBoundary?;

    /// convert boundary to image
    final image = await boundary!.toImage();

    /// set ImageByteFormat
    final data = (await image.toByteData(format: ui.ImageByteFormat.rawRgba))?.buffer.asUint8List();
    return data == null?null:ImageElement(
      width: image.width,
      height: image.height,
      data: Uint8Array.fromList(data)
    );

  } catch (e) {
    rethrow;
  }
}