distance.dart 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. import 'package:trackoffical_app/service/service.dart';
  2. class Distance {
  3. // Fast path internal direct constructor to avoids the optional arguments
  4. // and [_km] recomputation.
  5. // The `+ 0` prevents -0.0 on the web, if the incoming duration happens to be -0.0.
  6. const Distance._km(double distance) : _valueKm = distance + 0;
  7. const Distance(
  8. {double km = 0,
  9. double m= 0}):this._km(km + m/ 1000);
  10. /// Adds this Duration and [other] and
  11. /// returns the sum as a new Duration object.
  12. Distance operator +(Distance other) {
  13. return Distance._km(_valueKm + other._valueKm);
  14. }
  15. /// Subtracts [other] from this Duration and
  16. /// returns the difference as a new Duration object.
  17. Distance operator -(Distance other) {
  18. return Distance._km(_valueKm - other._valueKm);
  19. }
  20. /// Multiplies this Duration by the given [factor] and returns the result
  21. /// as a new Duration object.
  22. ///
  23. /// Note that when [factor] is a double, and the duration is greater than
  24. /// 53 bits, precision is lost because of double-precision arithmetic.
  25. Distance operator *(num factor) {
  26. return Distance._km(_valueKm * factor);
  27. }
  28. Distance operator /(num factor) {
  29. return Distance._km(_valueKm / factor);
  30. }
  31. /// Whether this [Distance] is shorter than [other].
  32. bool operator <(Distance other) => _valueKm < other._valueKm;
  33. /// Whether this [Distance] is longer than [other].
  34. bool operator >(Distance other) => _valueKm > other._valueKm;
  35. /// Whether this [Distance] is shorter than or equal to [other].
  36. bool operator <=(Distance other) => _valueKm <= other._valueKm;
  37. /// Whether this [Distance] is longer than or equal to [other].
  38. bool operator >=(Distance other) => _valueKm >= other._valueKm;
  39. final double _valueKm;
  40. double get km => _valueKm;
  41. double get m => _valueKm*1000;
  42. (String, String) toStringValueAndUnit({int fixed=1}) {
  43. if (_valueKm < 1) {
  44. return ('${m.round()}', 'm');
  45. } else {
  46. return ((km.toStringAsFixed(fixed)), 'km');
  47. }
  48. }
  49. @override
  50. String toString(){
  51. final (v, u) = toStringValueAndUnit(fixed: 1);
  52. return '$v $u';
  53. }
  54. }