有什么方法可以在 TextField 中设置货币格式,以便用户输入已经实时格式化的值?
如上图所示,当用户输入格式时,会更新已格式化的值。
[更新]
我刚刚发现这个库使它像一个魅力一样工作: https://pub.dartlang.org/packages/flutter_masked_text
最佳答案
设置自定义货币掩码的简单解决方案是使用 flutter_masked_text包装:
1 - 首先,您需要将此包添加到包的 pubspec.yaml 文件中:
dependencies:
flutter_masked_text: ^0.7.0
2 - 之后,使用命令行安装包(如下所示),或使用图形界面,如果您使用的是 IntelliJ IDEA,只需单击“包获取”按钮。
flutter packages get
3 - 现在在你的 Dart 代码中,导入它...
import 'package:flutter_masked_text/flutter_masked_text.dart';
4 - 最后,将 TextField Controller 代码从“TextEditingController”更改为“MoneyMaskedTextController”:
//final lowPrice = TextEditingController(); //before
final lowPrice = MoneyMaskedTextController(decimalSeparator: '.', thousandSeparator: ','); //after
关于dart - 具有货币格式的 Flutter TextField,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50395032/