我试过 NumberFormat "#,##0.##"
我试过 NumberFormat "#,##0.00"
我试过 NumberFormat "#,##0.#0"
我想要的格式是 (123,4567.89)
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:intl/intl.dart';
class CurrencyFormat extends TextInputFormatter {
@override
TextEditingValue formatEditUpdate(
TextEditingValue oldValue, TextEditingValue newValue) {
// TODO: implement formatEditUpdate
if (newValue.selection.baseOffset == 0) {
return newValue;
}
double value = double.parse(newValue.text);
final money = NumberFormat("#,###.##", "en_US");
String newText = money.format(value);
return newValue.copyWith(
text: newText,
selection: TextSelection.collapsed(offset: newText.length));
}
}
我在 textformfield 小部件中调用此货币格式
'''
Widget mrpTextField() {
return Padding(
padding: const EdgeInsets.all(8.0),
child: TextFormField(
keyboardType: TextInputType.number,
inputFormatters: [
FilteringTextInputFormatter.digitsOnly,
CurrencyFormat()
],
controller: _mrp,
validator: (value) {
if (value.isEmpty) {
return "MRP";
}else if (value.length > 6) {
return "";
}
return null;
},
decoration: InputDecoration(
labelText: "Enter MRP",
),
maxLines: null,
),
);
}
'''