繁体   English   中英

如何根据主题更改 TextFormField 的文本颜色

[英]How to change text color of TextFormField according to theme

我想根据当前主题更改输入的文本颜色,因为文本颜色不是InputDecorationTheme的一部分。

在此处输入图片说明

截至目前,更改输入文本颜色的唯一可能方法是为TextFormField提供样式,但这在主题更改时也不起作用+以这种方式我需要为应用程序中可用的每个文本字段重复类似的代码。

在此处输入图片说明

您可以通过如下设置 ThemeData 来完成。

 MaterialApp(
  theme: ThemeData(
    textTheme: TextTheme(
      subtitle1: TextStyle(fontSize: 50, fontWeight: FontWeight.bold),
    ),
  )
 ...

您似乎在查看InputDecorationTheme而不是TextTheme

您正在寻找的颜色属性应该是textTheme.body1.color如下所示:

Theme.of(context).textTheme.body1.color

如果不是这个,它应该是另一个textTheme属性。

您可以通过使用TextTheme内财产小标题

 theme: ThemeData( 
    brightness: Brightness.dark,
    primaryColor: Colors.orange,
    accentColor: Colors.green,
    textTheme: TextTheme(
      subhead: TextStyle(color: Colors.blue),
    ),
  ),

或者通过使用这个:

 theme: ThemeData(
      brightness: Brightness.dark,
      primaryColor: Colors.orange,
      accentColor: Colors.green,
      textTheme: Theme.of(context)
          .textTheme
          .apply(bodyColor: Colors.red)`enter code here`

  ),

关于 Flutter 中文本样式的博客https://medium.com/flutter-community/beginners-guide-to-text-styling-in-flutter-3939085d6607

注意: Material Design 排版方案在规范的当前(2018)版本中发生了重大变化,更多信息https://material.io/design/typography

2018 规范有 13 种文本样式:

 NAME         SIZE  WEIGHT  SPACING
 headline1    96.0  light   -1.5
 headline2    60.0  light   -0.5
 headline3    48.0  regular  0.0
 headline4    34.0  regular  0.25
 headline5    24.0  regular  0.0
 headline6    20.0  medium   0.15
 subtitle1    16.0  regular  0.15
 subtitle2    14.0  medium   0.1
 body1        16.0  regular  0.5   (bodyText1)
 body2        14.0  regular  0.25  (bodyText2)
 button       14.0  medium   1.25
 caption      12.0  regular  0.4
 overline     10.0  regular  1.5

其中“light”是FontWeight.w300 ,“regular”是FontWeight.w400 ,“medium”是FontWeight.w500

[TextTheme] API 最初基于原始材料(2014)设计规范,使用不同的文本样式名称。 为了向后兼容,此 API 继续公开旧名称。 下表应该有助于理解 API 的旧名称和新名称(根据 2018 材料规范的名称)的映射。

每个 [TextTheme] 文本样式都对应于 2018 规范中的一种样式。 默认情况下,字体大小、字体粗细和字母间距与 2014 年的原始值没有变化。

 NAME       SIZE   WEIGHT   SPACING  2018 NAME
 display4   112.0  thin     0.0      headline1
 display3   56.0   normal   0.0      headline2
 display2   45.0   normal   0.0      headline3
 display1   34.0   normal   0.0      headline4
 headline   24.0   normal   0.0      headline5
 title      20.0   medium   0.0      headline6
 subhead    16.0   normal   0.0      subtitle1
 body2      14.0   medium   0.0      body1 (bodyText1)
 body1      14.0   normal   0.0      body2 (bodyText2)
 caption    12.0   normal   0.0      caption
 button     14.0   medium   0.0      button
 subtitle   14.0   medium   0.0      subtitle2
 overline   10.0   normal   0.0      overline

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM