resolveFrom method

CupertinoDynamicColor resolveFrom (
  1. BuildContext context,
  2. {bool nullOk: true}
)

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), unless nullOk is set to false, in which case an exception will be thrown.

Implementation

CupertinoDynamicColor resolveFrom(BuildContext context, { bool nullOk = true }) {
  final Brightness brightness = _isPlatformBrightnessDependent
    ? CupertinoTheme.brightnessOf(context, nullOk: nullOk) ?? Brightness.light
    : Brightness.light;

  final bool isHighContrastEnabled = _isHighContrastDependent
    && (MediaQuery.of(context, nullOk: nullOk)?.highContrast ?? false);


  final CupertinoUserInterfaceLevelData level = _isInterfaceElevationDependent
    ? CupertinoUserInterfaceLevel.of(context, nullOk: nullOk) ?? CupertinoUserInterfaceLevelData.base
    : CupertinoUserInterfaceLevelData.base;

  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,
  );
}