getScreenShot static method

Future<void> getScreenShot(
  1. RenderViewModel viewModel,
  2. VoltronArray array,
  3. Promise? promise
)

Implementation

static Future<void> getScreenShot(RenderViewModel viewModel, VoltronArray array, Promise? promise) async {
  if (promise == null) {
    return;
  }

  /// find rootKey
  final globalKey = viewModel.context.getInstance(viewModel.rootId)?.rootKey;
  final renderObject = globalKey?.currentContext?.findRenderObject();
  if (globalKey == null) {
    LogUtils.e(kTag, "getScreenShot globalKey == null");
    return;
  }

  /// catch screenshot and encode base64
  final boundary = renderObject as RenderRepaintBoundary;
  final image = await boundary.toImage();
  final byteData = await image.toByteData(format: ImageByteFormat.png);
  final pngByteList = byteData?.buffer.asUint8List();
  final base64Content = pngByteList == null ? "" : base64Encode(pngByteList);

  /// promise callback
  final resultMap = VoltronMap();
  resultMap.push(kScreenShot, base64Content);

  final mediaQuery = MediaQueryData.fromWindow(window);
  final deviceWidth = mediaQuery.size.width.round();
  final deviceHeight = mediaQuery.size.height.round();
  resultMap.push(kScreenWidth, deviceWidth);
  resultMap.push(kScreenHeight, deviceHeight);
  resultMap.push(kScreenScale, 1.0);
  promise.resolve(resultMap);
}