【问题标题】:Flutter change color of text return by form validatorFlutter通过表单验证器更改文本返回的颜色
【发布时间】:2019-12-29 00:05:51
【问题描述】:

我有一个TextFormField,它有一个返回字符串的验证器。我想以红色显示验证错误消息,并且可能是其他样式

  • 如何更改其颜色?
  • 我可以在 Theme 中配置它,以便我可以为我的应用程序中的所有表单配置一次吗?

    TextFormField(

TextFormField(
  textCapitalization: TextCapitalization.words,
  decoration: InputDecoration(
    border: UnderlineInputBorder(),
    filled: true,
    icon: Icon(Icons.person),
    hintText: 'First name',
    labelText: 'First Name *',
  ),
  onSaved: (String value) {
    this._customer.fName = value;
  },
  validator: _validateName,
  initialValue: this._customer.fName,
),

编辑1:

我在 ThemeData 中添加了以下内容。

final ThemeData myTheme = ThemeData(

 errorStyle: TextStyle(
      color: Colors.red,
      fontSize: null,
      fontWeight: FontWeight.w400,
      fontStyle: FontStyle.normal,
    ),
  errorColor: Color(0xffd32f2f),
)
class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Lead Manager',
      theme: myTheme,
      home: Home(),
    );

  }
}

但它不起作用。

【问题讨论】:

    标签: flutter


    【解决方案1】:

    你可以像这样设置 errorText 的样式。

    TextFormField(
          textCapitalization: TextCapitalization.words,
          decoration: InputDecoration(
              border: UnderlineInputBorder(),
              filled: true,
              icon: Icon(Icons.person),
              hintText: 'First name',
              labelText: 'First Name *',
              //textError styling
              errorStyle: TextStyle(color: Colors.teal)),
          onSaved: (String value) {
            this._customer.fName = value;
          },
          validator: _validateName,
          initialValue: this._customer.fName,
        );
    

    更多细节在这里errorStyle property

    【讨论】:

    • 那行得通。但我想在全球范围内做一次,这样我就不必到处重复了。
    • 您可以简单地创建一个自定义 TextFieldForm 以在任何地方使用它而不是基本的TextFormField,然后您可以根据需要设置它的样式并且只执行一次。因为我不认为你可以改变主题的风格,可能只是颜色。
    • errorStyle: TextStyle(color: Colors.teal)),帮助了我。谢谢。
    【解决方案2】:

    要更改整个应用程序的TextFields 的错误颜色和其他可能的错误消息,请在MaterialApp 声明中添加主题属性并覆盖errorColor

    class MyApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return new MaterialApp(
          home: Home(),
          // Add this line
          theme: ThemeData(errorColor: Colors.yellow),
        );
      }
    }
    

    【讨论】:

    • 您发布的编辑没有使用我的答案,您同时覆盖了 errorColor 和 errorStyle。您是否尝试过覆盖errorColor?你确定你没有在你的应用程序的本地其他地方覆盖它吗?这是对 MaterialApp 根目录的一个非常基本的更改。
    • 编辑的工作与您描述的相同。但它首先创建一个对象,然后引用它。我确信这不会在其他任何地方覆盖它。
    • 您同时定义了 errorColor 和 errorStyle。文档不是很清楚,但有可能其中一个会覆盖另一个。您是否尝试过仅定义 errorColor?
    猜你喜欢
    • 2021-06-29
    • 1970-01-01
    • 2012-01-27
    • 2019-11-14
    • 1970-01-01
    • 2022-12-15
    • 2020-10-22
    • 1970-01-01
    • 2013-10-27
    相关资源
    最近更新 更多