effective static method

FlexSchemeColor effective(
  1. FlexSchemeColor colors,
  2. int usedColors, {
  3. bool swapLegacy = false,
  4. bool swapColors = false,
  5. Brightness? brightness,
})

Make effective FlexSchemeColor colors using 1 to 6 of the passed in colors based on the usedColors property.

The usedColors value corresponds to:

If swapColors is true, primary and secondary, as well as primaryContainer and secondaryContainer are swapped, before being usage limited by usedColors.

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.

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. If the passed in colors have none null error or errorContainer, they are kept.

Implementation

static FlexSchemeColor effective(
  final FlexSchemeColor colors,
  final int usedColors, {
  final bool swapLegacy = false,
  final bool swapColors = false,
  final Brightness? brightness,
}) {
  assert(usedColors >= 1 && usedColors <= 7, 'usedColors must be 1 to 7.');

  // Swap legacy M2 designed secondary and tertiary colors.
  final FlexSchemeColor fixColors = swapLegacy
      ? colors.copyWith(
          secondary: colors.tertiary,
          secondaryContainer: colors.tertiaryContainer,
          tertiary: colors.secondary,
          tertiaryContainer: colors.secondaryContainer,
        )
      : colors;

  // Swap primary and secondary colors, using legacy fixColors.
  final FlexSchemeColor effectiveColors = swapColors
      ? fixColors.copyWith(
          primary: fixColors.secondary,
          primaryContainer: fixColors.secondaryContainer,
          secondary: fixColors.primary,
          secondaryContainer: fixColors.primaryContainer,
        )
      : fixColors;

  if (brightness == Brightness.light) {
    return effectiveColors.copyWith(
      primary: effectiveColors.primary,
      primaryContainer: (usedColors > 2 && usedColors != 7)
          ? effectiveColors.primaryContainer
          : effectiveColors.primary.lighten(20).blend(Colors.white, 60),
      secondary: (usedColors > 1 || usedColors == 7)
          ? effectiveColors.secondary
          : effectiveColors.primary.darken().brighten(20),
      secondaryContainer: (usedColors > 3 && usedColors != 7)
          ? effectiveColors.secondaryContainer
          : usedColors > 1
              ? effectiveColors.secondary.brighten(14).blend(Colors.white, 50)
              : effectiveColors.primary
                  .darken()
                  .brighten(20)
                  .blend(Colors.white, 60),
      tertiary: (usedColors > 4 || usedColors == 7)
          ? effectiveColors.tertiary
          : effectiveColors.primary.brighten(15),
      tertiaryContainer: (usedColors > 5 && usedColors != 7)
          ? effectiveColors.tertiaryContainer
          : usedColors > 4
              ? effectiveColors.tertiary.brighten(18).blend(Colors.white, 50)
              : effectiveColors.primary
                  .brighten(15)
                  .lighten(20)
                  .blend(Colors.white, 60),
      appBarColor: colors.appBarColor,
      error: colors.error ?? FlexColor.materialLightError,
      errorContainer: colors.errorContainer ??
          FlexColor.lightErrorContainer(
              colors.error ?? FlexColor.materialLightError),
    );
  } else if (brightness == Brightness.dark) {
    return effectiveColors.copyWith(
      primary: effectiveColors.primary,
      primaryContainer: (usedColors > 2 && usedColors != 7)
          ? effectiveColors.primaryContainer
          : effectiveColors.primary.darken(5).blend(Colors.black, 55),
      secondary: (usedColors > 1 || usedColors == 7)
          ? effectiveColors.secondary
          : effectiveColors.primary.darken().brighten(20),
      secondaryContainer: (usedColors > 3 && usedColors != 7)
          ? effectiveColors.secondaryContainer
          : usedColors > 1
              ? effectiveColors.secondary.darken(25).blend(Colors.black, 50)
              : effectiveColors.primary
                  .darken()
                  .brighten(20)
                  .blend(Colors.black, 40),
      tertiary: (usedColors > 4 || usedColors == 7)
          ? effectiveColors.tertiary
          : effectiveColors.primary.brighten(15),
      tertiaryContainer: (usedColors > 5 && usedColors != 7)
          ? effectiveColors.tertiaryContainer
          : usedColors > 4
              ? effectiveColors.tertiary.darken(15).blend(Colors.black, 60)
              : effectiveColors.primary
                  .brighten(15)
                  .darken(20)
                  .blend(Colors.black, 30),
      appBarColor: colors.appBarColor,
      error: colors.error ?? FlexColor.materialDarkError,
      errorContainer: colors.errorContainer ??
          FlexColor.darkErrorContainer(
              colors.error ?? FlexColor.materialDarkError),
    );
  } else {
    // Return effective colors as computed in versions before 4, we do thus
    // not break version 4 API.
    return effectiveColors.copyWith(
      primary: effectiveColors.primary,
      primaryContainer: (usedColors > 2 && usedColors != 7)
          ? effectiveColors.primaryContainer
          : effectiveColors.primary.darken(kDarkenPrimaryContainer),
      secondary: (usedColors > 1 || usedColors == 7)
          ? effectiveColors.secondary
          : effectiveColors.primary.darken(kDarkenSecondary),
      secondaryContainer: (usedColors > 3 && usedColors != 7)
          ? effectiveColors.secondaryContainer
          : usedColors > 1
              ? effectiveColors.secondary
                  .darken(kDarkenSecondaryContainerFromSecondary)
              : effectiveColors.primary.darken(kDarkenSecondaryContainer),
      tertiary: (usedColors > 4 || usedColors == 7)
          ? effectiveColors.tertiary
          : effectiveColors.primary.lighten(kDarkenPrimaryContainer),
      tertiaryContainer: (usedColors > 5 && usedColors != 7)
          ? effectiveColors.tertiaryContainer
          : usedColors > 4
              ? effectiveColors.tertiary.lighten(kDarkenSecondaryContainer)
              : effectiveColors.primary
                  .brighten(kDarkenSecondary * 2)
                  .lighten(kDarkenSecondaryContainer),
      appBarColor: colors.appBarColor,
      error: colors.error,
      errorContainer: colors.errorContainer,
    );
  }
}