-
Notifications
You must be signed in to change notification settings - Fork 30.2k
Merge custom TextStyle with Theme.of by default across all material widgets #12681
Description
A number of our widgets take TextStyle to allow developers customize the look of text. For example, InputDecoration takes labelStyle, helperStyle, hintStyle, etc. It is desirable that the least path of resistance for developers using these options lead to reasonable results.
Currently we expect that the developer supplies a TextStyle that has all the required style properties. That requires that either they call the default TextStyle constructor passing all the properties, ensure that the text they are styling appears under a DefaultTextStyle that supplies the missing properties, or they create a copy of another style (e.g. from Theme.of) replacing a subset of properties. Neither of the options are along the path of least resistance. Developers want to use the default constructor and pass a subset of properties, like so:
new InputDecoration(
labelText: new TextStyle(fontSize: 5.0),
)This leads to broken behavior. The text style is incomplete (e.g. its missing fontFamily). Lerping this style with a style that comes from Theme.of results in an error, because this style's inherit: true while Theme.of has its set to false. There are probably other bugs that we haven't observed yet.
Proposal
Classes that take TextStyle as arguments should TextStyle.merge it with Theme.of. This would fill in the gaps in the passed style, but also retains the option of allowing the developer to do the merging manually or supply their own complete text style. All they need to do is pass a TextStyle with inherit: false, which will just use the style the developer set. Of course, in that case it is up to the developer to make sure their style is complete.
Widgets to revise
Material: many of the material widgets already use DefaultTextStyle populated from Theme.of and should behave correctly, but it's worth reviewing them and adding missing tests.
InputDecorationInputDecoratorMaterialChipDropdownButtonTabBarTextField
Widgets: these are tricky because Theme is part of the material library, not widgets. So perhaps widgets should either assume they are under a DefaultTextStyle or the received TextStyle is self-sufficient.
WidgetsAppBannerPainterEditableTextAnimatedDefaultTextStyle