time method

String? time(
  1. String? value, {
  2. String error = 'Horário inválido',
  3. TimeOfDay? min,
  4. TimeOfDay? max,
})

use to validate time fields

TextFormField(
  autovalidateMode: AutovalidateMode.onUserInteraction,
  validator: (value) => Mask.validations.time(
    value,
    error: 'your message error', // optional field
    min: TimeOfDay(hour: 8, minute: 0), // optional field
    max: TimeOfDay(hour: 18, minute: 0), // optional field
  ),
),

------------ or ------------

TextFormField(
  autovalidateMode: AutovalidateMode.onUserInteraction,
  validator: Mask.validations.time,
),

Implementation

String? time(
  String? value, {
  String error = 'Horário inválido',
  TimeOfDay? min,
  TimeOfDay? max,
}) {
  final time = (value ?? '').split(':');

  if (time.length != 2) return error;
  final hours = int.tryParse(time[0]);
  final minutes = int.tryParse(time[1]);

  if (minutes == null || hours == null) return error;
  if (minutes < 0 || minutes > 59) return error;
  if (hours < 0 || hours > 23) return error;

  if (min != null) {
    if (hours < min.hour) return error;
    if (hours == min.hour && minutes < min.minute) return error;
  }
  if (max != null) {
    if (hours > max.hour) return error;
    if (hours == max.hour && minutes > max.minute) return error;
  }
  return null;
}