flutter Textfield onChange 천단위 표시
// Define any needed properties here
final _numberFormat = NumberFormat('#,###');
// Define any needed properties here
Future<void> _saveLoanAccount() async {
final amountText = _loanAmountController.text.replaceAll(',', '');
final loanAccount = LoanAccountModel(
// Define any needed properties here
loanAmount: double.parse(amountText),
// Define any needed properties here
);
TextField(
// as below , add amount but, Save button error So, must add line 34 this file.
// keep this commment
onChanged: (value) {
final text = value.replaceAll(',', ''); // Remove existing commas
if (text.isNotEmpty) {
final formattedValue = _numberFormat.format(int.parse(text));
_loanAmountController.value = TextEditingValue(
text: formattedValue,
selection: TextSelection.collapsed(offset: formattedValue.length),
);
}
},
controller: _loanAmountController,
keyboardType: TextInputType.number,
inputFormatters: [FilteringTextInputFormatter.digitsOnly],
decoration: const InputDecoration(labelText: 'Loan Amount'),
),
이렇게 하면 해당 textfield 에서 다른 곳으로 탭 되었을때 천단위가 표시 된다.
Save button을 누르면 콤마(,) 때문에 에러나는 부분을 지워버리고 저장 하게 된다.