FlexSchemeOnColors.from constructor

FlexSchemeOnColors.from({
  1. bool? useMaterial3,
  2. required Color primary,
  3. Color? primaryContainer,
  4. required Color secondary,
  5. Color? secondaryContainer,
  6. Color? tertiary,
  7. Color? tertiaryContainer,
  8. required Color surface,
  9. Color? inverseSurface,
  10. Color? surfaceTint,
  11. required Color error,
  12. Color? errorContainer,
  13. Color? onPrimary,
  14. Color? onPrimaryContainer,
  15. Color? onSecondary,
  16. Color? onSecondaryContainer,
  17. Color? onTertiary,
  18. Color? onTertiaryContainer,
  19. Color? onSurface,
  20. Color? onSurfaceVariant,
  21. Color? onInverseSurface,
  22. Color? onError,
  23. Color? onErrorContainer,
  24. int primaryAlpha = 0,
  25. int primaryContainerAlpha = 0,
  26. int secondaryAlpha = 0,
  27. int secondaryContainerAlpha = 0,
  28. int tertiaryAlpha = 0,
  29. int tertiaryContainerAlpha = 0,
  30. int surfaceAlpha = 0,
  31. int inverseSurfaceAlpha = 0,
  32. int errorAlpha = 0,
  33. int errorContainerAlpha = 0,
  34. @Deprecated('This color was deprecated in FCS 8.0 because Flutter 3.22 ' 'deprecated the color. ' 'It no longer has any function in FCS v8 and will be removed in v9.') Color? background,
  35. @Deprecated('This color was deprecated in FCS 8.0 because Flutter 3.22 ' 'deprecated the color. ' 'It no longer has any function in FCS v8 and will be removed in v9.') Color? onBackground,
  36. @Deprecated('This property was deprecated in FCS 8.0 because Flutter 3.22 ' 'deprecated the related color. ' 'It no longer has any function in FCS v8 and will be removed in v9.') int backgroundAlpha = 0,
  37. @Deprecated('This color was deprecated in FCS 8.0 because Flutter 3.22 ' 'deprecated the color. ' 'It no longer has any function in FCS v8 and will be removed in v9.') Color? surfaceVariant,
  38. @Deprecated('This property was deprecated in FCS 8.0 because Flutter 3.22 ' 'deprecated the related color. ' 'It no longer has any function in FCS v8 and will be removed in v9.') int surfaceVariantAlpha = 0,
})

Compute on colors for required primary, secondary, surface, background and error colors and returns a valid FlexSchemeOnColors with correct on colors for these colors.

If an optional on color value is given as input, the value for that particular on color will be used instead of the computed on color value for the corresponding provided color.

The factory can also alpha blend the onColor, with each color using an optionally provided alpha blend level, that defaults to 0.

Implementation

