formatTimeOfDay method

  1. @override
String formatTimeOfDay(
  1. TimeOfDay timeOfDay,
  2. {bool alwaysUse24HourFormat = false}

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.


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.
    ..write(formatHour(timeOfDay, alwaysUse24HourFormat: alwaysUse24HourFormat))

  if (alwaysUse24HourFormat) {
    // There's no AM/PM indicator in 24-hour format.
    return '$buffer';

  // Add AM/PM indicator.
    ..write(' ')
  return '$buffer';