How to modify "colorScheme" property while using custom "colors" in FlexThemeData #116
-
Hi, I'm using a customtheme where I specify the "colors" property in FlexThemeData. But I want to specify a general color for the "Divider" widget. In M2 I know you change the "diverderColor" and in M3 you change the "outlineVariant", but both properties are inside the colorSheme. If I change the property in colorsheme all my custom colors are overriden. So how I go about extending my current colorScheme without overriding everything? Hope I have explained my self more or less. Here I leave the code I have: This shows my colors correctly but not the divider color is not the one I want:
If I change and add:
The whole theme colors get override. What can i do? Thanks a lot for any time and help given. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
Hi @Flucadetena, thanks for your question. I'm assuming here that you are on Flutter 3.7 and using latest stable 6.1.2 of FCS, or perhaps even latest FCS beta 7.0.0-dev.2. Not that it really matters so much for the proposed solution further below. From Flutter 3.3 to 3.7 there has been a change in how Divider gets its default color from the surrounding theme and its colorScheme. In Flutter 3.3 it is: M2: In Flutter 3.7 it is: M2: Where theme is `Theme.of(context)'. The In upcoming FCS V7 and already in above mentioned dev beta release, this change of color source for the Divider is better considered than in 6.x versions and earlier. Additionally an optional toggle where you can continue to use the opacity M2 style based Divider in M3, is available as a quick toggle. I have not added any property to change the Divider's theme color in FCS v7, mostly because it is simple enough to add it without need for including it in FCS, and because typically the default is reasonable and there is seldom a pressing need to change it. Still sometimes there is a need to change it. Luckily if you want to set a truly unique custom color on the Since FCS in any version only sets the color property in Long story short, if you want a custom color on darkTheme: FlexThemeData.dark(...).copyWith(dividerTheme: DividerThemeData(color: Colors.red)), This will work in both M2 and M3 mode. Below you can see a red divider in the Themes Playground app when I wrapped its entire theme with above copyWith. Always red, it does not matter what I do else to the theme or which scheme I choose, and I didi not have to change any other color in theme or its ColorScheme to get it set.
If you need a color from the effective You can do this with FlexColorScheme without context since it is just a data class with functions that can return a ThemeData class, and also its ColorScheme separately that it will include in its returned ThemeData. This FCS QA has some info and related example on how to extract its ColorScheme, so you can use its colors in sub-themes. This API guide part also show a good example of this: https://docs.flexcolorscheme.com/api_guide#use-flexcolorscheme-or-flexthemedata. The FCS docs FAQ here https://docs.flexcolorscheme.com/faq#can-i-use-the-colorscheme-flexcolorscheme-creates-in-custom-component-themes also contain info on this topic, as does this FAQ https://docs.flexcolorscheme.com/faq#how-do-i-modify-the-component-themes-with-custom-features, that discuss the steps when you want to override a property in a component theme and keep everything else it as it was, which we do not have to do in this particular If you are not doing the custom With the Divider case it would be: Theme(
// Find and extend the parent theme using `copyWith`. See the next
// section for more info on `Theme.of`.
data: Theme.of(context).copyWith(dividerTheme: DividerThemeData(color: Colors.red)),
child: ... // Widget branch with red Dividers
); Sorry way too long answer, hope it helps and maybe provides some additional useful insights into the wondrous world of Flutter app theming 😃 Cheers! |
Beta Was this translation helpful? Give feedback.
Hi @Flucadetena, thanks for your question.
I'm assuming here that you are on Flutter 3.7 and using latest stable 6.1.2 of FCS, or perhaps even latest FCS beta 7.0.0-dev.2. Not that it really matters so much for the proposed solution further below.
From Flutter 3.3 to 3.7 there has been a change in how Divider gets its default color from the surrounding theme and its colorScheme.
In Flutter 3.3 it is:
M2:
theme.dividerColor
M3:
theme.dividerColor
In Flutter 3.7 it is:
M2:
theme.dividerColor
M3:
theme.colorScheme.outlineVariant
Where theme is `Theme.of(context)'.
The
dividerColor
is on a deprecation roadmap in Flutter, see: flutter/flutter#91772In upcoming FCS V7 and already in above menti…