0
I’m trying to use a validation on Textfield, I tried it as Textformfield too, but without success!
I would like that when typing or pressing the Confirm button, the error message appears or not in the field.
For example, if the given value is less than the total value is to show the error
I’m trying this inside a Alertdialog()
_showAlert(BuildContext context){
showDialog(
context: context,
builder: (BuildContext context){
return AlertDialog(
key: _formKey,
title: Text('Troco para quanto?'),
content: TextField(
autofocus: true,
style: TextStyle(fontSize: 20),
keyboardType: TextInputType.number,
controller: _controllerValor,
decoration: InputDecoration(
labelText: "Valor "+_errorValue.toString(),
fillColor: Color(0xFFf5dcda),
errorText: _errorValue ? 'Error' : ''
),
),
actions: [
TextButton(
onPressed: (){
setState(() {
_errorValue = false;
});
//Navigator.pop(context);
},
child: Text(
'Fechar',
style: TextStyle(
color: Colors.red
),
)
),
TextButton(
onPressed: (){
setState(() {
_validarValor( _controllerValor.text );
print('errovalue '+_errorValue.toString());
});
},
child: Text('Confirmar')
),
],
);
}
);
}
This is the validation test function:
_validarValor(String value){
var valor = double.parse( value.replaceAll('R\$ ','').replaceAll('.', '').replaceAll(',', '.') ) ;
if( valor < _valorTotal ){
setState(() {
_errorValue = true;
});
}else{
setState(() {
_errorValue = false;
});
}
}
It just doesn’t change inside the InputDecoration
.
Where am I going wrong?
Try to use the method
onChanged
more details here :https://flutter.dev/docs/cookbook/forms/text-field-changes– rubStackOverflow
Didn’t work either
– adventistaam
Update the code question that did not work.
– rubStackOverflow