resolveFrom method

CupertinoDynamicColor resolveFrom(
  1. BuildContext context
)

Resolves this CupertinoDynamicColor using the provided BuildContext.

Calling this method will create a new CupertinoDynamicColor that is almost identical to this CupertinoDynamicColor, 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 CupertinoTheme's CupertinoThemeData.brightness or MediaQuery's MediaQueryData.platformBrightness is Brightness.dark), with a high accessibility contrast (the surrounding MediaQuery's MediaQueryData.highContrast is true), and an elevated interface elevation (the surrounding CupertinoUserInterfaceLevel's data is CupertinoUserInterfaceLevelData.elevated), the resolved CupertinoDynamicColor will be the same as this CupertinoDynamicColor, except its effective color will be the darkHighContrastElevatedColor variant from the original CupertinoDynamicColor.

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 CupertinoTheme.maybeBrightnessOf in an effort to determine the brightness. If color is different from highContrastColor, this method will call MediaQuery.maybeHighContrastOf in an effort to determine the high contrast setting.

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, normal contrast, CupertinoUserInterfaceLevelData.base elevation level).

Implementation

CupertinoDynamicColor resolveFrom(BuildContext context) {
  Brightness brightness = Brightness.light;
  if (_isPlatformBrightnessDependent) {
    brightness = CupertinoTheme.maybeBrightnessOf(context) ?? Brightness.light;
  }
  bool isHighContrastEnabled = false;
  if (_isHighContrastDependent) {
    isHighContrastEnabled = MediaQuery.maybeHighContrastOf(context) ?? false;
  }

  final CupertinoUserInterfaceLevelData level = _isInterfaceElevationDependent
    ? CupertinoUserInterfaceLevel.maybeOf(context) ?? CupertinoUserInterfaceLevelData.base
    : CupertinoUserInterfaceLevelData.base;

  final Color resolved;
  switch (brightness) {
    case Brightness.light:
      switch (level) {
        case CupertinoUserInterfaceLevelData.base:
          resolved = isHighContrastEnabled ? highContrastColor : color;
        case CupertinoUserInterfaceLevelData.elevated:
          resolved = isHighContrastEnabled ? highContrastElevatedColor : elevatedColor;
      }
    case Brightness.dark:
      switch (level) {
        case CupertinoUserInterfaceLevelData.base:
          resolved = isHighContrastEnabled ? darkHighContrastColor : darkColor;
        case CupertinoUserInterfaceLevelData.elevated:
          resolved = isHighContrastEnabled ? darkHighContrastElevatedColor : darkElevatedColor;
      }
  }

  Element? debugContext;
  assert(() {
    debugContext = context as Element;
    return true;
  }());
  return CupertinoDynamicColor._(
    resolved,
    color,
    darkColor,
    highContrastColor,
    darkHighContrastColor,
    elevatedColor,
    darkElevatedColor,
    highContrastElevatedColor,
    darkHighContrastElevatedColor,
    debugContext,
    _debugLabel,
  );
}