resolveFrom method

ArnaDynamicColor resolveFrom(
  1. BuildContext context
)

Resolves this ArnaDynamicColor using the provided BuildContext.

Calling this method will create a new ArnaDynamicColor that is almost identical to this ArnaDynamicColor, except the effective color is changed to adapt to the given BuildContext.

For example, if the given BuildContext indicates the widgets in the subtree should be displayed in dark mode (the surrounding ArnaTheme's ArnaThemeData.brightness or MediaQuery's MediaQueryData.platformBrightness is Brightness.dark), and with a high accessibility contrast (the surrounding MediaQuery's MediaQueryData.highContrast is true), the resolved ArnaDynamicColor will be the same as this ArnaDynamicColor, except its effective color will be the darkHighContrastColor variant from the original ArnaDynamicColor.

Calling this function may create dependencies on the closest instance of some InheritedWidgets that enclose the given BuildContext. E.g., if darkColor is different from color, this method will call ArnaTheme.of, and then MediaQuery.of if brightness wasn't specified in the theme data retrieved from the previous ArnaTheme.of call, in an effort to determine the brightness value.

If any of the required dependencies are missing from the given context, the default value of that trait will be used (Brightness.light platform brightness and normal contrast).

Implementation

ArnaDynamicColor resolveFrom(BuildContext context) {
  Brightness brightness = Brightness.light;
  if (_isPlatformBrightnessDependent) {
    brightness = ArnaTheme.maybeBrightnessOf(context) ?? Brightness.light;
  }

  bool isHighContrastEnabled = false;
  if (_isHighContrastDependent) {
    isHighContrastEnabled =
        MediaQuery.maybeOf(context)?.highContrast ?? false;
  }

  final Color resolved = brightness == Brightness.light
      ? isHighContrastEnabled
          ? highContrastColor
          : color
      : isHighContrastEnabled
          ? darkHighContrastColor
          : darkColor;

  Element? debugContext;
  assert(
    () {
      debugContext = context as Element;
      return true;
    }(),
  );
  return ArnaDynamicColor._(
    resolved,
    color,
    darkColor,
    highContrastColor,
    darkHighContrastColor,
    debugContext,
    _debugLabel,
  );
}