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) {
  final Brightness brightness = _isPlatformBrightnessDependent
    ? CupertinoTheme.maybeBrightnessOf(context) ?? Brightness.light
    : Brightness.light;

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

  final bool highContrast = _isHighContrastDependent
    && (MediaQuery.maybeHighContrastOf(context) ?? false);

  final Color resolved = switch ((brightness, level, highContrast)) {
    (Brightness.light, CupertinoUserInterfaceLevelData.base,     false) => color,
    (Brightness.light, CupertinoUserInterfaceLevelData.base,     true)  => highContrastColor,
    (Brightness.light, CupertinoUserInterfaceLevelData.elevated, false) => elevatedColor,
    (Brightness.light, CupertinoUserInterfaceLevelData.elevated, true)  => highContrastElevatedColor,
    (Brightness.dark,  CupertinoUserInterfaceLevelData.base,     false) => darkColor,
    (Brightness.dark,  CupertinoUserInterfaceLevelData.base,     true)  => darkHighContrastColor,
    (Brightness.dark,  CupertinoUserInterfaceLevelData.elevated, false) => darkElevatedColor,
    (Brightness.dark,  CupertinoUserInterfaceLevelData.elevated, true)  => darkHighContrastElevatedColor,
  };

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