getElementWidget method

Widget? getElementWidget({
  1. required Product product,
  2. required bool isInitiallyExpanded,
  3. required bool isClickable,
  4. required bool isTextSelectable,
  5. required int position,
  6. required bool simplified,
})

Returns the widget that displays the KP element, or rarely null.

cf. hasSomethingToDisplay.

Implementation

Widget? getElementWidget({
  required Product product,
  required bool isInitiallyExpanded,
  required bool isClickable,
  required bool isTextSelectable,
  required int position,
  required bool simplified,
}) {
  switch (elementType) {
    case KnowledgePanelElementType.TEXT:
      return KnowledgePanelTextCard(textElement: textElement!);

    case KnowledgePanelElementType.IMAGE:
      return KnowledgePanelImageCard(imageElement: imageElement!);

    case KnowledgePanelElementType.PANEL:
      final String panelId = panelElement!.panelId;
      final KnowledgePanel? panel = KnowledgePanelsBuilder.getKnowledgePanel(
        product,
        panelId,
      );
      if (panel == null) {
        // happened in https://github.com/openfoodfacts/smooth-app/issues/2682
        // due to some inconsistencies in the data sent by the server
        if (panelId == 'ecoscore' &&
            (product.productType ?? ProductType.food) != ProductType.food) {
          // just ignore
        } else {
          Logs.w('unknown panel "$panelId" for barcode "${product.barcode}"');
        }
        return null;
      }
      return KnowledgePanelCard(
        panelId: panelId,
        product: product,
        isClickable: isClickable,
        simplified: simplified,
      );

    case KnowledgePanelElementType.PANEL_GROUP:
      if (simplified) {
        final List<KnowledgePanel> squarePanels = <KnowledgePanel>[];
        for (final String panelId in panelGroupElement!.panelIds) {
          final KnowledgePanel? panel =
              KnowledgePanelsBuilder.getKnowledgePanel(product, panelId);
          if (panel != null && (panel.halfWidthOnMobile ?? false)) {
            squarePanels.add(panel);
          }
        }

        if (squarePanels.isNotEmpty) {
          return KnowledgePanelSquareCard(
            panels: squarePanels,
            panelsIds: panelGroupElement?.panelIds,
            product: product,
          );
        }
      }

      return KnowledgePanelGroupCard(
        groupElement: panelGroupElement!,
        product: product,
        isClickable: isClickable,
        isTextSelectable: isTextSelectable,
        position: position,
        simplified: simplified,
      );

    case KnowledgePanelElementType.TABLE:
      return ScrollableTableWidget(SmoothTable.fromKP(tableElement!));

    case KnowledgePanelElementType.MAP:
      return KnowledgePanelWorldMapCard(mapElement!);

    case KnowledgePanelElementType.UNKNOWN:
      return null;

    case KnowledgePanelElementType.ACTION:
      return KnowledgePanelActionCard(actionElement!, product);
  }
}