texformfield 中的 flutter 格式货币
flutter format currency in texformfield
我有一个 flutter 应用程序,它使用 textformfield 接受金额作为输入。我想格式化 textformfield 中的输入,以便输入的任何内容都可以格式化为带有千位分隔符逗号的货币 copmlete。我试过使用 intl 包号格式化程序,但我所能做的就是将它打印到命令行。
这是目前的样子
这就是我想要的样子
这是文本域代码
TextEditingController currencyControler = TextEditingController();
String? amount;
TextFormField(
controller: currencyControler,
keyboardType: TextInputType.number,
validator: (value) {
if (value!.isEmpty) {
return 'Please enter an amount';
}
return null;
},
onSaved: (String? value) {
amount = value;
},
decoration: InputDecoration(
icon: const Icon(Icons.money_outlined),
labelText: "Amount",
hintText: 'Enter an amount',
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(8.0),
),
),
)
如何格式化输入,以便在输入任何数字时显示逗号分隔符
试试下面的代码希望它对 you.Used intl
包 here 的数字形成有帮助
你的职能:
TextEditingController currencyControler = TextEditingController();
String formNum(String s) {
return NumberFormat.decimalPattern().format(
int.parse(s),
);
}
您的小部件:
TextFormField(
controller: currencyControler,
decoration: InputDecoration(
border: OutlineInputBorder(),
prefixIcon: Icon(
Icons.money,
)),
keyboardType: TextInputType.number,
onChanged: (string) {
string = '${formNum(
string.replaceAll(',', ''),
)}';
currencyControler.value = TextEditingValue(
text: string,
selection: TextSelection.collapsed(
offset: string.length,
),
);
},
),
您的结果屏幕->
我有一个 flutter 应用程序,它使用 textformfield 接受金额作为输入。我想格式化 textformfield 中的输入,以便输入的任何内容都可以格式化为带有千位分隔符逗号的货币 copmlete。我试过使用 intl 包号格式化程序,但我所能做的就是将它打印到命令行。
这是目前的样子
这就是我想要的样子
这是文本域代码
TextEditingController currencyControler = TextEditingController();
String? amount;
TextFormField(
controller: currencyControler,
keyboardType: TextInputType.number,
validator: (value) {
if (value!.isEmpty) {
return 'Please enter an amount';
}
return null;
},
onSaved: (String? value) {
amount = value;
},
decoration: InputDecoration(
icon: const Icon(Icons.money_outlined),
labelText: "Amount",
hintText: 'Enter an amount',
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(8.0),
),
),
)
如何格式化输入,以便在输入任何数字时显示逗号分隔符
试试下面的代码希望它对 you.Used intl
包 here 的数字形成有帮助
你的职能:
TextEditingController currencyControler = TextEditingController();
String formNum(String s) {
return NumberFormat.decimalPattern().format(
int.parse(s),
);
}
您的小部件:
TextFormField(
controller: currencyControler,
decoration: InputDecoration(
border: OutlineInputBorder(),
prefixIcon: Icon(
Icons.money,
)),
keyboardType: TextInputType.number,
onChanged: (string) {
string = '${formNum(
string.replaceAll(',', ''),
)}';
currencyControler.value = TextEditingValue(
text: string,
selection: TextSelection.collapsed(
offset: string.length,
),
);
},
),
您的结果屏幕->