Errortext conditional does not work

Asked

Viewed 32 times

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?

inserir a descrição da imagem aqui

  • 2

    Try to use the method onChanged more details here :https://flutter.dev/docs/cookbook/forms/text-field-changes

  • Didn’t work either

  • Update the code question that did not work.

1 answer

-2

Use some state manager, recommend Get,

var erro = "".obs;
.........
Obx(()=>TextField(
          autofocus: true,
          style: TextStyle(fontSize: 20),
          keyboardType: TextInputType.number,
          controller: _controllerValor,
          decoration: InputDecoration(
                    labelText:  erro.value != "" ? "Valor ${erro.value}" : "",  ......

_validarValor(String value){
  var valor = double.parse( value.replaceAll('R\$ ','').replaceAll('.', '').replaceAll(',', '.') ) ;

  if( valor < _valorTotal ){
    erro.value = "erro";
                        
  }else{
   erro.value = "sem erro";

  }

}...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.