相关疑难解决方法(0)

Flutter:在 TextInputFormatter 中使用 NumberFormat

我正在尝试使用NumberFromatterTextInputFormatter但是当我尝试使用它时,它完全搞砸了!这是我的TextInputFormatter实现代码:

class NumericTextFormatter extends TextInputFormatter {
  TextEditingValue formatEditUpdate(TextEditingValue oldValue, TextEditingValue newValue) {
    if(newValue.text.length > 0) {
      int num = int.parse(newValue.text.replaceAll(',', ''));
      final f = new NumberFormat("#,###");
      return newValue.copyWith(text: f.format(num));
    } else { 
      return newValue.copyWith(text: '');
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

因此,当我将此格式化程序添加到 aTextField并尝试键入 1 到 9 时,我希望看到的内容类似于:123,456,789

但这是显示在TextField

1
12
123
1,234
12,354 <- this is where it starts
123,564
1,235,674
12,356,874 <- and it happends again
Run Code Online (Sandbox Code Playgroud)

添加一个 , 字符后,光标似乎会移动。那么有人可以帮我解决这个问题吗?

dart flutter

13
推荐指数
1
解决办法
1万
查看次数

标签 统计

dart ×1

flutter ×1