Open amal-stack opened 7 months ago
Also facing this exact same issue. Would be great to get this fixed, as it renders these new colours unusable whilst using dynamic colour.
Looks like it may be related in some way to #574
For anyone wanting a quick workaround for now, this is how I solved it. For each of the lightDynamic
and darkDynamic
schemes generated from dyamic_color
, I create a ColorScheme
from seed, using each one's primary colour. Then, I simple take whichever colours I need from this scheme and set them in the dynamic scheme. Here's the code I used, if anyone would like to use it:
(ColorScheme light, ColorScheme dark) _generateDynamicColourSchemes(ColorScheme lightDynamic, ColorScheme darkDynamic) {
var lightBase = ColorScheme.fromSeed(seedColor: lightDynamic.primary);
var darkBase = ColorScheme.fromSeed(seedColor: darkDynamic.primary, brightness: Brightness.dark);
var lightAdditionalColours = _extractAdditionalColours(lightBase);
var darkAdditionalColours = _extractAdditionalColours(darkBase);
var lightScheme = _insertAdditionalColours(lightBase, lightAdditionalColours);
var darkScheme = _insertAdditionalColours(darkBase, darkAdditionalColours);
return (lightScheme.harmonized(), darkScheme.harmonized());
}
List<Color> _extractAdditionalColours(ColorScheme scheme) => [
scheme.surface,
scheme.surfaceDim,
scheme.surfaceBright,
scheme.surfaceContainerLowest,
scheme.surfaceContainerLow,
scheme.surfaceContainer,
scheme.surfaceContainerHigh,
scheme.surfaceContainerHighest,
];
ColorScheme _insertAdditionalColours(ColorScheme scheme, List<Color> additionalColours) => scheme.copyWith(
surface: additionalColours[0],
surfaceDim: additionalColours[1],
surfaceBright: additionalColours[2],
surfaceContainerLowest: additionalColours[3],
surfaceContainerLow: additionalColours[4],
surfaceContainer: additionalColours[5],
surfaceContainerHigh: additionalColours[6],
surfaceContainerHighest: additionalColours[7],
);
and the actual DynamicColorBuilder
widget:
return DynamicColorBuilder(
builder: (ColorScheme? lightDynamic, ColorScheme? darkDynamic) {
ColorScheme lightScheme, darkScheme;
if (lightDynamic != null && darkDynamic != null) {
(lightScheme, darkScheme) = _generateDynamicColourSchemes(lightDynamic, darkDynamic);
} else {
// logic to set standard static themes here
}
return MaterialApp(...);
},
);
The solution I think will work is described in https://github.com/material-foundation/flutter-packages/issues/574#issuecomment-2060494544
For anyone wanting a quick workaround for now, this is how I solved it. For each of the
lightDynamic
anddarkDynamic
schemes generated fromdyamic_color
, I create aColorScheme
from seed, using each one's primary colour. Then, I simple take whichever colours I need from this scheme and set them in the dynamic scheme. Here's the code I used, if anyone would like to use it:
Thank you very much, I'm new to Flutter and M3 so I can't think of these hacks yet (or maybe i'm just too lazy). However, while your solution is correct, you are extracting colors from the generated scheme then reinserting them into the same scheme, which is unnecessary.
here is my solution for now:
ColorScheme _generateColorScheme(Color? primaryColor,
[Brightness? brightness]) {
final Color seedColor = primaryColor ?? kFallbackAccentColor;
final ColorScheme newScheme = ColorScheme.fromSeed(
seedColor: seedColor,
brightness: brightness ?? Brightness.light,
);
}
return newScheme.harmonized();
}
then use it as follows:
return DynamicColorBuilder(
builder: (lightDynamic, darkDynamic) {
final ColorScheme lightScheme = _generateColorScheme(lightDynamic?.primary);
final ColorScheme darkScheme = _generateColorScheme(darkDynamic?.primary, Brightness.dark);
return MaterialApp(...);
},
);
574 (comment)
shouldn't you be inserting to the original color scheme passed to the function (which lacks the new color roles)? I think the current solution is redundant as you are getting and setting the new color roles to the same color scheme
Package
dynamic_color
Existing issue?
What happened?
Steps to reproduce:
and then:
dynamic_color
package:DynamicColorPlugin.getCorePalette
does not returnnull
(like Android).DynamicColorBuilder
widget and view its generatedColorScheme
properties, especially the new color roles likeprimaryFixed
surfaceContainer
and so on. (done usingText
widgets in the code sample below).Code Sample
The following code uses
DynamicColorBuilder
and displays some of the generated colors usingText
widgets.Expected Results
Extension method
CorePaletteToColorScheme.toColorScheme()
should assign each of these new surface and fixed color roles.Actual results
All the new color roles return the fallback colors. For instance,
primaryFixed
andprimaryFixedDim
return the fallbackprimary
because this is the behavior of Flutter'sColorScheme
as can be seen in its source:Reason
This is happening because
DynamicColorBuilder
converts aCorePalette
to a FlutterColorScheme
using the extension method onCorePalette
:CorePaletteToColorScheme.toColorScheme()
. This method still uses the deprecatedScheme.lightFromCorePalette
andScheme.darkFromCorePalette
methods and does not assign colors to the new roles.I tried to manually write my own builder using the
DynamicColorPlugin.getCorePalette
method. However, the MCU library does mention an explicit way to convert aCorePalette
to the newDynamicScheme
type (so I created an issue here.Relevant log output
No response