readIconName function

String? readIconName(
  1. Element element
)

User-provided name for an icon (id is separate). Never invents Material names.

Implementation

String? readIconName(Element element) {
  Icon? icon;
  void findIcon(Element e) {
    if (icon != null) return;
    if (e.widget is Icon) {
      icon = e.widget as Icon;
      return;
    }
    e.visitChildren(findIcon);
  }

  if (element.widget is Icon) {
    icon = element.widget as Icon;
  } else {
    element.visitChildren(findIcon);
  }

  final semantic = icon?.semanticLabel?.trim();
  if (semantic != null && semantic.isNotEmpty) return semantic;

  final iconButton = _selfOrAncestor<IconButton>(element);
  final buttonTooltip = iconButton?.tooltip?.trim();
  if (buttonTooltip != null && buttonTooltip.isNotEmpty) return buttonTooltip;

  final tooltip = _selfOrAncestor<Tooltip>(element);
  final tooltipMessage = tooltip?.message?.trim();
  if (tooltipMessage != null && tooltipMessage.isNotEmpty) {
    return tooltipMessage;
  }

  // Text-glyph icons (e.g. language "A") — only if the app literally rendered it.
  final glyph = readText(element)?.trim();
  if (glyph != null && glyph.isNotEmpty && glyph.length <= 2) return glyph;

  return null;
}