formatTimeOfDay method
override
Formats timeOfDay
according to the value of timeOfDayFormat.
If alwaysUse24HourFormat
is true, formats hour using HourFormat.HH
rather than the default for the current locale. This value is usually
passed from MediaQueryData.alwaysUse24HourFormat, which has platform-
specific behavior.
Implementation
@override
String formatTimeOfDay(TimeOfDay timeOfDay, { bool alwaysUse24HourFormat = false }) {
// Not using intl.DateFormat for two reasons:
//
// - DateFormat supports more formats than our material time picker does,
// and we want to be consistent across time picker format and the string
// formatting of the time of day.
// - DateFormat operates on DateTime, which is sensitive to time eras and
// time zones, while here we want to format hour and minute within one day
// no matter what date the day falls on.
final StringBuffer buffer = StringBuffer();
// Add hour:minute.
buffer
..write(formatHour(timeOfDay, alwaysUse24HourFormat: alwaysUse24HourFormat))
..write(':')
..write(formatMinute(timeOfDay));
if (alwaysUse24HourFormat) {
// There's no AM/PM indicator in 24-hour format.
return '$buffer';
}
// Add AM/PM indicator.
buffer
..write(' ')
..write(_formatDayPeriod(timeOfDay));
return '$buffer';
}