// ignore: sort_constructors_first
factory FlexSchemeOnColors.from({
  bool? useMaterial3,
  required Color primary,
  Color? primaryContainer,
  required Color secondary,
  Color? secondaryContainer,
  Color? tertiary,
  Color? tertiaryContainer,
  required Color surface,
  Color? inverseSurface,
  Color? surfaceTint,
  required Color error,
  Color? errorContainer,
  Color? onPrimary,
  Color? onPrimaryContainer,
  Color? onSecondary,
  Color? onSecondaryContainer,
  Color? onTertiary,
  Color? onTertiaryContainer,
  Color? onSurface,
  Color? onSurfaceVariant,
  Color? onInverseSurface,
  Color? onError,
  Color? onErrorContainer,
  int primaryAlpha = 0,
  int primaryContainerAlpha = 0,
  int secondaryAlpha = 0,
  int secondaryContainerAlpha = 0,
  int tertiaryAlpha = 0,
  int tertiaryContainerAlpha = 0,
  int surfaceAlpha = 0,
  int inverseSurfaceAlpha = 0,
  int errorAlpha = 0,
  int errorContainerAlpha = 0,
  @Deprecated('This color was deprecated in FCS 8.0 because Flutter 3.22 '
      'deprecated the color. '
      'It no longer has any function in FCS v8 and will be removed in v9.')
  Color? background,
  @Deprecated('This color was deprecated in FCS 8.0 because Flutter 3.22 '
      'deprecated the color. '
      'It no longer has any function in FCS v8 and will be removed in v9.')
  Color? onBackground,
  @Deprecated('This property was deprecated in FCS 8.0 because Flutter 3.22 '
      'deprecated the related color. '
      'It no longer has any function in FCS v8 and will be removed in v9.')
  int backgroundAlpha = 0,
  @Deprecated('This color was deprecated in FCS 8.0 because Flutter 3.22 '
      'deprecated the color. '
      'It no longer has any function in FCS v8 and will be removed in v9.')
  Color? surfaceVariant,
  @Deprecated('This property was deprecated in FCS 8.0 because Flutter 3.22 '
      'deprecated the related color. '
      'It no longer has any function in FCS v8 and will be removed in v9.')
  int surfaceVariantAlpha = 0,
}) {
  final bool useM3 = useMaterial3 ?? true;
  // Use optional surfaceTint, with fallback to primary.
  // Surface Tint is used onSurface, onSurfaceVariant and onInverseSurface
  final Color usedSurfaceTint = surfaceTint ?? primary;

  // Check brightness of primary, secondary, error, surface and background
  // colors, then calculate appropriate colors for their onColors, if an
  // "on" color was not passed in, otherwise we just use its given color.
  final Color usedOnPrimary = onPrimary ??
      (ThemeData.estimateBrightnessForColor(primary) == Brightness.dark
          ? Colors.white.blendAlpha(primary.brighten(20), primaryAlpha)
          : Colors.black.blendAlpha(primary.brighten(20), primaryAlpha));
  final Color? usedOnPrimaryContainer = onPrimaryContainer ??
      (primaryContainer == null
          ? null
          : (ThemeData.estimateBrightnessForColor(primaryContainer) ==
                  Brightness.dark
              ? Colors.white.blendAlpha(
                  primaryContainer.brighten(22), primaryContainerAlpha)
              : Colors.black.blendAlpha(
                  primaryContainer.brighten(8), primaryContainerAlpha)));

  final Color usedOnSecondary = onSecondary ??
      (ThemeData.estimateBrightnessForColor(secondary) == Brightness.dark
          ? Colors.white.blendAlpha(secondary.brighten(20), secondaryAlpha)
          : Colors.black.blendAlpha(secondary.brighten(20), secondaryAlpha));
  final Color? usedOnSecondaryContainer = onSecondaryContainer ??
      (secondaryContainer == null
          ? null
          : (ThemeData.estimateBrightnessForColor(secondaryContainer) ==
                  Brightness.dark
              ? Colors.white.blendAlpha(
                  secondaryContainer.brighten(22), secondaryContainerAlpha)
              : Colors.black.blendAlpha(
                  secondaryContainer.brighten(8), secondaryContainerAlpha)));
  final Color? usedOnTertiary = onTertiary ??
      (tertiary == null
          ? null
          : (ThemeData.estimateBrightnessForColor(tertiary) == Brightness.dark
              ? Colors.white.blendAlpha(tertiary.brighten(20), tertiaryAlpha)
              : Colors.black
                  .blendAlpha(tertiary.brighten(20), tertiaryAlpha)));
  final Color? usedOnTertiaryContainer = onTertiaryContainer ??
      (tertiaryContainer == null
          ? null
          : (ThemeData.estimateBrightnessForColor(tertiaryContainer) ==
                  Brightness.dark
              ? Colors.white.blendAlpha(
                  tertiaryContainer.brighten(22), tertiaryContainerAlpha)
              : Colors.black.blendAlpha(
                  tertiaryContainer.brighten(8), tertiaryContainerAlpha)));

  final Color usedOnSurface = onSurface ??
      (ThemeData.estimateBrightnessForColor(surface) == Brightness.dark
          ? useM3
              ? FlexColor.darkFlexOnSurface
                  .blendAlpha(usedSurfaceTint.darken(10), surfaceAlpha)
              : Colors.white
                  .blendAlpha(usedSurfaceTint.darken(10), surfaceAlpha)
          : useM3
              ? FlexColor.lightFlexOnSurface
                  .blendAlpha(usedSurfaceTint.lighten(24), surfaceAlpha)
              : Colors.black
                  .blendAlpha(usedSurfaceTint.lighten(24), surfaceAlpha));

  final Color usedOnSurfaceVariant = onSurfaceVariant ??
      (ThemeData.estimateBrightnessForColor(surface) == Brightness.dark
          ? useM3
              ? FlexColor.darkFlexOnSurfaceVariant
                  .blendAlpha(usedSurfaceTint.darken(10), surfaceAlpha)
              : Colors.white
                  .blendAlpha(usedSurfaceTint.darken(10), surfaceAlpha)
          : useM3
              ? FlexColor.lightFlexOnSurfaceVariant
                  .blendAlpha(usedSurfaceTint.lighten(24), surfaceAlpha)
              : Colors.black
                  .blendAlpha(usedSurfaceTint.lighten(24), surfaceAlpha));

  final Color invSurface = inverseSurface ??
      (ThemeData.estimateBrightnessForColor(surface) == Brightness.dark
          ? useM3
              ? FlexColor.darkFlexInverseSurface
              : FlexColor.materialLightSurface
          : useM3
              ? FlexColor.lightFlexInverseSurface
              : FlexColor.materialDarkSurface);

  final Color usedOnInverseSurface = onInverseSurface ??
      (ThemeData.estimateBrightnessForColor(invSurface) == Brightness.dark
          ? useM3
              ? FlexColor.lightFlexOnInverseSurface.blendAlpha(
                  usedSurfaceTint.lighten(24), inverseSurfaceAlpha)
              : FlexColor.materialLightSurface
                  .blendAlpha(invSurface, inverseSurfaceAlpha)
          : useM3
              ? FlexColor.darkFlexOnInverseSurface
                  .blendAlpha(usedSurfaceTint.darken(10), inverseSurfaceAlpha)
              : FlexColor.materialDarkSurface
                  .blendAlpha(invSurface, inverseSurfaceAlpha));

  final Color usedOnError = onError ??
      (estimateErrorBrightness(error) == Brightness.dark
          ? Colors.white.blendAlpha(error.brighten(20), errorAlpha)
          : Colors.black.blendAlpha(error.brighten(20), errorAlpha));

  final Color usedOnErrorContainer = onErrorContainer ??
      (estimateErrorBrightness(errorContainer ?? error) == Brightness.dark
          ? Colors.white.blendAlpha(
              errorContainer?.brighten(22) ?? error.brighten(20),
              errorContainerAlpha)
          : Colors.black.blendAlpha(
              errorContainer?.brighten(8) ?? error.brighten(20),
              errorContainerAlpha));

  return FlexSchemeOnColors(
    onPrimary: usedOnPrimary,
    onPrimaryContainer: usedOnPrimaryContainer,
    onSecondary: usedOnSecondary,
    onSecondaryContainer: usedOnSecondaryContainer,
    onTertiary: usedOnTertiary,
    onTertiaryContainer: usedOnTertiaryContainer,
    onSurface: usedOnSurface,
    onSurfaceVariant: usedOnSurfaceVariant,
    onInverseSurface: usedOnInverseSurface,
    onError: usedOnError,
    onErrorContainer: usedOnErrorContainer,
  );
}