getFromGallery function

Future<List<File>> getFromGallery(
  1. {List<File>? name,
  2. int selectedImageNo = 5,
  3. required BuildContext context}
)

Implementation

Future<List<File>> getFromGallery(
    {List<File>? name,
    int selectedImageNo = 5,
    required BuildContext context}) async {
  List<File> imageFiles = name.validate();

  bool checkPermission = await checkStrogePermission;

  if (checkPermission) {
    await getMultipleImageSource().then(
      (value) {
        if (imageFiles.validate().isNotEmpty) {
          value.forEach((element) {
            imageFiles.add(element);
          });
        } else {
          imageFiles = value;
        }
        if (imageFiles.length >= selectedImageNo) {
          imageFiles.removeRange(selectedImageNo, imageFiles.length);
          toast("Maximum ${selectedImageNo} Images you can add at a time.");
        }
      },
    );
  } else {
    showDialog(
      context: context,
      barrierDismissible: false,
      builder: (context) => AlertDialog(
        title: Text(
          "Go to Settings and grant permission to access photos and media on your device.",
          textAlign: TextAlign.center,
          style: primaryTextStyle(
            size: 16,
            weight: FontWeight.bold,
          ),
        ),
        actions: [
          CommonPrimaryButton(
            title: "Settings",
            onTap: () {
              finish(context);
              openAppSettings();
            },
          ),
          const SizedBox(height: 10.0),
          CommonPrimaryButton(
            title: "Cancel",
            onTap: () {
              finish(context);
            },
          )
        ],
      ),
    );
  }

  return imageFiles;
}