toScheme property

ColorScheme toScheme

Returns the ColorScheme object defined by FlexColorScheme properties.

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

You can use then use this returned color scheme in a standard ThemeData.from color scheme based theme factory to create a theme from FlexColorScheme. This will result in a theme that is based on the color scheme defined in FlexColorScheme, including the surface and background color branding, and true black for dark mode, if those were used in its creation via the light and dark factories. The main difference will be that the Flutter's ThemeData.from theme creation from this scheme will not include any of the theme fixes, but then again also none of the opinionated styles, used in FlexColorScheme.toTheme method.

The AppBar theme options will also not be available and scaffoldBackground will be equal to background, which does not look so good if heavy branding was used on surfaces.

Implementation

ColorScheme get toScheme {
  // A convenience bool to check if this theme is for light or dark mode
  final bool isDark = brightness == Brightness.dark;

  // Check brightness of primary, secondary, error, surface and background
  // colors, and then calculate appropriate colors for their onColors, if an
  // "on" color was not passed in.
  final FlexSchemeOnColors onColors = FlexSchemeOnColors.from(
    primary: primary,
    secondary: secondary,
    surface: surface ??
        (isDark
            ? FlexColor.materialDarkSurface
            : FlexColor.materialLightSurface),
    background: background ??
        (isDark
            ? FlexColor.materialDarkBackground
            : FlexColor.materialLightBackground),
    error: error ??
        (isDark ? FlexColor.materialDarkError : FlexColor.materialLightError),
    onPrimary: onPrimary,
    onSecondary: onSecondary,
    onSurface: onSurface,
    onBackground: onBackground,
    onError: onError,
  );

  return ColorScheme(
    primary: primary,
    primaryVariant: primaryVariant,
    secondary: secondary,
    secondaryVariant: secondaryVariant,
    surface: surface ??
        (isDark
            ? FlexColor.materialDarkSurface
            : FlexColor.materialLightSurface),
    background: background ??
        (isDark
            ? FlexColor.materialDarkBackground
            : FlexColor.materialLightBackground),
    error: error ??
        (isDark ? FlexColor.materialDarkError : FlexColor.materialLightError),
    onBackground: onColors.onBackground,
    onSurface: onColors.onSurface,
    onError: onColors.onError,
    onPrimary: onColors.onPrimary,
    onSecondary: onColors.onSecondary,
    brightness: brightness,
  );
}