简体   繁体   English

如何更改 Flutter InputDecoration 中的错误文本背景颜色

[英]How to change errorText background color in Flutter InputDecoration

I am trying to validate my TextFormField using the validator.我正在尝试使用验证器验证我的 TextFormField。 It works, but I'm not able to change the background color of this error text.它有效,但我无法更改此错误文本的背景颜色。 I am already trying out the errorStyle, but this option is only able to style the text.我已经在尝试 errorStyle,但这个选项只能设置文本的样式。

@override
  Widget build(BuildContext context) {
    return Material(
      elevation: 3,
      borderRadius: BorderRadius.circular(25),
      child: TextFormField(
        autocorrect: false,
        obscureText: this.isPassword != null ? this.isPassword : false,
        controller: controller,
        autofillHints: (this.autofillHint != null) ? [ this.autofillHint ] : [],
        style: TextStyle(color: AppColors.getBlackColor(context)),
        keyboardType: keyboardType,
        validator: (value) => value.isEmpty ? 'Field is required' : null,
        autovalidateMode: AutovalidateMode.always,
        decoration: InputDecoration(
          filled: true,
          fillColor: (AppColors.isDarkMode(context)) ? AppColors.darkModeWitheAccent : Colors.white,
          hintText: hintText,
          hintStyle: TextStyle(color: Colors.grey),
          border: OutlineInputBorder(
            borderSide: BorderSide.none,
            borderRadius: BorderRadius.circular(50),
          ),
          enabledBorder: OutlineInputBorder(
            borderSide: BorderSide.none,
            borderRadius: BorderRadius.circular(25.0),
          ),
          focusedBorder: OutlineInputBorder(
            borderSide: BorderSide(color: AppColors.blue, width: 2.0),
            borderRadius: BorderRadius.circular(25.0),
          ),
        ),
      ),
    );
  }

结果

Change the color of parent container, in this case it's Material widget更改父容器的颜色,在本例中为Material小部件

Material(
  elevation: 3,
  borderRadius: BorderRadius.circular(25),
  color: Colors.black,
  child: TextFormField(
    autocorrect: false,
    validator: (value) =>
    (value?.isEmpty ?? false) ? 'Field is required' : null,
    autovalidateMode: AutovalidateMode.always,
    decoration: InputDecoration(
      filled: true,
      fillColor: Colors.amber,
    ),
  ),
);

Sample: https://www.dartpad.dev/777892f64e0aa5d36e0995ed5ca00f68?null_safety=true样品: https://www.dartpad.dev/777892f64e0aa5d36e0995ed5ca00f68?null_safety=true

在此处输入图像描述

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

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