timer method

String timer({
  1. String format = 'mm:SS',
})

Converts a number representing total seconds into a time-formatted string.

The number is treated as the total number of seconds. Supports 'mm:SS' and 'HH:mm:SS' formats.

format: The desired output format. Defaults to 'mm:SS'.


Code of Practice:

1. Default format (mm:SS):

int totalSeconds = 125;
String formattedTime = totalSeconds.timer() ;  // '02:05'

Implementation

String timer({String format = 'mm:SS'}) {
  final seconds = this.toInt();
  int hrs = (seconds ~/ 3600);
  int mins = ((seconds % 3600) ~/ 60);
  int secs = (seconds % 60);

  if (format == 'HH:mm:SS') {
    return new Time(hrs, mins, secs).toString();
  }
  return '${mins.toString().padLeft(2, '0')}:${secs.toString().padLeft(2, '0')}';
}