FlexSchemeOnColors.from constructor

FlexSchemeOnColors.from({
  1. required Color primary,
  2. required Color secondary,
  3. required Color surface,
  4. required Color background,
  5. required Color error,
  6. Color? onPrimary,
  7. Color? onSecondary,
  8. Color? onSurface,
  9. Color? onBackground,
  10. Color? onError,
})

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.

Implementation

factory FlexSchemeOnColors.from({
  required Color primary,
  required Color secondary,
  required Color surface,
  required Color background,
  required Color error,
  Color? onPrimary,
  Color? onSecondary,
  Color? onSurface,
  Color? onBackground,
  Color? onError,
}) {
  // 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 _onPrimary = onPrimary ??
      (ThemeData.estimateBrightnessForColor(primary) == Brightness.dark
          ? Colors.white
          : Colors.black);
  final Color _onSecondary = onSecondary ??
      (ThemeData.estimateBrightnessForColor(secondary) == Brightness.dark
          ? Colors.white
          : Colors.black);
  final Color _onError = onError ??
      (ThemeData.estimateBrightnessForColor(error) == Brightness.dark
          ? Colors.white
          : Colors.black);

  final Color _onSurface = onSurface ??
      (ThemeData.estimateBrightnessForColor(surface) == Brightness.dark
          ? Colors.white
          : Colors.black);

  final Color _onBackground = onBackground ??
      (ThemeData.estimateBrightnessForColor(background) == Brightness.dark
          ? Colors.white
          : Colors.black);

  return FlexSchemeOnColors(
    onPrimary: _onPrimary,
    onSecondary: _onSecondary,
    onSurface: _onSurface,
    onBackground: _onBackground,
    onError: _onError,
  );
}