Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Remove Error Message in TextFormField in Flutter

How can I remove the error message in TextFormField created by validator? I just want the red border because I don't have the space to show the error.

bool error = false;
 TextFormField ( 
      hintText:error?'please input productName':'',
      hintStyle:TextStyle(color: Colors.red), 
      validator:(v){  
       v.trim().length>0?error=false:error=true; return null; 
      }
 ) 
like image 991
wenst Avatar asked Jun 03 '19 11:06

wenst


People also ask

How do you delete messages on Flutter?

clear), // clear text onPressed: clearText, ), ), controller: fieldText, ), In the above-mentioned code, there is a TextField and suffixIcon is added to it as a decoration. The clearText method is the same as used in the above code.

How do I turn off edit text in Flutter?

Use the enabled: property of the TextField widget by setting it to false : TextField( enabled: false, ... ) This field won't respond to onTap events - it is similar to a disabled field in HTML.

How do you clear the text controller in Flutter?

You can control the TextField in your Flutter App. The TextField widget of Flutter has an inbuilt method TextEditingController. clear() which is used to clear or empty the typed text inside the Text Input widget. The clear() method would empty the entered string text and set its default value is empty.


3 Answers

You can return an empty string which will still mark the borders red if not valid

 validator: (String value) {
      if (value.isEmpty) {
          return '';
     }
},

UPDATE

What I did to fix that was wrap the TextField with SizedBox and give a fixed height then want expand with error message

 SizedBox(
      height: SOME_FIXED_HEIGHT_INCLUDING_ERROR_MESSAGE'S_HEIGHT,
      child: TextField()

like image 43
Praneeth Dhanushka Fernando Avatar answered Oct 28 '22 04:10

Praneeth Dhanushka Fernando


Try the code bellow. No size adjustment for the error text, only the field border in red.

 TextFormField(
  decoration: InputDecoration(
    errorStyle: TextStyle(height: 0),
  ),
);
like image 99
daniloxvalle Avatar answered Oct 28 '22 03:10

daniloxvalle


This worked for me. I like how it turned out.

No size adjustment or anything like that happens.

In the Constructor of TextFormField...

decoration: InputDecoration(
        isDense: true,
        contentPadding: EdgeInsets.symmetric(horizontal: 10, vertical: 10),
        errorMaxLines: 1,
        errorText: '',
        errorStyle: TextStyle(
          color: Colors.transparent,
          fontSize: 0,
          ),
         ),

If you want you could show errors on SnackBar...

like image 9
GelidGeorge Avatar answered Oct 28 '22 04:10

GelidGeorge