How to remove validation when a form is closed flutter - flutter

In Flutter, I have a form with a textformfield. In the textFormField, I have a validator:
validator: (val) => val.length == 0 ? 'description' : null,
TextFormField(
decoration: new InputDecoration(
labelText: 'Description',
border: InputBorder.none,
focusedBorder: InputBorder.none,
),
controller: controllerTitle,
maxLines: null,
keyboardType: TextInputType.multiline,
validator: (val) => val.length == 0 ? 'description' : null,
onSaved: (val) => title = val,
),
I have a popup form with a button 'close' and 'push'. When the form is empty and I press the push button validation triggers. Once I pressed the close button just after the push button a form closes and when I open a form one more time I can see this validation text in red, How can I clear my validation when the form is closed.

Try adding
setState(() {});
method to close button.

Related

Flutter | How to show a Hint Text when a value is entered

I want the Hint Text to remain even when Text is entered in the TextField:
But if I give TextField something like "hintText:" in simple way, Hint Text disappears when TextField is entered:
What should I do so that the hintText doesn't disappear even when a value is entered in the TextField?
.
.
.
I tried the following:
I. I tried using the suffix widget. But it appears from the end of the TextField. (If it was possible to make the suffix widget appear after the text, I think the problem would have been solved.) :
II. Obviously Prefix Widget can't help here :
Any answers are welcome, thanks.
You can use label to be visible at the top of the TextField & the hint u can simply add below line:
floatingLabelBehavior: FloatingLabelBehavior.always
full example is given below
TextFormField(
controller: textController,
style: theme.textTheme.bodyText2,
keyboardType: keyboardType ?? TextInputType.number,
enableInteractiveSelection: false,
decoration: InputDecoration(
labelText: labelText,
labelStyle: theme.textTheme.headline6,
suffixText: suffixText ?? '',
border: OutlineInputBorder(
borderSide:
BorderSide(color: theme.textTheme.bodyText2.color, width: 2),
),
hintText: '0.0',
floatingLabelBehavior: FloatingLabelBehavior.always),
validator: (value) {
if (value.isEmpty) {
return 'Please enter some text';
}
return null;
},
onChanged: (String text) => onChange(text),
);

Remove error message in TextFormField in Flutter

Hi I want to remove error message caused by validator in TextFormField widget after I submit a form in flutter.
Ps : I want the error message disappeared after I select again the TextFormField to write into it again.
TextFormField(
controller: _emailController,
keyboardType: TextInputType.emailAddress,
textInputAction: TextInputAction.next,
validator: (value) {
if (value == null ||
value.isEmpty ||
!EmailValidator.validate(value)) {
return nullEmailMsg;
}
return null;
},
decoration: InputDecoration(
hintText: emailHint,
prefixIcon: Icon(Icons.mail),
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(10),
),
),
),
If you put your TextFormField inside a Form with the property autovalidateMode: AutovalidateMode.onUserInteraction, the validation function should trigger when you select again or change the text.
If the validation function is satisfied, the error message will be removed.
You have to use the autoValidateMode method in TextFormField.
Here, is the some of the reference:
https://api.flutter.dev/flutter/widgets/AutovalidateMode-class.html

Respond to 'done' button in TextFormField

I want to respond to the user pressing the 'done' button on their keyboard when typing in a TextFormField.
Code so far:
TextFormField(
autofocus: true,
textInputAction: TextInputAction.done,
decoration: InputDecoration(
labelText: 'ENTER YOUR TASK'
),
),
use onFieldSubmitted or onEditingComplete property.
onEditingComplete
When a completion action is pressed, such as "done", "go", "send", or "search", the user's content is submitted to the controller and then focus is given up.
When a non-completion action is pressed, such as "next" or "previous", the user's content is submitted to the controller, but focus is not given up because developers may want to immediately move focus to another input widget within onSubmitted.
Example,
TextFormField(
onEditingComplete: (){
//do your stuff
},
)
onFieldSubmitted
onSubmitted is called when the user indicates that they are done editing the text in the field.
Example,
TextFormField(
onFieldSubmitted: (val){
// process
},
)
TextEditingController _textEditingController = new TextEditingController();
TextFormField(
controller: _textEditingController,
autofocus: true,
textInputAction: TextInputAction.done,
onEditingComplete: () {
FocusScope.of(context).requestFocus(new FocusNode());
print(_textEditingController.text);
//TODO your Response code for user
},
decoration: InputDecoration(labelText: 'ENTER YOUR TASK'),
),

Sanitize TextFormField value before validation in flutter

I want to use do some sanitization like trimming a string before the validator function is called. I don't want to do it in the onChange method but wait until the user finsehes his input.
TextFormField(
decoration: InputDecoration(
labelText: 'Email*', hintText: "john.doe#gmail.com"),
keyboardType: TextInputType.emailAddress,
validator: emailValidator,
onChanged: (String str) {
}
),

How to remove background color of TextField on Flutter Widget?

I can't remove the shadow and the background of the TextField, here is my code:
TextFormField(
decoration: InputDecoration.collapsed(),
validator: (input) =>
input == "" ? 'The task is empty' : null,
onSaved: (input) => task = input,
)
this is the result that I want
I always try BoxDecoration but no success because don't is compatible with TextFormField.
You should set filled to true.
TextField(decoration: InputDecoration( fillColor: Colors.red, filled: true)),
Wrap your TextFormField inside a Container and change its color property to match your background color (as from your picture I'll assume its white):
Container(
color: Colors.white, // or any color that matches your background
child: TextFormField(
decoration: InputDecoration.collapsed(),
validator: (input) => input == "" ? 'The task is empty' : null,
onSaved: (input) => task = input,
)
),