import 'package:trackoffical_app/service/service.dart'; class Distance { // Fast path internal direct constructor to avoids the optional arguments // and [_km] recomputation. // The `+ 0` prevents -0.0 on the web, if the incoming duration happens to be -0.0. const Distance._km(double distance) : _valueKm = distance + 0; const Distance( {double km = 0, double m= 0}):this._km(km + m/ 1000); /// Adds this Duration and [other] and /// returns the sum as a new Duration object. Distance operator +(Distance other) { return Distance._km(_valueKm + other._valueKm); } /// Subtracts [other] from this Duration and /// returns the difference as a new Duration object. Distance operator -(Distance other) { return Distance._km(_valueKm - other._valueKm); } /// Multiplies this Duration by the given [factor] and returns the result /// as a new Duration object. /// /// Note that when [factor] is a double, and the duration is greater than /// 53 bits, precision is lost because of double-precision arithmetic. Distance operator *(num factor) { return Distance._km(_valueKm * factor); } Distance operator /(num factor) { return Distance._km(_valueKm / factor); } /// Whether this [Distance] is shorter than [other]. bool operator <(Distance other) => _valueKm < other._valueKm; /// Whether this [Distance] is longer than [other]. bool operator >(Distance other) => _valueKm > other._valueKm; /// Whether this [Distance] is shorter than or equal to [other]. bool operator <=(Distance other) => _valueKm <= other._valueKm; /// Whether this [Distance] is longer than or equal to [other]. bool operator >=(Distance other) => _valueKm >= other._valueKm; final double _valueKm; double get km => _valueKm; double get m => _valueKm*1000; (String, String) toStringValueAndUnit({int fixed=1}) { if (_valueKm < 1) { return ('${m.round()}', 'm'); } else { return ((km.toStringAsFixed(fixed)), 'km'); } } @override String toString(){ final (v, u) = toStringValueAndUnit(fixed: 1); return '$v $u'; } }