Thanks to visit codestin.com
Credit goes to github.com

Skip to content

ThemeData.brightness == ThemeData.colorScheme.brightness #56956

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
May 18, 2020

Conversation

HansMuller
Copy link
Contributor

@HansMuller HansMuller commented May 11, 2020

The overall Material Theme should have one brightness value which indicates if the theme represents a light (default) or dark color scheme per https://material.io/design/color/dark-theme.html.

The ThemeData.brightness and ThemeData.colorScheme.brightness properties are redundant and currently they are not kept in sync. We are evolving the Theme's colorScheme as the primary source of default material components colors as well the overall theme's brightness.

This PR changes ThemeData.brightness to be a simple getter that returns colorScheme.brightness. It's part of the changes recommended in "Compute brightness, primaryColorBrightness, accentColorBrightness" section of flutter.dev/go/material-theme-system-updates.

@fluttergithubbot fluttergithubbot added f: material design flutter/packages/flutter/material repository. framework flutter/packages/flutter repository. See also f: labels. labels May 11, 2020
@HansMuller HansMuller marked this pull request as draft May 11, 2020 23:39
@HansMuller HansMuller force-pushed the theme_data_brightness branch 2 times, most recently from a60663e to 366d868 Compare May 13, 2020 23:18
@HansMuller HansMuller marked this pull request as ready for review May 13, 2020 23:26
@HansMuller HansMuller requested a review from darrenaustin May 14, 2020 16:36
Copy link
Contributor

@darrenaustin darrenaustin left a comment

Choose a reason for hiding this comment

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

Nice, LGTM. Some minor comments and questions below.

@@ -270,8 +269,8 @@ class ThemeData with Diagnosticable {
BottomNavigationBarThemeData bottomNavigationBarTheme,
bool fixTextFieldOutlineLabel,
}) {
brightness ??= Brightness.light;
final bool isDark = brightness == Brightness.dark;
final Brightness _brightness = brightness ?? colorScheme?.brightness ?? Brightness.light;
Copy link
Contributor

Choose a reason for hiding this comment

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

This means the user could still specify a brightness that doesn't match the given colorScheme.brightness. Perhaps if they are both given and don't match we should throw an assertion? Or maybe have the colorScheme.brightness take precedence as that is what the eventual value will be?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I did the opposite. If you specify both, then the brightness parameter replaces the ColorScheme's brightness. Developers occasionally specify a brightness value in local Theme overrides, to override the the "correct" value, in order to flip light/dark color choices.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah, then maybe I am missing something because if I set the brightness here that is different from the colorScheme's when do you override the colorScheme's in this constructor? I see where this brightness is used in the constructor to set a bunch of stuff, but then the colorScheme's brightness will be used after that with the new getter.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Actually, you're right. It makes more sense to catch this (likely) error than to support it. I've added the assert and a test for it.

@@ -659,7 +655,7 @@ class ThemeData with Diagnosticable {
/// this theme is localized using text geometry using [ThemeData.localize].
factory ThemeData.light() => ThemeData(brightness: Brightness.light);

/// A default dark theme with a teal accent color.
/// A default dark theme with a teal secondary [ColorScheme] color.
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe more explicit here?

Suggested change
/// A default dark theme with a teal secondary [ColorScheme] color.
/// A default dark theme with a teal [ColorScheme.secondary] color.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

@@ -1054,6 +1045,8 @@ class ThemeData with Diagnosticable {
final bool fixTextFieldOutlineLabel;

/// Creates a copy of this theme but with the given fields replaced with the new values.
///
/// The [brightness] value is applied to the [colorScheme].
Copy link
Contributor

Choose a reason for hiding this comment

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

This seems a little odd. When would you want to change the brightness of the colorScheme without changing its colors as well? Is this because we don't want to break the existing API by removing the brightness param here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes. I found one use-case in an internal app so I'm assuming that there are others in outside apps.

I think this is comparable to allowing the ThemeData brightness constructor parameter override the color scheme's brightness.

@@ -216,7 +216,6 @@ void main() {
);

final ThemeData theme = ThemeData.raw(
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we need a test to check that the brightness is set correctly with the changes to the constructor?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point; I will add that.

@HansMuller HansMuller force-pushed the theme_data_brightness branch from df560c7 to b1c759d Compare May 16, 2020 01:24
@darrenaustin
Copy link
Contributor

That looks great. Thanks for doing this.

@fluttergithubbot fluttergithubbot merged commit 750dbe0 into flutter:master May 18, 2020
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 31, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
f: material design flutter/packages/flutter/material repository. framework flutter/packages/flutter repository. See also f: labels.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants