showImagePickerBottomSheet function

Future<Object?> showImagePickerBottomSheet({
  1. required bool cropImage,
  2. required bool useImagePicker,
  3. Color iconColor = Colors.grey,
  4. Color fontColor = Colors.black,
  5. EdgeInsetsGeometry padding = const EdgeInsets.symmetric(horizontal: marginX2),
  6. double modalFontSize = fontSizeL,
  7. File? image,
  8. CropAspectRatio? aspectRatio,
  9. String toolBarTitle = 'Crop Image',
  10. Color toolbarColor = Colors.grey,
  11. Color toolbarWidgetColor = Colors.white,
  12. bool hideBottomControls = true,
  13. bool lockAspectRatio = false,
  14. double? imageHeight,
  15. double? imageWidth,
  16. int? imageQuality,
  17. ResolutionPreset resolutionPreset = ResolutionPreset.medium,
  18. bool pickMultiple = false,
})

Implementation

Future<Object?> showImagePickerBottomSheet({
  required bool cropImage,
  required bool useImagePicker,
  Color iconColor = Colors.grey,
  Color fontColor = Colors.black,
  EdgeInsetsGeometry padding = const EdgeInsets.symmetric(horizontal: marginX2),
  double modalFontSize = fontSizeL,
  File? image,
  CropAspectRatio? aspectRatio,
  String toolBarTitle = 'Crop Image',
  Color toolbarColor = Colors.grey,
  Color toolbarWidgetColor = Colors.white,
  bool hideBottomControls = true,
  bool lockAspectRatio = false,
  double? imageHeight,
  double? imageWidth,
  int? imageQuality,
  ResolutionPreset resolutionPreset = ResolutionPreset.medium,
  bool pickMultiple = false,
}) async {
  if (cropImage) {
    File? result = await Get.bottomSheet(
      Container(
        color: Colors.white,
        child: SafeArea(
          child: Column(
            mainAxisSize: MainAxisSize.min,
            children: [
              ListTile(
                contentPadding: padding,
                leading: Icon(
                  Icons.camera_alt,
                  color: iconColor,
                ),
                title: FXText(
                  'กล้อง',
                  size: modalFontSize,
                  weight: FontWeight.bold,
                  color: fontColor,
                ),
                onTap: () async {
                  final cameras = await availableCameras();

                  File? file = await Get.to(
                    () => FXCameraPage(
                      cameras: cameras,
                      resolution: resolutionPreset,
                    ),
                  );

                  image = await cameraCrop(
                    file!.path,
                    aspectRatio: aspectRatio,
                    toolbarTitle: toolBarTitle,
                    toolbarColor: toolbarColor,
                    toolbarWidgetColor: toolbarWidgetColor,
                    hideBottomControls: hideBottomControls,
                    lockAspectRatio: lockAspectRatio,
                  );

                  Get.back(result: image);
                },
              ),
              const Divider(),
              ListTile(
                contentPadding: padding,
                leading: Icon(
                  Icons.photo,
                  color: iconColor,
                ),
                title: FXText(
                  'แกลลอรี่',
                  size: modalFontSize,
                  weight: FontWeight.bold,
                  color: fontColor,
                ),
                onTap: () async {
                  final file = await ImagePicker().pickImage(
                    source: ImageSource.gallery,
                    maxHeight: imageHeight,
                    maxWidth: imageWidth,
                    imageQuality: imageQuality,
                  );

                  image = await cameraCrop(
                    file!.path,
                    aspectRatio: aspectRatio,
                    toolbarTitle: toolBarTitle,
                    toolbarColor: toolbarColor,
                    toolbarWidgetColor: toolbarWidgetColor,
                    hideBottomControls: hideBottomControls,
                    lockAspectRatio: lockAspectRatio,
                  );

                  Get.back(result: image);
                },
              ),
            ],
          ),
        ),
      ),
    );

    return result;
  } else {
    ImagePickerSource? result = await Get.bottomSheet(
      Container(
        color: Colors.white,
        child: SafeArea(
          child: Column(
            mainAxisSize: MainAxisSize.min,
            children: [
              ListTile(
                contentPadding: padding,
                leading: Icon(
                  Icons.camera_alt,
                  color: iconColor,
                ),
                title: FXText(
                  'กล้อง',
                  size: modalFontSize,
                  weight: FontWeight.bold,
                  color: fontColor,
                ),
                onTap: () async {
                  Get.back(result: ImagePickerSource.camera);
                },
              ),
              const Divider(),
              ListTile(
                contentPadding: padding,
                leading: Icon(
                  Icons.photo,
                  color: iconColor,
                ),
                title: FXText(
                  'แกลลอรี่',
                  size: modalFontSize,
                  weight: FontWeight.bold,
                  color: fontColor,
                ),
                onTap: () async {
                  Get.back(result: ImagePickerSource.gallery);
                },
              ),
            ],
          ),
        ),
      ),
    );

    File? pickedFile;
    List<File> listPickedFile = [];

    if (result == ImagePickerSource.camera) {
      if (Platform.isAndroid) {
        if (useImagePicker) {
          pickedFile = await getImageFromCamera(
            buttonColor: iconColor,
            imageHeight: imageHeight,
            imageWidth: imageWidth,
            imageQuality: imageQuality,
          );

          if (pickMultiple) {
            listPickedFile.add(pickedFile!);
          }
        } else {
          final cameras = await availableCameras();
          pickedFile = await Get.to(
            () => FXCameraPage(
              cameras: cameras,
              resolution: resolutionPreset,
            ),
          );

          if (pickMultiple) {
            listPickedFile.add(pickedFile!);
          }
        }
      } else if (Platform.isIOS) {
        pickedFile = await getImageFromCamera(
          buttonColor: iconColor,
          imageHeight: imageHeight,
          imageWidth: imageWidth,
          imageQuality: imageQuality,
        );

        if (pickMultiple) {
          listPickedFile.add(pickedFile!);
        }
      }
    } else if (result == ImagePickerSource.gallery) {
      if (pickMultiple) {
        listPickedFile = await getImageFromGallery(
          buttonColor: iconColor,
          imageHeight: imageHeight,
          imageWidth: imageWidth,
          imageQuality: imageQuality,
          pickMultiple: pickMultiple,
        );
      } else {
        pickedFile = await getImageFromGallery(
          buttonColor: iconColor,
          imageHeight: imageHeight,
          imageWidth: imageWidth,
          imageQuality: imageQuality,
          pickMultiple: pickMultiple,
        );
      }
    }

    if (pickMultiple) {
      return listPickedFile;
    } else {
      return pickedFile;
    }
  }
}