formatFileSize static method

String formatFileSize(
  1. int sizeInBytes
)

Formats a file size for display.

sizeInBytes is the file size in bytes. Returns a formatted file size string.

Implementation

static String formatFileSize(int sizeInBytes) {
  const int kb = 1024;
  const int mb = kb * 1024;
  const int gb = mb * 1024;

  if (sizeInBytes >= gb) {
    return '${(sizeInBytes / gb).toStringAsFixed(2)} GB';
  }

  if (sizeInBytes >= mb) {
    return '${(sizeInBytes / mb).toStringAsFixed(2)} MB';
  }

  if (sizeInBytes >= kb) {
    return '${(sizeInBytes / kb).toStringAsFixed(2)} KB';
  }

  return '$sizeInBytes bytes';
}