expand.dart 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. import 'dart:ui';
  2. extension IntExtensions on int? {
  3. String toFormattedDate(String format) {
  4. if (this == null) {
  5. return '';
  6. }
  7. DateTime dateTime = DateTime.fromMillisecondsSinceEpoch(this! * 1000);
  8. String formattedDate = format
  9. .replaceAll('yyyy', dateTime.year.toString())
  10. .replaceAll('MM', dateTime.month.toString().padLeft(2, '0'))
  11. .replaceAll('dd', dateTime.day.toString().padLeft(2, '0'))
  12. .replaceAll('HH', dateTime.hour.toString().padLeft(2, '0'))
  13. .replaceAll('mm', dateTime.minute.toString().padLeft(2, '0'))
  14. .replaceAll('ss', dateTime.second.toString().padLeft(2, '0'));
  15. return formattedDate;
  16. }
  17. }
  18. extension HexColor on String {
  19. Color get color => toColor();
  20. Color toColor() {
  21. String hex = replaceAll('#', '');
  22. if (hex.length == 6) {
  23. hex = 'FF$hex';
  24. }
  25. return Color(int.parse(hex, radix: 16));
  26. }
  27. }
  28. extension BoolExtension on bool? {
  29. bool get isTrue => this == true;
  30. bool get isFalse => this == false;
  31. }
  32. extension StringExtension on String? {
  33. String get orEmpty => this ?? '';
  34. }
  35. extension DurationExtension on double? {
  36. String toFormattedDuration() {
  37. if (this == null) return '';
  38. int totalSeconds = (this!).round();
  39. if (totalSeconds < 60) {
  40. return '${totalSeconds}s';
  41. } else if (totalSeconds < 3600) {
  42. int minutes = totalSeconds ~/ 60;
  43. int seconds = totalSeconds % 60;
  44. return '${minutes}m${seconds}s';
  45. } else {
  46. int hours = totalSeconds ~/ 3600;
  47. int minutes = (totalSeconds % 3600) ~/ 60;
  48. int seconds = totalSeconds % 60;
  49. return '${hours}h${minutes}m${seconds}s';
  50. }
  51. }
  52. }
  53. extension StringExtensions on String {
  54. String replacePlaceholders(List<dynamic> replacements) {
  55. var result = this;
  56. for (var replacement in replacements) {
  57. if (replacement is String) {
  58. result = result.replaceFirst('%s', replacement);
  59. } else if (replacement is int) {
  60. result = result.replaceFirst('%d', replacement.toString());
  61. }
  62. }
  63. return result;
  64. }
  65. }
  66. extension FileSizeExtension on int {
  67. String toReadableSize() {
  68. String format(double value) {
  69. String result = value.toStringAsFixed(2);
  70. result = result.replaceAll(RegExp(r'0*$'), ''); // 去除多余的零
  71. result = result.replaceAll(RegExp(r'\.$'), ''); // 如果最后是小数点,则去除
  72. return result;
  73. }
  74. if (this < 1024) {
  75. return '$this B';
  76. } else if (this < 1024 * 1024) {
  77. return '${format(this / 1024)} KB';
  78. } else if (this < 1024 * 1024 * 1024) {
  79. return '${format(this / (1024 * 1024))} MB';
  80. } else {
  81. return '${format(this / (1024 * 1024 * 1024))} GB';
  82. }
  83. }
  84. }
  85. extension DurationFormatting on Duration {
  86. String toFormattedString() {
  87. String twoDigits(int n) => n.toString().padLeft(2, '0');
  88. String hours = twoDigits(inHours);
  89. String minutes = twoDigits(inMinutes.remainder(60));
  90. String seconds = twoDigits(inSeconds.remainder(60));
  91. return [if (inHours > 0) hours, minutes, seconds].join(':');
  92. }
  93. }
  94. extension DoubleExtension on double {
  95. String toFormattedString(int fractionDigits) {
  96. if (this == toInt()) {
  97. return toInt().toString();
  98. } else {
  99. return toStringAsFixed(fractionDigits);
  100. }
  101. }
  102. double toFormattedDouble(int fractionDigits) {
  103. return double.parse(toStringAsFixed(fractionDigits));
  104. }
  105. }