expand.dart 2.6 KB

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