openPicker method

  1. @override
Future<List<HLPickerItem>> openPicker({
  1. List<String>? selectedIds,
  2. HLPickerOptions? pickerOptions,
  3. bool? cropping,
  4. HLCropOptions? cropOptions,
  5. LocalizedImagePicker? localized,
})

Select images or videos from a library

selectedIds: A list of string IDs representing the initially selected images or videos from the library. This allows users to pre-select items before opening the picker.

pickerOptions: Additional options for the picker: mediaType, maxSelectedAssets, maxFileSize,...

cropping: Indicating whether or not cropping is enabled. Just work when mediaType = MediaType.image

cropOptions: Configuration options for the cropping functionality: aspectRatio, aspectRatioPresets, compressQuality, compressFormat

localized: Custom text displayed for the plugin

Returns a list of HLPickerItem

Implementation

@override
Future<List<HLPickerItem>> openPicker({
  List<String>? selectedIds,
  HLPickerOptions? pickerOptions,
  bool? cropping,
  HLCropOptions? cropOptions,
  LocalizedImagePicker? localized,
}) async {
  double? cropCompressQuality = cropOptions?.compressQuality;
  assert(cropCompressQuality == null ||
      (cropCompressQuality > 0 && cropCompressQuality <= 1));

  double? thumbnailCompressQuality = pickerOptions?.thumbnailCompressQuality;
  assert(thumbnailCompressQuality == null ||
      (thumbnailCompressQuality > 0 && thumbnailCompressQuality <= 1));

  int? pickerWidth = pickerOptions?.maxSizeOutput?.maxWidth;
  assert(pickerWidth == null || pickerWidth >= 10);

  int? pickerHeight = pickerOptions?.maxSizeOutput?.maxHeight;
  assert(pickerHeight == null || pickerHeight >= 10);

  int? cropWidth = cropOptions?.maxSizeOutput?.maxWidth;
  assert(cropWidth == null || cropWidth >= 10);

  int? cropHeight = cropOptions?.maxSizeOutput?.maxHeight;
  assert(cropHeight == null || cropHeight >= 10);

  const defaultPresets = [
    CropAspectRatioPreset.original,
    CropAspectRatioPreset.square,
    CropAspectRatioPreset.ratio3x2,
    CropAspectRatioPreset.ratio4x3,
    CropAspectRatioPreset.ratio16x9
  ];

  final data = await methodChannel.invokeMethod('openPicker', {
    'mediaType': pickerOptions?.mediaType?.name,
    'maxSelectedAssets': pickerOptions?.maxSelectedAssets,
    'selectedIds': selectedIds,
    'maxFileSize': pickerOptions?.maxFileSize,
    'cropping': cropping,
    'isExportThumbnail': pickerOptions?.isExportThumbnail,
    'enablePreview': pickerOptions?.enablePreview,
    'ratioX': cropOptions?.aspectRatio?.ratioX,
    'ratioY': cropOptions?.aspectRatio?.ratioY,
    'aspectRatioPresets': (cropOptions?.aspectRatioPresets ?? defaultPresets)
        .map((e) => e.value)
        .toList(),
    'cropCompressQuality': cropOptions?.compressQuality,
    'cropCompressFormat': cropOptions?.compressFormat?.name,
    'croppingStyle': cropOptions?.croppingStyle?.name,
    'thumbnailCompressQuality': pickerOptions?.thumbnailCompressQuality,
    'thumbnailCompressFormat': pickerOptions?.thumbnailCompressFormat?.name,
    'maxDuration':
        pickerOptions?.maxDuration ?? pickerOptions?.recordVideoMaxSecond,
    'convertLivePhotosToJPG': pickerOptions?.convertLivePhotosToJPG,
    'convertHeicToJPG': pickerOptions?.convertHeicToJPG,
    'minSelectedAssets': pickerOptions?.minSelectedAssets,
    'minDuration': pickerOptions?.minDuration,
    'minFileSize': pickerOptions?.minFileSize,
    'numberOfColumn': pickerOptions?.numberOfColumn,
    'usedCameraButton': pickerOptions?.usedCameraButton,
    'cropMaxWidth': cropWidth,
    'cropMaxHeight': cropHeight,
    'isGif': pickerOptions?.isGif,
    'localized': localized?.toMap(),
    'maxWidth': pickerWidth,
    'maxHeight': pickerHeight,
    'compressQuality': pickerOptions?.compressQuality,
    'compressFormat': pickerOptions?.compressFormat?.name,
  });
  List<HLPickerItem> selectedItems = [];
  if (data != null) {
    selectedItems =
        (data as List).map((item) => HLPickerItem.fromMap(item)).toList();
  }
  return selectedItems;
}