FlexSchemeColor.from constructor

FlexSchemeColor.from({
  1. required Color primary,
  2. Color? primaryContainer,
  3. Color? secondary,
  4. Color? secondaryContainer,
  5. Color? tertiary,
  6. Color? tertiaryContainer,
  7. Color? appBarColor,
  8. Color? error,
  9. Color? errorContainer,
  10. Brightness? brightness,
  11. bool swapOnMaterial3 = false,
})

Make a FlexSchemeColor from just one primary color or possible also from a more complete color scheme set. This is a convenience factory that can create nice toned color schemes based on only the primary color.

If brightness is null, this function works as in versions before v5, with respect to the colors properties that existed in FlexSchemeColor then. When brightness is dark or light, it has new logic more suitable for the M3 ColorScheme color definitions, for light or dark theme.

With brightness set it also creates error colors for error and errorContainer using past M2 standard error FlexColor.materialLightError or FlexColor.materialDarkError or as input value for error if not provided and computes errorContainer. If either value is passed in they are used as given.

Implementation

factory FlexSchemeColor.from({
  required Color primary,
  Color? primaryContainer,
  Color? secondary,
  Color? secondaryContainer,
  Color? tertiary,
  Color? tertiaryContainer,
  Color? appBarColor,
  Color? error,
  Color? errorContainer,
  Brightness? brightness,
  bool swapOnMaterial3 = false,
}) {
  if (brightness == Brightness.light) {
    return FlexSchemeColor(
      primary: primary,
      primaryContainer:
          primaryContainer ?? primary.lighten(20).blend(Colors.white, 60),
      secondary: secondary ?? primary.darken().brighten(20),
      secondaryContainer: secondaryContainer ??
          secondary?.brighten(14).blend(Colors.white, 50) ??
          primary.darken().brighten(20).blend(Colors.white, 60),
      tertiary: tertiary ?? primary.brighten(15),
      tertiaryContainer: tertiaryContainer ??
          tertiary?.brighten(18).blend(Colors.white, 50) ??
          primary.brighten(15).lighten(20).blend(Colors.white, 60),
      appBarColor: appBarColor ??
          tertiary ??
          primary.brighten(15).lighten(20).blend(Colors.white, 60),
      error: error ?? FlexColor.materialLightError,
      errorContainer: errorContainer ??
          FlexColor.lightErrorContainer(
              error ?? FlexColor.materialLightError),
      swapOnMaterial3: swapOnMaterial3,
    );
  } else if (brightness == Brightness.dark) {
    return FlexSchemeColor(
      primary: primary,
      primaryContainer:
          primaryContainer ?? primary.darken(5).blend(Colors.black, 55),
      secondary: secondary ?? primary.darken().brighten(20),
      secondaryContainer: secondaryContainer ??
          secondary?.darken(25).blend(Colors.black, 50) ??
          primary.darken().brighten(20).blend(Colors.black, 40),
      tertiary: tertiary ?? primary.brighten(15),
      tertiaryContainer: tertiaryContainer ??
          tertiary?.darken(15).blend(Colors.black, 60) ??
          primary.brighten(15).darken(20).blend(Colors.black, 30),
      appBarColor: appBarColor ??
          tertiary ??
          primary.brighten(15).darken(20).blend(Colors.black, 30),
      error: error ?? FlexColor.materialDarkError,
      errorContainer: errorContainer ??
          FlexColor.darkErrorContainer(error ?? FlexColor.materialDarkError),
      swapOnMaterial3: swapOnMaterial3,
    );
  } else {
    return FlexSchemeColor(
      primary: primary,
      primaryContainer:
          primaryContainer ?? primary.darken(kDarkenPrimaryContainer),
      secondary: secondary ?? primary.darken(kDarkenSecondary),
      secondaryContainer: secondaryContainer ??
          secondary?.darken(kDarkenSecondaryContainerFromSecondary) ??
          primary.darken(kDarkenSecondaryContainer),
      tertiary: tertiary ?? primary.lighten(kDarkenPrimaryContainer),
      tertiaryContainer: tertiaryContainer ??
          tertiary?.lighten(kDarkenSecondaryContainer) ??
          primary
              .brighten(kDarkenSecondary * 2)
              .lighten(kDarkenSecondaryContainer),
      appBarColor: appBarColor ??
          tertiary ??
          secondary?.lighten(kDarkenPrimaryContainer) ??
          primary.lighten(kDarkenPrimaryContainer),
      error: error,
      errorContainer: errorContainer,
      swapOnMaterial3: swapOnMaterial3,
    );
  }
}