Skip to content

Merge custom TextStyle with Theme.of by default across all material widgets #12681

@yjbanov

Description

@yjbanov

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.

  • InputDecoration
  • InputDecorator
  • Material
  • Chip
  • DropdownButton
  • TabBar
  • TextField

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.

  • WidgetsApp
  • BannerPainter
  • EditableText
  • AnimatedDefaultTextStyle

/cc @mehmetf @Hixie

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2Important issues not at the top of the work listc: API breakBackwards-incompatible API changesc: proposalA detailed proposal for a change to Flutterf: material designflutter/packages/flutter/material repository.frameworkflutter/packages/flutter repository. See also f: labels.team-designOwned by Design Languages teamtriaged-designTriaged by Design Languages team

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions