toScheme property

ColorScheme toScheme

Returns the effective ColorScheme defined by your FlexColorScheme.

After you have defined your scheme with FlexColorScheme or one of its recommended factories FlexColorScheme.light, FlexColorScheme.dark, you can use the toScheme method to get the effective standard Flutter ColorScheme object defined by your FlexColorScheme definition.

While you can use use this returned color scheme in a standard ThemeData.from color scheme based theme factory to create a theme from FlexColorScheme, this is NOT the recommended way to make a fully FlexColorScheme based theme. Normally you want to use FlexColorScheme.toTheme to make your ThemeData when using FlexColorScheme.

The main usage of this method is to get the effective resulting ColorScheme from [FlexColorScheme) and use it when making sub-themes that need access to the resulting color definitions so you can use them in custom sub-themes to use the same color scheme for their definitions. This is e.g. needed on custom hover, ink and splash effects.

If you use ThemeData.from and the ColorScheme returned by FlexColorScheme.toScheme to create tour theme, this will work and result in a theme that is based on the color scheme defined in FlexColorScheme, including the surface and background color branding, and e.g. true black for dark mode, if those were used in its creation via the light and dark factories. The big difference will be that Flutter's ThemeData.from theme creation from this scheme will not include any of the theme fixes, included in the FlexColorScheme.toTheme method. The AppBar theme options will also not be available and scaffoldBackground will be equal to background, which might not be the design you intended.

The sub-theming is of course also not available, unless you apply them all with copyWith to the produced ThemeData.

Implementation

ColorScheme get toScheme {
  // Get effective brightness.
  final Brightness _brightness =
      brightness ?? colorScheme?.brightness ?? Brightness.light;
  final bool isDark = _brightness == Brightness.dark;

  // Get effective primary color.
  final Color _primary = primary ??
      colorScheme?.primary ??
      (isDark
          ? FlexColor.materialDarkPrimary
          : FlexColor.materialLightPrimary);

  // Calculate default fallback colors from primary color.
  final FlexSchemeColor _fallbacks = FlexSchemeColor.from(
    primary: _primary,
    primaryVariant: primaryVariant,
    secondary: secondary,
    secondaryVariant: secondaryVariant,
  );

  // Determine effective main colors
  final Color _primaryVariant = primaryVariant ??
      colorScheme?.primaryVariant ??
      _fallbacks.primaryVariant;
  final Color _secondary =
      secondary ?? colorScheme?.secondary ?? _fallbacks.secondary;
  final Color _secondaryVariant = secondaryVariant ??
      colorScheme?.secondaryVariant ??
      _fallbacks.secondaryVariant;

  // Determine effective surface, background and error colors.
  final Color _surface = surface ??
      colorScheme?.surface ??
      (isDark
          ? FlexColor.materialDarkSurface
          : FlexColor.materialLightSurface);
  final Color _background = background ??
      colorScheme?.background ??
      (isDark
          ? FlexColor.materialDarkBackground
          : FlexColor.materialLightBackground);
  final Color _error = error ??
      colorScheme?.error ??
      (isDark ? FlexColor.materialDarkError : FlexColor.materialLightError);

  // Checks brightness of primary, secondary, error, surface and background
  // colors, and returns appropriate colors for their onColors, if an
  // onColor for it was was not passed in or no colorScheme with them given.
  final FlexSchemeOnColors onColors = FlexSchemeOnColors.from(
    primary: _primary,
    secondary: _secondary,
    surface: _surface,
    background: _background,
    error: _error,
    onPrimary: onPrimary ?? colorScheme?.onPrimary,
    onSecondary: onSecondary ?? colorScheme?.onSecondary,
    onSurface: onSurface ?? colorScheme?.onSurface,
    onBackground: onBackground ?? colorScheme?.onBackground,
    onError: onError ?? colorScheme?.onError,
  );

  // Return the [ColorScheme] as a copyWith on original passed in colorScheme
  // if one was passed in, with all the new effective properties. This will
  // keep new M3 properties intact (on master channel) that we are not yet
  // dealing with. If there was no colorScheme passed in, we create one
  // with the effective properties.
  return colorScheme?.copyWith(
        primary: _primary,
        primaryVariant: _primaryVariant,
        secondary: _secondary,
        secondaryVariant: _secondaryVariant,
        surface: _surface,
        background: _background,
        error: _error,
        onPrimary: onColors.onPrimary,
        onSecondary: onColors.onSecondary,
        onSurface: onColors.onSurface,
        onBackground: onColors.onBackground,
        onError: onColors.onError,
        brightness: _brightness,
      ) ??
      ColorScheme(
        primary: _primary,
        primaryVariant: _primaryVariant,
        secondary: _secondary,
        secondaryVariant: _secondaryVariant,
        surface: _surface,
        background: _background,
        error: _error,
        onPrimary: onColors.onPrimary,
        onSecondary: onColors.onSecondary,
        onSurface: onColors.onSurface,
        onBackground: onColors.onBackground,
        onError: onColors.onError,
        brightness: _brightness,
      );
}