如何在Flutter中更改整个主题的文本颜色?

如何在Flutter中更改整个主题的文本颜色?

问题描述:

我可能缺少明显的东西。是否有一个 属性可以更改Flutter应用程序中所有文本的颜色?

There is probably something obvious I'm missing. Is there one property that can change the color of all the text in a Flutter app?

我现在的操作方式是我的MaterialApp:

The way I am doing it now is, in my MaterialApp:

theme: ThemeData(
    textTheme: Theme.of(context).textTheme.copyWith(
          body1:
              Theme.of(context).textTheme.body1.apply(color: Colors.pink),
          body2:
              Theme.of(context).textTheme.body2.apply(color: Colors.pink),
          display1:
              Theme.of(context).textTheme.display1.apply(color: Colors.pink),
          display2:
              Theme.of(context).textTheme.display2.apply(color: Colors.pink),
          ... // and so on
        ),
    ),
),

我也尝试过

textTheme: Theme.of(context).textTheme.apply(bodyColor: Colors.pink),

但这适用于下拉文本,而不是常规文本。同样, displayColor 适用于appBar文本和InputDecoration文本,但不适用于常规文本。我的代码中似乎没有任何 decorationText ,因此我不确定该代码的用途。

but this applies to Dropdown text, not regular text. Likewise, displayColor applies to the appBar text and a InputDecoration text, but not regular text. I don't seem to have any decorationText in my code so I'm not sure what that one is for.

我注意到有一个 textSelectionColor 属性,但这仅适用于 TextField 小部件。

I note there is a textSelectionColor property but that only applies for TextField widgets.

我认为 TextTheme.apply 是您想要的。 bodyColor 将应用于标题 title subhead 按钮 body1 body2 displayColor 将通过 display4 应用于 display1 ,并且标题。如果同时指定 bodyColor displayColor 并使用相同的颜色值,则将有效地更改所有文本样式的文本颜色。

I think TextTheme.apply is what you want. bodyColor will be applied to headline, title, subhead, button, body1, and body2. displayColor will be applied to display1 through display4, and caption. If you specify both bodyColor and displayColor and use the same color value, that will effectively change text colors on all text styles.

示例:

final newTextTheme = Theme.of(context).textTheme.apply(
  bodyColor: Colors.pink,
  displayColor: Colors.pink,
);