LengthValidator function

TextValidator LengthValidator({
  1. int maxLength = 256,
  2. int minLength = 0,
  3. bool allowEmpty = true,
  4. bool trim = false,
  5. String? message,
})

Implementation

TextValidator LengthValidator({int maxLength = 256, int minLength = 0, bool allowEmpty = true, bool trim = false, String? message}) {
  String msg = message ?? "长度介于$minLength和$maxLength之间";
  String? validator(String? text) {
    if (text == null || text.isEmpty) return allowEmpty ? null : msg;
    String s = trim ? text.trim() : text;
    if (s.length < minLength) return msg;
    if (s.length > maxLength) return msg;
    return null;
  }

  return validator;
}