expand.dart 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. }