desktopImagePicker function

Future<Uint8List?> desktopImagePicker()

Implementation

Future<Uint8List?> desktopImagePicker() async {
  AtSignLogger atSignLogger = AtSignLogger('desktopImagePicker');
  try {
    // ignore: omit_local_variable_types
    const XTypeGroup typeGroup = XTypeGroup(
      label: 'images',
      extensions: ['jpg', 'png', 'jpeg'],
    );
    final List<XFile> files = await openFiles(acceptedTypeGroups: [typeGroup]);
    if (files.isEmpty) {
      return null;
    }
    // ignore: omit_local_variable_types
    final XFile file = files[0];

    final input = ImageFile(
      rawBytes: await file.readAsBytes(),
      filePath: file.path,
    );

    final output = compress(ImageFileConfiguration(
        input: input,
        config: const Configuration(
            jpgQuality: 50,
            pngCompression: PngCompression.defaultCompression,
            outputType: OutputType.jpg)));

    if (output.sizeInBytes > 100000) {
      CustomToast().show('image size cannot exceed 100 kb',
          NavService.groupPckgLeftHalfNavKey.currentContext!);
      return null;
    }

    return output.rawBytes;
  } catch (e) {
    atSignLogger.severe('Error in desktopImagePicker $e');
    return null;
  }
}