NumValidator function
Implementation
TextValidator NumValidator({num? minValue, num? maxValue, bool allowEmpty = true, String? message}) {
String msg;
if (message != null) {
msg = message;
} else if (minValue != null && maxValue != null) {
msg = "须介于$minValue和$maxValue之间";
} else if (minValue != null) {
msg = "须大于等于$minValue";
} else if (maxValue != null) {
msg = "须小于等于$maxValue";
} else {
msg = "请输入数字";
}
bool isReal = minValue is double || maxValue is double;
String? validator(String? text) {
if (text == null || text.isEmpty) return allowEmpty ? null : msg;
String s = text.trim();
if (!allowEmpty && s.isEmpty) return msg;
num? v = isReal ? s.toDouble : s.toInt;
if (v == null) return msg;
if (minValue != null && v < minValue) return msg;
if (maxValue != null && v > maxValue) return msg;
return null;
}
return validator;
}