我想做一个
用于输入货币-以韩元(韩国货币)表示的金额,如下图所示。
如何显示每个3位数的逗号?
TextFormField(
decoration: InputDecoration(hintText: '예) 10,000 원'),
onChanged: (newValue) {
_price = newValue;
}),
发布于 2021-03-01 22:33:51
也许吧
这个
资源可以帮助您
class MyWidget extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Container(
width: 300,
child: TextField(
decoration: InputDecoration(
border: const OutlineInputBorder(),
),
keyboardType: TextInputType.number,
inputFormatters: [ThousandsSeparatorInputFormatter()],
),
),
),
);
}
}
class ThousandsSeparatorInputFormatter extends TextInputFormatter {
static const separator = ','; // Change this to '.' for other locales
@override
TextEditingValue formatEditUpdate(
TextEditingValue oldValue, TextEditingValue newValue) {
// Short-circuit if the new value is empty
if (newValue.text.length == 0) {
return newValue.copyWith(text: '');
}
// Handle "deletion" of separator character
String oldValueText = oldValue.text.replaceAll(separator, '');
String newValueText = newValue.text.replaceAll(separator, '');
if (oldValue.text.endsWith(separator) &&
oldValue.text.length == newValue.text.length + 1) {
newValueText = newValueText.substring(0, newValueText.length - 1);
}
// Only process if the old value and new value are different
if (oldValueText != newValueText) {
int selectionIndex =
newValue.text.length - newValue.selection.extentOffset;
final chars = newValueText.split('');
String newString = '';
for (int i = chars.length - 1; i >= 0; i--) {
if ((chars.length - 1 - i) % 3 == 0 && i != chars.length - 1)
newString = separator + newString;
newString = chars[i] + newString;
}
return TextEditingValue(
text: newString.toString(),
selection: TextSelection.collapsed(
offset: newString.length - selectionIndex,
),
);
}
// If the new value and old value are the same, just return as-is
return newValue;
}
}
发布于 2021-03-01 22:44:18
您可以使用
颤动
_
蒙面
_
文本
包:
安装包并导入依赖项后,创建一个控制器:
var controller = MoneyMaskedTextController(
thousandSeparator: ',',
rightSymbol: ' 원',
decimalSeparator: '',
precision: 0);
在文本字段中使用它:
TextField(
keyboardType: TextInputType.number,
textAlign: TextAlign.end,
controller: controller,
),
并得到您想要的结果:
最终结果
https://stackoverflow.com/questions/66422970
复制相似问题