Skip to content

Conversation

hasali19
Copy link

@hasali19 hasali19 commented Jul 7, 2024

Description

Adds the new tone-based colors from Flutter 3.22 to the color scheme.

On API >= 34, the color values are retrieved directly from the Android OS.

On API >= 31, the existing core palette will continue to be used, with the mappings taken from https://github.com/material-components/material-components-android/blob/master/docs/theming/Color.md.

Issues

Fixes #574, #582

Checklist

@hasali19 hasali19 requested a review from a team as a code owner July 7, 2024 22:39
@hasali19 hasali19 requested review from guidezpl and removed request for a team July 7, 2024 22:40
@Henry-Hiles
Copy link

I'm trying to use this, but I get this error: When a TonalPalette is created with fromList, tone must be one of [0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 95, 99, 100]

@hasali19
Copy link
Author

hasali19 commented Jul 9, 2024

Interesting...I don't think I've changed anything related to that. Does the latest main branch work fine for you?

@Henry-Hiles
Copy link

Interesting...I don't think I've changed anything related to that. Does the latest main branch work fine for you?

Well, from pub it doesn't have the correct colours, but no errors. I switched the pubspec to use this fork, and boom: error. I'll try main asap.

@Henry-Hiles
Copy link

Interesting...I don't think I've changed anything related to that. Does the latest main branch work fine for you?

Latest main works fine. Just this branch :)

@Henry-Hiles
Copy link

I'm using DynamicColorBuilder ofc.

@hasali19
Copy link
Author

hasali19 commented Jul 9, 2024

Which flutter version are you using?

@Henry-Hiles
Copy link

quadradical@quadraticpc ~/D/C/qlib (master)> flutter --version
Flutter 3.22.2 • channel stable • https://github.com/flutter/flutter.git
Framework • revision nixpkgs000 () • 1970-01-01 00:00:00
Engine • revision edd8546116
Tools • Dart 3.4.3 • DevTools 2.34.3

@hasali19
Copy link
Author

hasali19 commented Jul 9, 2024

Ok, I think I see the problem. Support for arbitrary colour tones were added in material_color_utilities 0.11.0, but stable flutter depends on 0.8.0. I just realised I was using the beta channel, which is why I didn't run into it.

I think the solution will be to sidestep material_color_utilities and try to get the colours from the native side for the API < 34 case as well.

@Henry-Hiles
Copy link

Oh, I see, thanks!

@hasali19
Copy link
Author

hasali19 commented Jul 9, 2024

I've taken a different approach of implementing logic to "guess" tones that are missing from the common tones list, based on the implementation in material_color_utilities. This will be needed until flutter supports the new version.

@Henry-Hiles Can you confirm if this works for you now?

@Henry-Hiles
Copy link

Sure, I'll test right now!

@Henry-Hiles
Copy link

While this no longer has an error, it still has the same issues as without this PR (very similar colors so nothing looks correct)

@Henry-Hiles
Copy link

Screenshot_20240709-185210_QJobProcessor

@Henry-Hiles
Copy link

As you can see, the navbar and background are the same, which isn't normal.

@hasali19
Copy link
Author

hasali19 commented Jul 9, 2024

I can't seem to reproduce that. Do you have an example repo/code you can share?

Screenshot_1720566342

@Henry-Hiles
Copy link

I can't seem to reproduce that. Do you have an example repo/code you can share?

Screenshot_1720566342

Hmm it happened when I upgraded to 3.22.2, I'll see if I can make a test repo.

@Henry-Hiles
Copy link

Nevermind deleting pub cache and re-getting fixed it! Thank you for this!

@resucutie
Copy link

After building my application using it, the primary color isn't accurate
image
image

@hasali19
Copy link
Author

Which android version are you using?

@resucutie
Copy link

resucutie commented Jul 14, 2024

Android 14. I could give the project to you try it out seeing what's wrong

@hasali19
Copy link
Author

@resucutie Thanks, I'll take a look.

Could you also share your flutter version please?

@hasali19
Copy link
Author

From what I can see this is working as expected; Android's quick settings tiles use a slightly different shade of the primary colour.

You can see here that the shadeActive colour (used by the Android system for the quick settings tiles) uses tone 90 of the primary colour, while the material library defines primary in the colour scheme as tone 40 in light mode and 80 in dark mode.

@resucutie
Copy link

Welp, what about in comparasion to other apps? It looks odd, or I guess it is the consensus here, like if it is using the wrong color shade, but I am not sure

@hasali19
Copy link
Author

Do you mean you are seeing different colours compared to other apps using the same dynamic colour scheme?

This implementation should be using the same colours as those used by the official native material library. If this is not the case, please share a repro.

@resucutie
Copy link

I've (finally) tested it and apparently it indeed take the correct colors. It is a bit odd that it looks different on an emulator
Screenshot_20240730-170947_LocalBooru.png

Screenshot_20240730-170950_Settings.png

PR is good I think

@wildsylvan
Copy link

wildsylvan commented Aug 9, 2024

in our case, after activating dynamic colors (using this commit):
image
primary container e secondary container became the same color!


look at the cards (list a/b and the new list fab. they should be different colors but with dynamic colors they become the same)

without dynamic colors (correct)

Screenshot_2024-08-09-07-07-49-47_304e098f875053d2a3a592b681756698

with dynamic colors (wrong)

Screenshot_2024-08-09-07-07-28-30_304e098f875053d2a3a592b681756698

@hasali19
Copy link
Author

hasali19 commented Aug 9, 2024

Please can you share your flutter and android versions, device information, debug logs and a link to a code repo if possible.

@wildsylvan
Copy link

wildsylvan commented Aug 9, 2024

thanks for the quick reply! here:

