usePhotoAs static method

Future<ProductImagePageResult?> usePhotoAs({
  1. required BuildContext context,
  2. required Product product,
  3. required OpenFoodFactsLanguage language,
  4. required ProductImage productImage,
})

Implementation

static Future<ProductImagePageResult?> usePhotoAs({
  required BuildContext context,
  required Product product,
  required OpenFoodFactsLanguage language,
  required ProductImage productImage,
}) async {
  final AppLocalizations appLocalizations = AppLocalizations.of(context);
  final SmoothColorsThemeExtension extension = context
      .extension<SmoothColorsThemeExtension>();

  final List<ImageField> imageFields =
      ImageFieldSmoothieExtension.getOrderedMainImageFields(
        product.productType,
      );

  final Widget existingPictureIcon = icons.Picture.check(
    color: extension.success,
    semanticLabel: appLocalizations.photo_already_exists,
  );
  final Widget missingPictureIcon = icons.Picture.error(
    color: extension.error,
    semanticLabel: appLocalizations.photo_missing,
  );

  final ImageField?
  selectedImageField = await showSmoothListOfChoicesModalSheet<ImageField>(
    context: context,
    title: appLocalizations.photo_viewer_use_picture_as_title(
      Languages().getNameInLanguage(language),
    ),
    padding: const EdgeInsetsDirectional.only(start: 15.0, end: 19.0),
    labels: imageFields
        .map((ImageField imageField) {
          return switch (imageField) {
            ImageField.FRONT => appLocalizations.photo_field_front,
            ImageField.INGREDIENTS =>
              appLocalizations.photo_field_ingredients,
            ImageField.NUTRITION => appLocalizations.photo_field_nutrition,
            ImageField.PACKAGING => appLocalizations.photo_field_packaging,
            ImageField.OTHER => throw UnimplementedError(),
          };
        })
        .toList(growable: false),
    values: imageFields,
    prefixIcons: imageFields
        .map((ImageField imageField) {
          return switch (imageField) {
            ImageField.FRONT => const icons.Milk.happy(),
            ImageField.INGREDIENTS => const icons.Ingredients.alt(),
            ImageField.NUTRITION => const icons.NutritionFacts(),
            ImageField.PACKAGING => const icons.Recycling(),
            ImageField.OTHER => throw UnimplementedError(),
          };
        })
        .toList(growable: false),
    suffixIcons: imageFields
        .map((ImageField imageField) {
          final bool exists = TransientFile.fromProduct(
            product,
            imageField,
            language,
          ).isImageAvailable();
          return exists ? existingPictureIcon : missingPictureIcon;
        })
        .toList(growable: false),
  );

  if (context.mounted && selectedImageField != null) {
    final CropParameters? cropParameters =
        await UploadedImageGallery.useExistingPhotoFor(
          context: context,
          rawImage: productImage,
          barcode: product.barcode!,
          imageField: selectedImageField,
          isLoggedInMandatory: true,
          productType: product.productType,
          language: language,
        );

    if (cropParameters != null) {
      return ProductImagePageResult(
        cropParameters: cropParameters,
        imageField: selectedImageField,
      );
    }
  }

  return null;
}