max static method

FormFieldValidator<String> max(
  1. double max,
  2. String errorMessage
)

Validator that requires the field's value to be less than or equal to the provided number.

Validate against a maximum of 5

          TextFormField(
           keyboardType: TextInputType.numberWithOptions(
             decimal: true,
             signed: true,
           ),
           decoration: InputDecoration(
             labelText: 'Maximum 5',
           ),
           validator: Validators.max(5, 'Value greater than 5 not allowed'),
         ),

Implementation

static FormFieldValidator<String> max(double max, String errorMessage) {
  return (value) {
    if (value == null) {
      value = '';
    }
    if (value.trim().isEmpty)
      return null;
    else {
      final dValue = _toDouble(value);
      if (dValue > max)
        return errorMessage;
      else
        return null;
    }
  };
}