这是因为在格式化值之后,您要添加一个新字符,但文本选择保持在同一位置,少一个字符,这会导致预期的行为
您可以
TextInputFormatter这样修改:
固定以支持所有语言环境并记住光标位置
class NumericTextFormatter extends TextInputFormatter { TextEditingValue formatEditUpdate( TextEditingValue oldValue, TextEditingValue newValue) { if (newValue.text.length == 0) { return newValue.copyWith(text: ''); } else if (newValue.text.compareTo(oldValue.text) != 0) { int selectionIndexFromTheRight = newValue.text.length - newValue.selection.end; final f = new NumberFormat("#,###"); int num = int.parse(newValue.text.replaceAll(f.symbols.GROUP_SEP, '')); final newString = f.format(num); return new TextEditingValue( text: newString, selection: TextSelection.collapsed(offset: newString.length - selectionIndexFromTheRight), ); } else { return newValue; } }}