[√] Flutter (Channel beta, 3.24.0-0.2.pre, on Microsoft Windows [Version 10.0.22631.3880], locale en-US)
[√] Windows Version (Installed version of Windows is version 10 or higher)
[√] Android toolchain - develop for Android devices (Android SDK version 35.0.0)
[√] Chrome - develop for the web
[X] Visual Studio - develop Windows apps
    X Visual Studio not installed; this is necessary to develop Windows apps.
      Download at https://visualstudio.microsoft.com/downloads/.
      Please install the "Desktop development with C++" workload, including all of its default components
[√] Android Studio (version 2024.1)
[√] VS Code (version 1.92.1)
[√] Connected device (3 available)
[√] Network resources

testing on a real phone, oneplus 8t with android 14
image


i can provide a repro next week... it's very vanilla use of material components tho, not many customizations

@wildsylvan
Copy link

this is the oneplus color settings:
image

@wildsylvan
Copy link

ok these impact how colors are shown in the app:
image

but the difference between primary container e secondary container remains basically non-existent

@wildsylvan
Copy link

@hasali19 what would be the best way to ignore completely oneplus color presets, just get the "main accent color" from android and utilize that color to build a correct material 3 color palette?

@hasali19
Copy link
Author

hasali19 commented Aug 9, 2024

Thanks for the info. Given that you are using Android 14 where the colours are retrieved directly from the system without any conversion, it is likely OnePlus is simply reporting both colours as the same.

One thing you could try is to force use of the fallback method used for older versions of Android, e.g:

final corePalette = await DynamicColorPlugin.getCorePalette();
final lightDynamic = corePalette?.toColorScheme(brightness: Brightness.light);
final darkDynamic = corePalette?.toColorScheme(brightness: Brightness.dark);

If you just want a specific "accent" colour, you can pick one from the fields on the colour schemes provided by DynamicColorBuilder.

@wildsylvan
Copy link

wildsylvan commented Aug 10, 2024

update


final corePalette = await DynamicColorPlugin.getCorePalette();
final lightDynamic = corePalette?.toColorScheme(brightness: Brightness.light);
final darkDynamic = corePalette?.toColorScheme(brightness: Brightness.dark);

doesn't fix the issue


my colleague tested with his Pixel 7 Pro (and also inside an emulator) and he has the same issue

the issue seems to be present only in light mode tho (and our theme settings don't have any special settings for only one of the modes):

wrong in light mode

image

correct in dark mode

image

@wildsylvan
Copy link

wildsylvan commented Aug 10, 2024

this is the card (the one with the text Test):
image

this is the fab:
image

and this is the theme config:
image

@hasali19
Copy link
Author

Although it's subtle, the colours in your screenshot do appear to be slightly different. I'm also able to reproduce very similar looking colours in light mode when running in an emulator.

image

@wildsylvan
Copy link

wildsylvan commented Aug 10, 2024

yeah i was just about to write it! they are a little different... so it's more of a general issue with the way the m3 color generator works?

@wildsylvan
Copy link

with a green wallpaper the difference is less subtle too...

image

@hasali19
Copy link
Author

Right, I guess it's just an artifact of whatever algorithm Android uses to pick the system colours.

@wildsylvan
Copy link

wildsylvan commented Aug 10, 2024

well to be fair the material theme builder shows it as well with the blues (tho the difference is a little more visible):
image

while with the greens the difference is much less subtle:
image

@guidezpl is it supposed to be that different between hues?

petlyh added a commit to petlyh/JS-Dict that referenced this pull request Sep 24, 2024
While `surfaceVariant` has been deprecated in favor of `surfaceContainerHighest`, the `dynamic_color` package does not yet support it, leading to an incorrect palette.

Ignoring the deprecation warnings while waiting for material-foundation/flutter-packages#599.
@im-trisha
Copy link

It seems like the card color is incorrect... Is it just me or...?

Not just the card color, but also the bottom nav bar and many other colors seem to be washed off (see, the one on the left is way more purple)

image

@hasali19
Copy link
Author

hasali19 commented Jan 1, 2025

Given that nobody appears to be maintaining this repo, I have published my fork to pub.dev as dynamic_system_colors.

@guidezpl
Copy link
Collaborator

guidezpl commented Aug 1, 2025

Hi, this repo is maintained sporadically when I have time, but also it is summer and I am on vacation :) please be patient and I will review when back

@guidezpl guidezpl self-assigned this Aug 3, 2025
ColorScheme _toLightColorScheme(CorePalette corePalette) {
return ColorScheme(
brightness: Brightness.light,
primary: Color(corePalette.primary.get(40)),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately, these values only work for one type of scheme, there are many types. See https://github.com/material-foundation/material-color-utilities/blob/main/dev_guide/creating_color_scheme.md

}

// This logic is taken from material_color_utilities 0.12 - https://github.com/material-foundation/material-color-utilities/blob/be615fc90286787bbe0c04ef58a6987e0e8fdc29/dart/lib/palettes/tonal_palette.dart#L93C5-L111.
// Once flutter updates to the latest version, this workaround can be removed.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Best to wait for flutter/flutter#170000 rather than introduce workarounds IMO

);
}
}

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is somewhat fragile an difficult to validate correctness with DynamicColorPlugin.kt

@RequiresApi(Build.VERSION_CODES.UPSIDE_DOWN_CAKE)
private fun getColorScheme(resources: Resources): IntArray {
return intArrayOf(
// light
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not obtain system_palette_key_color_primary_light and other key colors and generate the scheme directly?

@guidezpl
Copy link
Collaborator

guidezpl commented Sep 2, 2025

To anyone on this PR with issues, please create a new separate issue if appropriate, or refer to an existing one, it's really hard to follow the discussion here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Using dynamic_color on Flutter 3.21.0-1.0.pre.2 yields weird/incorrect color palette
7 participants