9

Just starting out with Flutter + Material design. I notice that when a create a theme using ThemeData, if I use something like this:

ThemeData(
  textTheme: TextTheme(bodyText1: TextStyle(color: Colors.white)),
)

It doesn't seem to colour the text in a simple GridView with Text widgets with the colour white. However, if I change the above to use bodyText2 it does.

What is the logic behind bodyText2 being used for text across the app? Is there a good place to reference which text style names get used and why in a theming situation? Is this all just knowledge acquired through trial-and-error or are there some good catch-all rules for which styles get used in which circumstances?

Thanks.

shennan
  • 10,798
  • 5
  • 44
  • 79
  • You can read the specifications and details here: https://api.flutter.dev/flutter/material/TextTheme-class.html – Midhun MP Mar 31 '21 at 11:31
  • 3
    Yeah I've seen that breakdown. This gives a good _what_ but not a very good _why_. I get that different names map to different font specs; what is not so obvious is what defaults are used throughout the application itself. Why does changing ` `bodyText2` and not `bodyText1` seem to be used in my widgets? Can I be certain that `bodyText2` will always be used if I don't pick a specific text theme at the local level? The answers to these questions seem absent from the documentation. – shennan Mar 31 '21 at 11:49

2 Answers2

6

Update: If using 2021 styles, not 2018 styles, swap bodyText2 (2018 style), for bodyMedium (2021 style). End update.

I ran into the same issue and did some research on this.

As per the official documentation for Text :

The style argument is optional. When omitted, the text will use the style from the closest enclosing DefaultTextStyle.

And the explanation for bodyText2 in TextTheme:

The default text style for Material.

Now the answer is quite clear. If your Text widget does not have any explicitly given text style, and has no inherited text style from its ancestors in the way of placing a DefaultTextStyle at some nodes. Then it would use the value in bodyText2 from the theme.

BeniaminoBaggins
  • 11,202
  • 41
  • 152
  • 287
Evan
  • 430
  • 6
  • 16
  • 2
    I'll accept this as the answer. You're right in that `bodyText2` is used in scenarios when no text style is specified. However, it seems to be an unintuitive default; and it would be nice if there was some more information as to _why_ this style is default and whether it was picked based on some cultural or logical principle. – shennan May 26 '21 at 16:21
0

You are read documentation related to themes here : https://flutter.dev/docs/cookbook/design/themes

They have explained with example.

Muhammad Ashir
  • 418
  • 2
  • 14
  • As per previous comment; these docs don't explain why `bodyText2` is used for an unstyled widget. Which default styles are used in which situation is a bit of a black box. – shennan Mar 31 '21 at 12:04