在 Theme 中设置 TextFromField 样式

Nov*_*evi 13 dart flutter flutter-layout

因此,在我的 flutter 应用程序中,我创建 TextFormField 用作输入,如下所示(并将其返回到脚手架中):

final password = TextFormField(
      controller: passController,
      autofocus: false,
      obscureText: true,
      decoration: InputDecoration(hintText: 'Password'),
      style: new TextStyle(color: Colors.orange),
);
Run Code Online (Sandbox Code Playgroud)

我想更改stylea 内的属性themeData,但找不到要指定的属性。

我能找到的最接近的一个是textTheme: new TextTheme(body1: new TextStyle(color: Colors.orange)),但是这个对我的 TextFormField 没有任何作用。

如何设置 TextFormField 样式?请帮忙,我对 Dart 和编程都很陌生。我现在 13 岁,没有人能帮我解决这些事情。

PS:如果需要,完整的代码位于 GitHub 上: https: //github.com/Legolaszstudio/novynotifier

Bli*_*Dev 11

更新:

如果您想按 Flutter 应用程序的主题更改 TextField 中的文本。现在是 TextTheme 的 subtitle1。

textTheme: TextTheme(
    subtitle1: TextStyle(color: Colors.blue),
)
Run Code Online (Sandbox Code Playgroud)

  • 这有效。不幸的是,`subTitle1` 不能在 Material Design 3 上使用。而是使用 `titleMedium`。 (4认同)
  • 奇怪的是,Flutter 允许在主题上设置所有其他文本样式,例如标签样式,但不允许设置输入样式本身。 (3认同)

Trầ*_*Tâm 6

嗯!这是一个很长的问题。TextFormField是 的子类TextField。默认样式为TextField可以从下面的源中找到。

\n\n
final TextStyle style = themeData.textTheme.subhead.merge(widget.style);\n
Run Code Online (Sandbox Code Playgroud)\n\n

因此,您的源代码有两种解决方案。

\n\n

解决方案1

\n\n
    \n
  • 删除style属性输入到password.
  • \n
\n\n
final password = TextFormField(\n  controller: passController,\n  autofocus: false,\n  obscureText: true,\n  decoration: InputDecoration(hintText: 'Password'),\n  style: new TextStyle(color: Colors.orange), // \xe2\x98\x85 => Delete this.\n);\n
Run Code Online (Sandbox Code Playgroud)\n\n
    \n
  • 定义自定义subhead样式DataTheme样式并将其输入到其中。
  • \n
\n\n
MaterialApp(\n  theme: ThemeData(\n    textTheme: TextTheme(\n      subhead: TextStyle(color: Colors.orange),\n    ),\n  ),\n  home: null,\n)\n
Run Code Online (Sandbox Code Playgroud)\n\n

解决方案2

\n\n
    \n
  • 定义自定义subhead样式DataTheme样式并将其输入到其中。
  • \n
\n\n
MaterialApp(\n  theme: ThemeData(\n    textTheme: TextTheme(\n      subhead: TextStyle(color: Colors.orange),\n    ),\n  ),\n  home: null,\n)\n
Run Code Online (Sandbox Code Playgroud)\n\n
    \n
  • 将此副标题样式复制到password
  • \n
\n\n
final themes = Theme.of(context);\nfinal password = TextFormField(\n  controller: passController,\n  autofocus: false,\n  obscureText: true,\n  decoration: InputDecoration(hintText: 'Password'),\n  style: themes.textTheme.subhead, // \xe2\x98\x85 => Update this.\n);\n
Run Code Online (Sandbox Code Playgroud)\n

  • subhead 现已弃用,请使用 subtitle2 ;) (4认同)

Joe*_*ler 5

2021 - 2022

排版的 Material 规范在2021 年发生了显着变化,现在使用显示、标题、标题、正文和标签类型。titleMedium 会影响所有 TextFields 和 TextFormFields。

TextTheme 类文档中也强调了这一事实。

如果您将 titleMedium TextStyle 用于其他用途,您可以轻松地将 TextFields 和 TextFormFields 包装在主题小部件中,并专门为这些小部件更新此样式,如下所示:

Theme(
        data: Theme.of(context).copyWith(
            textTheme: Theme.of(context).textTheme.copyWith(
                  titleMedium: TextStyle(
                    fontSize: 12, 
                    fontWeight: FontWeight.w300,
                  ),
                )),
        child: TextField()
      ),
Run Code Online (Sandbox Code Playgroud)