-
Notifications
You must be signed in to change notification settings - Fork 26.8k
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
ThemeData.copyWith() doesn't update dependent themes #22913
Comments
Seems like the problem stems from the fact that |
We have same issue. We created a light and a dark theme that should preferably inherit shared properties from a base themedata object. But using copyWith on the base theme will not work, so the only workaround for now is to have redundant properties in both the light and the dark theme. |
Same issue here. I think its The constructor of ThemeData did this, so it works fine. buttonTheme ??= ButtonThemeData(
colorScheme: colorScheme,
buttonColor: buttonColor,
disabledColor: disabledColor,
highlightColor: highlightColor,
splashColor: splashColor,
materialTapTargetSize: materialTapTargetSize,
); Can we solve this problem? Because this behavior was used in the code of this codalabs and caused the wrong effect. // TODO: Build a Shrine Theme (103)
final ThemeData _kShrineTheme = _buildShrineTheme();
ThemeData _buildShrineTheme() {
final ThemeData base = ThemeData.light();
return base.copyWith(
accentColor: kShrineBrown900,
primaryColor: kShrinePink100,
buttonColor: kShrinePink100,
scaffoldBackgroundColor: kShrineBackgroundWhite,
cardColor: kShrineBackgroundWhite,
textSelectionColor: kShrinePink100,
errorColor: kShrineErrorRed,
// TODO: Add the text themes (103)
// TODO: Add the icon themes (103)
// TODO: Decorate the inputs (103)
);
} |
I think copyWith method or buttonTheme should be refactored due to current mechanism could cause side effect |
I was able to reproduce in The flutter/packages/flutter/lib/src/material/theme_data.dart Lines 1107 to 1253 in 54c9441
flutter doctor -v
|
This issue still exists. Updating labels to reflect the same |
Summary: Adding colors to ButtonThemeData (#22013) highlighted a general problem with
ThemeData.copyWith
: subordinate themes (like ThemeData.buttonTheme) aren't updated when a ThemeData property they depend on is changed.ThemeData.copyWith has simple semantics: it updates the specified fields, but not dependent fields. For example there are 10 subordinate themes, each with its own set of ThemeData dependencies:
None of the subordinate themes are changed if (say) a color they depend on is changed with ThemeData.copyWith(). For example:
myThemeData.copyWith(buttonColor: myButtonColor)
doesn't changemyThemeData.buttonTheme
.We could add a
ThemeData.apply
method, likeTextTheme.apply
but more complicated. The basic idea would be to update all of the subordinate themes that depend on ThemeData properties like primaryColor and accentColor.To implement ThemeData.apply, we'd have to implement colorScheme.apply, buttonTheme.apply etc.
Documenting ThemeData.apply would be complicated itself because each subordinate theme has its own set of ThemeData dependencies.
That said, in the applications I've looked at - usually - where developers use ThemeData.copyWith they're usually careful to rebuild the subordinate theme they're trying to effect. The ButtonTheme is an exception because it's new, and see #22711.
For the moment I'm inclined to leave things as they are. Some improvements in the docs (including some examples) would help.
The text was updated successfully, but these errors were encountered: