resolveFrom method Null safety

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.of, and then MediaQuery.of if brightness wasn't specified in the theme data retrieved from the previous CupertinoTheme.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, 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.maybeOf(context)?.highContrast ?? 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;
          break;
        case CupertinoUserInterfaceLevelData.elevated:
          resolved = isHighContrastEnabled ? highContrastElevatedColor : elevatedColor;
          break;
      }
      break;
    case Brightness.dark:
      switch (level) {
        case CupertinoUserInterfaceLevelData.base:
          resolved = isHighContrastEnabled ? darkHighContrastColor : darkColor;
          break;
        case CupertinoUserInterfaceLevelData.elevated:
          resolved = isHighContrastEnabled ? darkHighContrastElevatedColor : darkElevatedColor;
          break;
      }
  }

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