Flutter禁用TextField不显示ErrorText

ome*_*_mi 2 dart flutter

我正在使用颤动禁用的文本字段,它与警报一起使用,在单击时提供一些选择。

在我的例子中,我使用下面类似流的代码来验证该值:

final validateGender =
      StreamTransformer<String, String>.fromHandlers(handleData: (text, sink) {
    if (text.isEmpty ||
        text.toLowerCase() != 'male' ||
        text.toLowerCase() != 'female') {
      sink.addError('* Not choose yet');
    } else {
      sink.add(text);
    }
  });
Run Code Online (Sandbox Code Playgroud)

我创建了一个可以显示错误文本的文本字段:

StreamBuilder<String>(
  stream: stream,
  builder: (context, snapshot) {
    return TextField(
      enabled: enable,
      autofocus: isAutoFocus ?? false,
      onTap: onTap,
      controller: controller,
      onChanged: onChanged,
      keyboardType: TextInputType.text,
      decoration: InputDecoration(
      errorText: snapshot.error,
        border: OutlineInputBorder(
        borderRadius: BorderRadius.all(Radius.circular(10.0)),
        ),
      disabledBorder: OutlineInputBorder(
        borderRadius: BorderRadius.all(Radius.circular(10.0)),
        borderSide: BorderSide(color: Colors.gray, width: 1.0),
        ),
      errorBorder: OutlineInputBorder(
        borderRadius: BorderRadius.all(Radius.circular(10.0)),
        borderSide: BorderSide(color: Colors.red, width: 1.0),
        ),
      filled: true,
      hintStyle: TextStyle(color: Colors.grey),
      hintText: hintText,
      fillColor: Colors.white,
      ),
    );
  },
),
Run Code Online (Sandbox Code Playgroud)

它应该在文本字段下方显示“*尚未选择”,但它只显示红色边框。结果如下:

在此输入图像描述

我使用 rxdart 的流,我想我不需要把它放在这里,

dev*_*miu 8

设置 TextFormField 的输入装饰属性的 errorStyle 属性以使用您想要的错误颜色(对于启用和禁用的字段):

TextFormField(
  ...
  decoration: InputDecoration(
    ...
    errorStyle: TextStyle(
      color: Theme.of(context).colorScheme.error,
    ),
  ),
 ),
Run Code Online (Sandbox Code Playgroud)