getImageFromGallery function

dynamic getImageFromGallery({
  1. required Color buttonColor,
  2. bool pickMultiple = false,
  3. double? imageHeight,
  4. double? imageWidth,
  5. int? imageQuality,
})

Implementation

getImageFromGallery({
  required Color buttonColor,
  bool pickMultiple = false,
  double? imageHeight,
  double? imageWidth,
  int? imageQuality,
}) async {
  bool galleryEnabled = await fxCanAccessGallery();
  File? file;
  List<File> fileList = [];

  if (galleryEnabled) {
    final imagePicker = ImagePicker();

    if (pickMultiple) {
      final pickedFile = await imagePicker.pickMultiImage(
        maxHeight: imageHeight,
        maxWidth: imageWidth,
        imageQuality: imageQuality,
      );

      if (pickedFile.isNotEmpty) {
        for (XFile data in pickedFile) {
          file = File(data.path);
          fileList.add(file);
        }
      }
    } else {
      final pickedFile = await imagePicker.pickImage(
        source: ImageSource.gallery,
        maxHeight: imageHeight,
        maxWidth: imageWidth,
        imageQuality: imageQuality,
      );

      if (pickedFile != null) {
        file = File(pickedFile.path);
      }
    }
  } else {
    Get.dialog(
      FXAlertDialog(
        title: 'กรุณาตรวจสอบสิทธิ์ในการเข้าแกลลอรี่',
        buttonColor: buttonColor,
      ),
    );
  }

  if (pickMultiple) {
    return fileList;
  } else {
    return file;
  }
}