takeAndSaveScreenShot method

Future<void> takeAndSaveScreenShot({
  1. required BuildContext context,
  2. required GlobalKey<State<StatefulWidget>> repaintBoundaryKey,
  3. required String filename,
  4. required String imageToSaveCustomDirectoryName,
  5. required dynamic onScreenShotSavedCallback(
    1. ScreenShotHelperModel? screenShotHelperModel
    ),
  6. String? androidSystemDirectoryPath,
  7. double? pixelRatio,
})

takes the screen shot and then save context is required repaintBoundaryKey is the Global key that you will have to assign to the repaint boundary widget filename is the name of the file that want to save the screen shot with imageToSaveCustomDirectoryName the custom directory inside the system directory | for creating a separate directory to save this image in androidSystemDirectoryPath the system directory | for now only for Android, default is downloads directory for Android and for iOS is documents directory onScreenShotSavedCallback is the call back with the model populated with the data about the method success, permission result and save success! pixelRatio for screen ratio, default is 1

Implementation

Future<void> takeAndSaveScreenShot(
    {required BuildContext context,
    required GlobalKey repaintBoundaryKey,
    required String filename,
    required String imageToSaveCustomDirectoryName,
    required Function(ScreenShotHelperModel? screenShotHelperModel)
        onScreenShotSavedCallback,
    String? androidSystemDirectoryPath,
    double? pixelRatio}) async {
  if (repaintBoundaryKey.currentContext != null &&
      repaintBoundaryKey.currentContext!.findRenderObject() != null) {
    RenderRepaintBoundary boundary = repaintBoundaryKey.currentContext!
        .findRenderObject() as RenderRepaintBoundary;
    final image = await boundary.toImage(pixelRatio: pixelRatio ?? 1);
    final byteData = await image.toByteData(format: ui.ImageByteFormat.png);

    Uint8List pngBytes;
    if (byteData != null) {
      pngBytes = byteData.buffer.asUint8List();
      PermissionHelperUtil.instance.checkIfStoragePermissionGranted().then(
          (PermissionHandlerHelperModel? permissionHandlerHelperModel) async {
        if (permissionHandlerHelperModel != null) {
          if (permissionHandlerHelperModel.permissionsResult ==
              PermissionsResultsEnums.granted) {
            String newPath = await createDirectory(
                context: context,
                androidDirectoryPath: androidSystemDirectoryPath);
            var directory =
                Directory('$newPath/$imageToSaveCustomDirectoryName');
            directory.exists().then((bool isExists) {
              if (isExists) {
                final imagePath = "${directory.path}/$filename";
                File(imagePath).writeAsBytes(pngBytes).then((File file) {
                  file.exists().then((bool exists) {
                    if (exists) {
                      onScreenShotSavedCallback(ScreenShotHelperModel(
                          saveSuccess: true,
                          savedImagePath: imagePath,
                          permissionsResultsEnum:
                              PermissionsResultsEnums.granted,
                          errorReason: null));
                    } else {
                      onScreenShotSavedCallback(ScreenShotHelperModel(
                          saveSuccess: false,
                          savedImagePath: null,
                          permissionsResultsEnum:
                              PermissionsResultsEnums.granted,
                          errorReason: "Error Code 32199"));
                    }
                  });
                }, onError: (e) {
                  onScreenShotSavedCallback(ScreenShotHelperModel(
                      saveSuccess: false,
                      savedImagePath: null,
                      permissionsResultsEnum: PermissionsResultsEnums.granted,
                      errorReason: "Error Code 0120:::$e"));
                });
              } else {
                directory
                    .create(recursive: true)
                    .then((Directory createdDirectory) {
                  final imagePath = "${createdDirectory.path}/$filename";
                  File(imagePath).writeAsBytes(pngBytes).then((value) {
                    onScreenShotSavedCallback(ScreenShotHelperModel(
                        savedImagePath: imagePath,
                        permissionsResultsEnum:
                            PermissionsResultsEnums.granted,
                        errorReason: null,
                        saveSuccess: true));
                  }, onError: (e) {
                    onScreenShotSavedCallback(ScreenShotHelperModel(
                        saveSuccess: false,
                        savedImagePath: null,
                        permissionsResultsEnum:
                            PermissionsResultsEnums.granted,
                        errorReason: "Error Code 0122:::$e"));
                  });
                });
              }
            });
          } else if (permissionHandlerHelperModel.permissionsResult ==
              PermissionsResultsEnums.denied) {
            onScreenShotSavedCallback(ScreenShotHelperModel(
                saveSuccess: false,
                savedImagePath: null,
                permissionsResultsEnum: PermissionsResultsEnums.denied,
                errorReason: "Permission is denied!"));
          } else if (permissionHandlerHelperModel.permissionsResult ==
              PermissionsResultsEnums.permanentlyDenied) {
            onScreenShotSavedCallback(ScreenShotHelperModel(
                saveSuccess: false,
                savedImagePath: null,
                permissionsResultsEnum:
                    PermissionsResultsEnums.permanentlyDenied,
                errorReason: "Permission is permanently denied!"));
          }
        } else {
          onScreenShotSavedCallback(ScreenShotHelperModel(
              saveSuccess: false,
              savedImagePath: null,
              permissionsResultsEnum: null,
              errorReason: "Permission helper model is null!"));
        }
      });
    } else {
      onScreenShotSavedCallback(ScreenShotHelperModel(
          saveSuccess: false,
          savedImagePath: null,
          permissionsResultsEnum: null,
          errorReason: "Image is null, Error Code 0123"));
    }
  } else {
    onScreenShotSavedCallback(ScreenShotHelperModel(
        saveSuccess: false,
        savedImagePath: null,
        permissionsResultsEnum: null,
        errorReason:
            "Repaint Boundary/Current Context/Render Object is null, Error Code 0124"));
  }
}