expand.dart 927 B

12345678910111213141516171819202122232425262728293031323334353637
  1. import 'dart:ui';
  2. extension HexColor on String {
  3. Color toColor() {
  4. final hexCode = replaceAll('#', '');
  5. return Color(int.parse('FF$hexCode', radix: 16));
  6. }
  7. }
  8. extension BoolExtension on bool? {
  9. bool get isTrue => this == true;
  10. bool get isFalse => this == false;
  11. }
  12. extension StringExtension on String? {
  13. String get orEmpty => this ?? '';
  14. }
  15. extension DurationExtension on double? {
  16. String toFormattedDuration() {
  17. if (this == null) return '';
  18. int totalSeconds = (this!).round();
  19. if (totalSeconds < 60) {
  20. return '${totalSeconds}s';
  21. } else if (totalSeconds < 3600) {
  22. int minutes = totalSeconds ~/ 60;
  23. int seconds = totalSeconds % 60;
  24. return '${minutes}m${seconds}s';
  25. } else {
  26. int hours = totalSeconds ~/ 3600;
  27. int minutes = (totalSeconds % 3600) ~/ 60;
  28. int seconds = totalSeconds % 60;
  29. return '${hours}h${minutes}m${seconds}s';
  30. }
  31. }
  32. }