getElementWidget method
Returns the widget that displays the KP element, or rarely null.
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);
}
}