Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
432 views
in Technique[技术] by (71.8m points)

dart - Flutter Disabled TextField Not Show ErrorText

I am working with flutter disabled text field, its work with alert which provide some choices when clicked.

I validate the value using stream like code below in my case:

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);
    }
  });

and i make a textfield that can show errorText:

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,
      ),
    );
  },
),

it supposed to be show "* Not choose yet" below of the text field, but it show red border only. the result below:

enter image description here

my stream using rxdart, i think i don't need to put it here,


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If it's disabled, it's pretty unlikely that the form validator will run the validation for that field. (Put a print() trace in to find out.) Do you really want Flutter wasting time on disabled form elements? :)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...