-
My primary color is #65BB46, and the FlexColorScheme generate a dark onPrimary for best contrast accordingly, but I need it to be white for branding reasons, even if contrast is downgraded. How can I do that? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hi @Luwx and thanks for your question! TLDRHow about just setting Longer explanationWith the FlexColorScheme API you can easily override the generated colors, or the pre-defined ones set via For example: theme: FlexThemeData.light(
useMaterial3: true,
scheme: FlexScheme.redWine,
onPrimary: Colors.yellow, // <== override onPrimary color to YELLOW
blendLevel: 2,
appBarOpacity: 0.98,
subThemesData: const FlexSubThemesData(
inputDecoratorRadius: 8,
inputDecoratorPrefixIconSchemeColor: SchemeColor.primary,
inputDecoratorSchemeColor: SchemeColor.primary,
inputDecoratorBackgroundAlpha: 0x18,
inputDecoratorUnfocusedHasBorder: false,
appBarScrolledUnderElevation: 6,
popupMenuOpacity: 0.96,
bottomNavigationBarOpacity: 0.96,
navigationBarOpacity: 0.96,
navigationRailOpacity: 0.96,
),
keyColors: const FlexKeyColors(),
), FlexColorScheme contains many more flexibility and convenience API's than those used and settable via the Themes Playground. This particular yellow Result of above theme: From the doc comment about /// A color that is clearly legible when drawn on [primary] color.
///
/// To ensure that an app is accessible, a contrast ratio of 4.5:1 for
/// [primary] and [onPrimary] is recommended. See
/// <https://www.w3.org/TR/UNDERSTANDING-WCAG20/visual-audio-contrast-contrast.html>.
///
/// When using this extension, this is an override color for the color that
/// would be used based on the corresponding color property defined in
/// [FlexSchemeColor] [colors] property or when using pre-defined color
/// scheme based [FlexScheme] and its [scheme] property, including any
/// used blend logic. If a [colorScheme] was provided with this
/// corresponding color defined, this color property will override the
/// same color in it as well.
///
/// You can use this property for convenience if you want to override the
/// color that this scheme color gets via the extensions factory behavior.
final Color? onPrimary, In Themes PlaygroundIn the Themes Playground, you can define and ask for main onColors to always be white or black, but they will be so based on "correct" contrast for the color. There is an algorithm (provided by Flutter SDK) for determining if a color is "light" and should have a dark contrasting color, or if it is "dark" and should have a light contrasting color. The white/black auto selection here is based on that. The color you mention However, the color In that case the override above should give you what you need. Testing with your use case colorI'm curious what it looks like so, here first with default dark darkTheme: FlexThemeData.dark(
colors: FlexSchemeColor.from(
primary: const Color(0xff65bb46),
),
surfaceMode: FlexSurfaceMode.levelSurfacesLowScaffold,
blendLevel: 11,
subThemesData: const FlexSubThemesData(
blendOnLevel: 20,
blendOnColors: false,
useTextTheme: true,
useM2StyleDividerInM3: true,
alignedDropdown: true,
useInputDecoratorThemeInDialogs: true,
),
keyColors: const FlexKeyColors(
keepPrimary: true,
),
visualDensity: FlexColorScheme.comfortablePlatformDensity,
useMaterial3: true,
swapLegacyOnMaterial3: true,
), Then with the darkTheme: FlexThemeData.dark(
onPrimary: Colors.white, // <=== Set to white here!
colors: FlexSchemeColor.from(
primary: const Color(0xff65bb46),
),
surfaceMode: FlexSurfaceMode.levelSurfacesLowScaffold,
blendLevel: 11,
subThemesData: const FlexSubThemesData(
blendOnLevel: 20,
blendOnColors: false,
useTextTheme: true,
useM2StyleDividerInM3: true,
alignedDropdown: true,
useInputDecoratorThemeInDialogs: true,
),
keyColors: const FlexKeyColors(
keepPrimary: true,
),
visualDensity: FlexColorScheme.comfortablePlatformDensity,
useMaterial3: true,
swapLegacyOnMaterial3: true,
), Result: Imo, it looks like white works pretty well contrast wise on this color too, as I suspected 😅 Hope this helps! BR Related questionsThis question has some relation to these two questions and answers: |
Beta Was this translation helpful? Give feedback.
Hi @Luwx and thanks for your question!
TLDR
How about just setting
onPrimary
toColors.white
?Longer explanation
With the FlexColorScheme API you can easily override the generated colors, or the pre-defined ones set via
scheme
orcolors
API, it even overrides the color set in a passedcolorScheme
.For example: