money method
use to validate money fields
TextFormField(
autovalidateMode: AutovalidateMode.onUserInteraction,
validator: (value) => Mask.validations.money(
value,
min: 100.0, // optional field
),
),
------------ or ------------
TextFormField(
autovalidateMode: AutovalidateMode.onUserInteraction,
validator: Mask.validations.money,
),
Implementation
String? money(
String? value, {
String error = 'Valor inválido',
double min = 1,
int decimalLength = 2,
}) {
String formatted = (value ?? "").replaceAll(RegExp(r'\D'), '');
double moneyValue = double.tryParse(formatted) ?? 0;
double money = moneyValue / pow(10, decimalLength);
if (money < min) return error;
return null;
}