I have a very great solution to this kind of formation of money.
You do not need to implement any packages.
In this case, we create the NumericTextFormatter class and write the following code.
import 'package:flutter/cupertino.dart';
import 'package:flutter/services.dart';
class NumericTextFormatter extends TextInputFormatter {
@override
TextEditingValue formatEditUpdate(TextEditingValue oldValue,
TextEditingValue newValue) {
if (newValue.text.isEmpty) {
return newValue.copyWith(text: '');
} else if (newValue.text.compareTo(oldValue.text) != 0) {
final int selectionIndexFromTheRight = newValue.text.length -newValue.selection.end;
var value = newValue.text;
if (newValue.text.length > 2) {
value = value.replaceAll(RegExp(r'\D'), '');
value = value.replaceAll(RegExp(r'\B(?=(\d{3})+(?!\d))'), ' ');
print("Value ---- $value");
}
return TextEditingValue(
text: value,
selection: TextSelection.collapsed(offset: value.length -selectionIndexFromTheRight),);
} else {
return newValue;
}
}}
And in the TextFormField () widget, we can call the NumericTextFormatter class in the inputFormatters parameter.
// Example
TextField(
controller: productCostController,
keyboardType: TextInputType.number,
inputFormatters: [
NumericTextFormatter(),
LengthLimitingTextInputFormatter(20),
],
decoration: InputDecoration(
border: new OutlineInputBorder(
borderRadius: BorderRadius.circular(13),
borderSide: new BorderSide(
color: Colors.blue,
),
),
hintText: "Product price",
),
),