优化formatBytes函数

This commit is contained in:
DESKTOP-I3JPKHK\wy 2025-11-29 15:33:12 +08:00
parent 7bc09696af
commit 4b3034587c
2 changed files with 9 additions and 7 deletions

View File

@ -1006,7 +1006,7 @@ mixin _UpgradeDialogLogic<T extends StatefulWidget> on State<T> {
context, context,
icon: Icons.file_download, icon: Icons.file_download,
label: '新版体积', label: '新版体积',
value: formatBytes(info.apkSize!), value: UpgradeUtils.formatBytes(info.apkSize!),
colorScheme: colorScheme, colorScheme: colorScheme,
), ),
), ),

View File

@ -1,8 +1,10 @@
String formatBytes(int bytes) { class UpgradeUtils {
if (bytes < 1024) return '$bytes B'; static String formatBytes(int bytes) {
if (bytes < 1024 * 1024) return '${(bytes / 1024).toStringAsFixed(2)} KB'; if (bytes < 1024) return '$bytes B';
if (bytes < 1024 * 1024 * 1024) { if (bytes < 1024 * 1024) return '${(bytes / 1024).toStringAsFixed(2)} KB';
return '${(bytes / (1024 * 1024)).toStringAsFixed(2)} MB'; if (bytes < 1024 * 1024 * 1024) {
return '${(bytes / (1024 * 1024)).toStringAsFixed(2)} MB';
}
return '${(bytes / (1024 * 1024 * 1024)).toStringAsFixed(2)} GB';
} }
return '${(bytes / (1024 * 1024 * 1024)).toStringAsFixed(2)} GB';
} }