variantOf static method

StyleVariant variantOf(
  1. BuildContext context
)

Returns the StyleVariant that IconButtons use at the given context.

The closest enclosing IconButtonTheme whose IconButtonThemeData.variant is non-null determines the variant, so an IconButtonTheme that doesn't specify a variant inherits it from its ancestors. If none of the enclosing IconButtonThemes specify a variant, the variant of ThemeData.iconButtonTheme is used. If that is also null, StyleVariant.material3 is returned.

Typical usage is as follows:

final StyleVariant variant = IconButtonTheme.variantOf(context);

Implementation

static StyleVariant variantOf(BuildContext context) {
  InheritedElement? element = context.getElementForInheritedWidgetOfExactType<IconButtonTheme>();
  while (element != null) {
    // Depend on every theme that is visited, so that a change to any of them
    // rebuilds the dependent.
    context.dependOnInheritedElement(element);
    final StyleVariant? variant = (element.widget as IconButtonTheme).data.variant;
    if (variant != null) {
      return variant;
    }
    // Calling getElementForInheritedWidgetOfExactType on the element itself
    // would return the same element, so continue the search from its parent.
    Element? parent;
    element.visitAncestorElements((Element ancestor) {
      parent = ancestor;
      return false;
    });
    element = parent?.getElementForInheritedWidgetOfExactType<IconButtonTheme>();
  }
  return Theme.of(context).iconButtonTheme.variant ?? StyleVariant.material3;
}