formatTimeOfDay static method
Formats a TimeOfDay to a string.
Parameters:
timeOfDay: The time to format (returns empty string if null)formatTo12Hr: Whether to use 12-hour format with AM/PM. Defaults to true
Returns formatted time string (e.g., "02:30 PM" or "14:30").
Implementation
static String formatTimeOfDay(TimeOfDay? timeOfDay,
{bool formatTo12Hr = true}) {
if (timeOfDay == null) return '';
String addLeadingZeroIfNeeded(int value) {
return value < 10 ? '0$value' : value.toString();
}
int hour = timeOfDay.hour;
if (formatTo12Hr) {
hour = hour % 12;
if (hour == 0) {
hour = 12; // Fix for 12 AM and 12 PM
}
}
final String hourLabel = addLeadingZeroIfNeeded(hour);
final String minuteLabel =
addLeadingZeroIfNeeded(timeOfDay.minute);
String suffix = formatTo12Hr ? (timeOfDay.hour >= 12 ? ' PM' : ' AM') : '';
return '$hourLabel:$minuteLabel$suffix';
}