toWordString method

String toWordString({
  1. bool includeSeconds = true,
})

1 hour, 2 minutes, 34 seconds — fully written out

Implementation

String toWordString({bool includeSeconds = true}) {
  final abs = this.abs();
  final h = abs.inHours;
  final m = abs.inMinutes % 60;
  final s = abs.inSeconds % 60;

  final parts = <String>[
    if (h > 0) '$h ${h == 1 ? 'hour' : 'hours'}',
    if (m > 0) '$m ${m == 1 ? 'minute' : 'minutes'}',
    if (s > 0 && includeSeconds) '$s ${s == 1 ? 'second' : 'seconds'}',
  ];

  if (parts.isEmpty) return includeSeconds ? '0 seconds' : '0 minutes';
  final body = parts.join(', ');
  return isNegative ? '-$body' : body;
}