capture function

Future capture({
  1. String? fileName,
})

Implementation

Future capture({String? fileName}) async {
  const _chars = 'AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz1234567890';
  Random _rnd = Random();

  String getRandomString(int length) => String.fromCharCodes(Iterable.generate(
      length, (_) => _chars.codeUnitAt(_rnd.nextInt(_chars.length))));

  if(fileName == null){
    fileName = getRandomString(15);
  }

  RenderRepaintBoundary? boundary = globalKey.currentContext!.findRenderObject() as RenderRepaintBoundary?;
  final image = await boundary!.toImage(pixelRatio: 3);
  final byteData = await image.toByteData(format: ui.ImageByteFormat.png);
  final pngBytes = byteData!.buffer.asUint8List();

  final directory = await getApplicationDocumentsDirectory();
  final pathOfImage = await File('${directory.path}/${fileName}.png').create();
  await pathOfImage.writeAsBytes(pngBytes);
  return pathOfImage; }