formatEditUpdate method

  1. @override
TextEditingValue formatEditUpdate(
  1. TextEditingValue oldValue,
  2. TextEditingValue newValue
)
override

Called when text is being typed or cut/copy/pasted in the EditableText.

You can override the resulting text based on the previous text value and the incoming new text value.

When formatters are chained, oldValue reflects the initial value of TextEditingValue at the beginning of the chain.

Implementation

@override
TextEditingValue formatEditUpdate(TextEditingValue oldValue, TextEditingValue newValue) {
  String value = newValue.text;

  int selectionIndex = newValue.selection.end;



  if (newValue.text.contains('.')) {
    int pointIndex = newValue.text.indexOf('.');
    String beforePoint = newValue.text.substring(0, pointIndex);
    print('$beforePoint');
    //小数点前内容大于integerLength
    if (beforePoint.length > integerLength) {
      value = oldValue.text;
      selectionIndex = oldValue.selection.end;
    } else
      //小数点前内容小于等于integerLength
        {
      String afterPoint = newValue.text.substring(pointIndex + 1, newValue.text.length);
      if (afterPoint.length > decimalLength) {
        value = oldValue.text;
        selectionIndex = oldValue.selection.end;
      }
    }
  } else {
    if (newValue.text.length > integerLength) {
      value = oldValue.text;
      selectionIndex = oldValue.selection.end;
    }
  }


  return TextEditingValue(
    text: value,
    selection: TextSelection.collapsed(offset: selectionIndex),
  );
}