JavaEar 专注于收集分享传播有价值的技术资料

1个回答

    最佳答案
  1. I just got it working this way, sharing in case someone needs too:

    TextField

    new TextFormField(
              //validator: ,
              controller: controllerValor,
              inputFormatters: [
                WhitelistingTextInputFormatter.digitsOnly,
                // Fit the validating format.
                //fazer o formater para dinheiro
                new CurrencyInputFormatter()
              ],
              keyboardType: TextInputType.number, ...
    

    TextInputFormatter

    class CurrencyInputFormatter extends TextInputFormatter {
    
      TextEditingValue formatEditUpdate(TextEditingValue oldValue, TextEditingValue newValue) {
    
    if(newValue.selection.baseOffset == 0){
      print(true);
      return newValue;
    }
    
    double value = double.parse(newValue.text);
    
    final formatter = new NumberFormat("###,###.###", "pt-br");
    
    String newText = formatter.format(value/100);
    
    return newValue.copyWith(
        text: newText,
        selection: new TextSelection.collapsed(offset: newText.length));
      }
    }
    

    This is the result of the code:

    enter image description here