【问题标题】:Flutter ThemeData is not working for the TextFlutter ThemeData 不适用于文本
【发布时间】:2019-01-10 10:46:01
【问题描述】:

代码:

@override
Widget build(BuildContext context) {
  return Theme(
    data: ThemeData(textTheme: TextTheme(body1: TextStyle(fontSize: 40))),
    child: Text("Hello World!"), // size not changing
  );
}

但是当我使用

data: ThemeData(textTheme: TextTheme(body1: TextStyle(fontSize: 40))),

在我的MaterialApptheme 中,Text 的大小会发生变化。


PS:我知道我可以通过使用 style: 属性给 Text 一个大小,但我想知道为什么我的代码没有改变 Text 字体大小。

【问题讨论】:

  • 但为什么要使用body1?例如,为什么不body2?或任何其他TextStyle
  • @pskink 我使用body1 尝试过它并且它有效(当在MaterialApp 中应用主题时)

标签: dart flutter


【解决方案1】:

Text 不使用ThemeTheme 是特定于材料设计的,而 Text 是通用目的。

Text 使用的是DefaultTextStyle,它由MaterialApp(或其他一些小部件,例如AppBar)使用来自Theme 的值进行编辑。

以下应该有效:

DefaultTextStyle(
  style: TextStyle(fontSize: 40),
  child: Text("Hello World"),
);

【讨论】:

    猜你喜欢
    • 2020-06-14
    • 2020-08-13
    • 2019-12-31
    • 2022-07-19
    • 2020-04-08
    • 2022-01-15
    • 1970-01-01
    • 1970-01-01
    • 2021-06-19
    相关资源
    最近更新 更多