【问题标题】:Set TextFromField style in Theme在 Theme 中设置 TextFromField 样式
【发布时间】:2019-11-18 19:29:38
【问题描述】:

所以在我的颤振应用程序中,我创建了 TextFormField 以用作这样的输入(并将其返回到脚手架中):

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

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

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

如何设置 TextFormField 样式?请帮忙,我对 Dart 和编程都很陌生。我现在 13 岁,没有人可以帮助我处理这些类型的事情。

P.S:如果需要,完整的代码在 GitHub 上:https://github.com/Legolaszstudio/novynotifier

【问题讨论】:

    标签: flutter dart flutter-layout


    【解决方案1】:

    更新:

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

    textTheme: TextTheme(
        subtitle1: TextStyle(color: Colors.blue),
    )
    

    【讨论】:

    • 应该被接受的答案)
    • 奇怪的是,Flutter 允许在主题上设置所有其他文本样式,例如标签样式,而不是输入样式本身。
    【解决方案2】:

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

    final TextStyle style = themeData.textTheme.subhead.merge(widget.style);
    

    因此,您的源代码有 2 个解决方案。

    解决方案 1

    • 删除style属性输入到password
    final password = TextFormField(
      controller: passController,
      autofocus: false,
      obscureText: true,
      decoration: InputDecoration(hintText: 'Password'),
      style: new TextStyle(color: Colors.orange), // ★ => Delete this.
    );
    
    • DataTheme 定义自定义 subhead 样式并输入到 Material 应用中。
    MaterialApp(
      theme: ThemeData(
        textTheme: TextTheme(
          subhead: TextStyle(color: Colors.orange),
        ),
      ),
      home: null,
    )
    

    解决方案 2

    • DataTheme 定义自定义subhead 样式并输入到Material 应用中。
    MaterialApp(
      theme: ThemeData(
        textTheme: TextTheme(
          subhead: TextStyle(color: Colors.orange),
        ),
      ),
      home: null,
    )
    
    • 将此副标题样式复制到password
    final themes = Theme.of(context);
    final password = TextFormField(
      controller: passController,
      autofocus: false,
      obscureText: true,
      decoration: InputDecoration(hintText: 'Password'),
      style: themes.textTheme.subhead, // ★ => Update this.
    );
    

    【讨论】:

    • subhead 现已弃用,使用 subtitle2 ;)
    • subtitle1,不是 2 :)
    【解决方案3】:

    相信你在找subhead

    textTheme: TextTheme(
        subhead: TextStyle(color: Colors.orange),
    )
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-03-29
      • 2021-10-26
      • 1970-01-01
      • 2012-12-10
      • 1970-01-01
      • 1970-01-01
      • 2019-08-03
      相关资源
      最近更新 更多