selectImageFromGallery static method

Future<CropParameters?> selectImageFromGallery({
  1. required BuildContext context,
  2. required Product product,
  3. required ImageField imageField,
  4. required OpenFoodFactsLanguage language,
  5. required bool isLoggedInMandatory,
})

Implementation

static Future<CropParameters?> selectImageFromGallery({
  required BuildContext context,
  required Product product,
  required ImageField imageField,
  required OpenFoodFactsLanguage language,
  required bool isLoggedInMandatory,
}) async {
  final AppLocalizations appLocalizations = AppLocalizations.of(context);
  if (!await ProductRefresher().checkIfLoggedIn(
    context,
    isLoggedInMandatory: isLoggedInMandatory,
  )) {
    return null;
  }

  if (!context.mounted) {
    return null;
  }

  List<ProductImage> rawImages = getRawProductImages(
    product,
    ImageSize.DISPLAY,
  );
  if (rawImages.isNotEmpty) {
    return _openGallery(
      context: context,
      rawImages: rawImages,
      productType: product.productType,
      barcode: product.barcode!,
      imageField: imageField,
      language: language,
      isLoggedInMandatory: isLoggedInMandatory,
    );
  }

  final bool fetched = await ProductRefresher().fetchAndRefresh(
    barcode: product.barcode!,
    context: context,
  );
  if (!fetched) {
    return null;
  }

  if (!context.mounted) {
    return null;
  }

  final Product? latestProduct = await DaoProduct(
    context.read<LocalDatabase>(),
  ).get(product.barcode!);
  if (!context.mounted) {
    return null;
  }
  if (latestProduct != null) {
    // very likely
    rawImages = getRawProductImages(latestProduct, ImageSize.DISPLAY);
  }

  if (rawImages.isEmpty) {
    await showDialog<void>(
      context: context,
      builder: (BuildContext context) => SmoothAlertDialog(
        body: Text(
          appLocalizations.edit_photo_select_existing_downloaded_none,
        ),
        actionsAxis: Axis.vertical,
        positiveAction: SmoothActionButton(
          text: appLocalizations.okay,
          onPressed: () => Navigator.of(context).pop(),
        ),
      ),
    );
    return null;
  }
  return _openGallery(
    context: context,
    rawImages: rawImages,
    productType: product.productType,
    barcode: product.barcode!,
    imageField: imageField,
    language: language,
    isLoggedInMandatory: isLoggedInMandatory,
  );
}