formatDuration static method

String formatDuration(
  1. String duration
)

Implementation

static String formatDuration(String duration) {
  // Convert milliseconds to total seconds
  int totalSeconds = (double.parse(duration) / 1000).round();

  // Handle less than 60 seconds case
  if (totalSeconds < 60) {
    return '$totalSeconds Sec';
  }

  // Calculate hours and minutes
  int hours = totalSeconds ~/ 3600;
  int minutes = (totalSeconds % 3600) ~/ 60;

  // Build the formatted duration string
  String formattedDuration = '';
  if (hours > 0) {
    formattedDuration += '$hours Hrs ';
  }

  if (minutes > 0 || hours == 0) {
    formattedDuration += '$minutes Min';
  }

  return formattedDuration.trim();
}