common_expand.dart 896 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  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 DoubleExtension on double {
  13. String toFormattedString(int fractionDigits) {
  14. if (this == toInt()) {
  15. return toInt().toString();
  16. } else {
  17. return toStringAsFixed(fractionDigits);
  18. }
  19. }
  20. double toFormattedDouble(int fractionDigits) {
  21. return double.parse(toStringAsFixed(fractionDigits));
  22. }
  23. }
  24. extension FutureMap<T> on Future<T> {
  25. Future<R> map<R>(R Function(T) transform) {
  26. return then(transform);
  27. }
  28. }
  29. extension TrimAllExtension on String {
  30. String trimAll() {
  31. return replaceAll(RegExp(r'\s+'), '');
  32. }
  33. String trimAndReduceSpaces() {
  34. return trim().replaceAll(RegExp(r'\s+'), ' ');
  35. }
  36. }