capture method

Future<File?> capture(
  1. GlobalKey<State<StatefulWidget>>? key
)

Implementation

Future<File?> capture(GlobalKey? key) async {
  if (key == null) return null;

  RenderRepaintBoundary? boundary = key.currentContext?.findRenderObject() as RenderRepaintBoundary?;
  final image = await boundary?.toImage(pixelRatio: 90);
  final byteData = await image?.toByteData(format: ui.ImageByteFormat.png);

  var randomtext = getRandomString(10);

  final file = File('${(await getTemporaryDirectory()).path}/profile${key.hashCode}-${randomtext}.PNG');
  await file.writeAsBytes(byteData!.buffer.asUint8List(byteData.offsetInBytes, byteData.lengthInBytes));
  return file;
}