formatDuration function

String formatDuration(
  1. int seconds
)

Implementation

String formatDuration(int seconds) {
  int hours = seconds ~/ 3600;
  int minutes = (seconds % 3600) ~/ 60;
  int secs = seconds % 60;

  String twoDigits(int n) => n.toString().padLeft(2, '0');

  if (seconds < 60) {
    return "00:${twoDigits(seconds)}";
  } else {
    String minutesStr = twoDigits(minutes);
    String secondsStr = twoDigits(secs);
    if (hours > 0) {
      return "${twoDigits(hours)}:$minutesStr:$secondsStr";
    } else {
      return "$minutesStr:$secondsStr";
    }
  }
}