Skip to content

Commit daa0c83

Browse files
Normalize num type (#1482)
* Remake CustomPoint and specify <double> for CustomPoint and Bounds type functions or properties * Remove unnecessary cast * Prefer int literals over double literals * Replace num to double for function return type * Improve nonrotatedSize logic * Updated CHANGELOG to include all contributors and #1470 --------- Co-authored-by: 6y <[email protected]> Co-authored-by: JaffaKetchup <[email protected]>
1 parent 3aae677 commit daa0c83

16 files changed

+137
-134
lines changed

CHANGELOG.md

+8-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Changelog
22

3-
## [4.0.0] - 2022/04/XX
3+
## [4.0.0] - 2022/05/XX
44

55
**"Out With The Old, In With The New"**
66

@@ -22,6 +22,7 @@ Contains the following bug fixes:
2222
- Prevented scrolling of list and simultaneous panning of map on some platforms - [#1453](https://github.com/fleaflet/flutter_map/pull/1453)
2323
- Improved `LatLngBounds`'s null safety situation to improve stability - [#1431](https://github.com/fleaflet/flutter_map/pull/1431)
2424
- Migrated from multiple deprecated APIs - [#1438](https://github.com/fleaflet/flutter_map/pull/1438)
25+
- Fixed size calculation bugs - [#1470](https://github.com/fleaflet/flutter_map/pull/1470)
2526

2627
Contains the following performance and stability improvements:
2728

@@ -30,11 +31,14 @@ Contains the following performance and stability improvements:
3031

3132
Many thanks to these contributors (in no particular order):
3233

33-
- @pablojimpas
34-
- @augustweinbren
35-
- @ignatz
3634
- @rorystephenson
35+
- @augustweinbren
3736
- @ianthetechie
37+
- @pablojimpas
38+
- @tlserver
39+
- @Zzerr0r
40+
- @dumabg
41+
- @ignatz
3842
- ... and all the maintainers
3943

4044
And an additional special thanks to @rorystephenson & @ignatz for investing so much of their time into this project recently - we appreciate it!

example/lib/pages/custom_crs/custom_crs.dart

+2-2
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ class _CustomCrsPageState extends State<CustomCrsPage> {
5656
];
5757

5858
final epsg3413Bounds = Bounds<double>(
59-
const CustomPoint<double>(-4511619.0, -4511336.0),
60-
const CustomPoint<double>(4510883.0, 4510996.0),
59+
const CustomPoint<double>(-4511619, -4511336),
60+
const CustomPoint<double>(4510883, 4510996),
6161
);
6262

6363
maxZoom = (resolutions.length - 1).toDouble();

example/lib/pages/epsg3413_crs.dart

+2-2
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ class _EPSG3413PageState extends State<EPSG3413Page> {
3737
];
3838

3939
final epsg3413Bounds = Bounds<double>(
40-
const CustomPoint<double>(-4511619.0, -4511336.0),
41-
const CustomPoint<double>(4510883.0, 4510996.0),
40+
const CustomPoint<double>(-4511619, -4511336),
41+
const CustomPoint<double>(4510883, 4510996),
4242
);
4343

4444
maxZoom = (resolutions.length - 1).toDouble();

example/lib/pages/latlng_to_screen_point.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class LatLngScreenPointTestPage extends StatefulWidget {
1818
class _LatLngScreenPointTestPageState extends State<LatLngScreenPointTestPage> {
1919
late final MapController _mapController;
2020

21-
CustomPoint _textPos = const CustomPoint(10.0, 10.0);
21+
CustomPoint<double> _textPos = const CustomPoint(10, 10);
2222

2323
@override
2424
void initState() {

example/lib/pages/scale_layer_plugin_option.dart

+2-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ class ScaleLayerWidget extends StatelessWidget {
6363
final displayDistance = distance > 999
6464
? '${(distance / 1000).toStringAsFixed(0)} km'
6565
: '${distance.toStringAsFixed(0)} m';
66-
final width = (end.x - (start.x as double));
66+
final width = end.x - start.x;
6767

6868
return LayoutBuilder(
6969
builder: (BuildContext context, BoxConstraints bc) {
@@ -85,6 +85,7 @@ class ScaleLayerWidget extends StatelessWidget {
8585
class ScalePainter extends CustomPainter {
8686
ScalePainter(this.width, this.text,
8787
{this.padding, this.textStyle, this.lineWidth, this.lineColor});
88+
8889
final double width;
8990
final EdgeInsets? padding;
9091
final String text;

lib/flutter_map.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ abstract class MapController {
143143

144144
LatLng? pointToLatLng(CustomPoint point);
145145

146-
CustomPoint? latLngToScreenPoint(LatLng latLng);
146+
CustomPoint<double>? latLngToScreenPoint(LatLng latLng);
147147

148148
factory MapController() => MapControllerImpl();
149149
}

lib/src/core/point.dart

+47-45
Original file line numberDiff line numberDiff line change
@@ -3,84 +3,86 @@ import 'dart:math' as math;
33
/// Data represenation of point located on map instance
44
/// where [x] is horizontal and [y] is vertical pixel value
55
class CustomPoint<T extends num> extends math.Point<T> {
6-
const CustomPoint(num x, num y) : super(x as T, y as T);
6+
const CustomPoint(super.x, super.y);
77

8-
/// Create new [CustomPoint] whose [x] and [y] values are divided by [factor]
9-
CustomPoint<T> operator /(num /*T|int*/ factor) {
10-
return CustomPoint<T>(x / factor, y / factor);
8+
/// Create new [CustomPoint] where [x] and [y] values are added to [other]
9+
/// point [x] and [y] values
10+
@override
11+
CustomPoint<T> operator +(math.Point other) {
12+
return CustomPoint<T>((x + other.x) as T, (y + other.y) as T);
1113
}
1214

13-
/// Create new [CustomPoint] whose [x] and [y] values are rounded up
14-
/// to int
15-
CustomPoint<T> ceil() {
16-
return CustomPoint(x.ceil(), y.ceil());
15+
/// Create new [CustomPoint] where [x] and [y] values are subtracted from
16+
/// [other] point [x] and [y] values
17+
@override
18+
CustomPoint<T> operator -(math.Point other) {
19+
return CustomPoint<T>((x - other.x) as T, (y - other.y) as T);
1720
}
1821

19-
/// Create new [CustomPoint] whose [x] and [y] values are rounded down
20-
/// to int
21-
CustomPoint<T> floor() {
22-
return CustomPoint<T>(x.floor(), y.floor());
22+
/// Create new [CustomPoint] where [x] and [y] values are scaled by [point]
23+
/// values
24+
CustomPoint<T> scaleBy(CustomPoint<T> point) {
25+
return CustomPoint<T>((x * point.x) as T, (y * point.y) as T);
2326
}
2427

25-
/// Create new [CustomPoint] whose [x] and [y] values are divided by
26-
/// other [point] values
27-
CustomPoint<T> unscaleBy(CustomPoint<T> point) {
28-
return CustomPoint<T>(x / point.x, y / point.y);
28+
/// Create new [CustomPoint] whose [x] and [y] values are divided by other
29+
/// [point] values
30+
CustomPoint<double> unscaleBy(CustomPoint<T> point) {
31+
return CustomPoint<double>(x / point.x, y / point.y);
2932
}
3033

31-
/// Create new [CustomPoint] where [other] point values [x] and [y] are added
34+
/// Create new [CustomPoint] where [x] and [y] values are multiplied by
35+
/// [factor]
3236
@override
33-
CustomPoint<T> operator +(math.Point<T> other) {
34-
return CustomPoint<T>(x + other.x, y + other.y);
37+
CustomPoint<T> operator *(num factor) {
38+
return CustomPoint<T>((x * factor) as T, (y * factor) as T);
3539
}
3640

37-
/// Create new [CustomPoint] where [x] and [y] values are subtracted from
38-
/// [other] point [x] and [y] values
39-
@override
40-
CustomPoint<T> operator -(math.Point<T> other) {
41-
return CustomPoint<T>(x - other.x, y - other.y);
41+
/// Create new [CustomPoint] where [x] and [y] values are divided by [factor]
42+
CustomPoint<T> operator /(num factor) {
43+
return CustomPoint<T>((x / factor) as T, (y / factor) as T);
4244
}
4345

44-
/// Create new [CustomPoint] where [x] and [y] are multiplied by [factor]
45-
@override
46-
CustomPoint<T> operator *(num /*T|int*/ factor) {
47-
return CustomPoint<T>((x * factor), (y * factor));
48-
}
49-
50-
/// Create new [CustomPoint] where [x] and [y] are scaled by [point] values
51-
CustomPoint scaleBy(CustomPoint point) {
52-
return CustomPoint(x * point.x, y * point.y);
46+
/// Create new [CustomPoint] where [x] and [y] is rounded to int
47+
CustomPoint<int> round() {
48+
return CustomPoint<int>(x.round(), y.round());
5349
}
5450

55-
/// Create new [CustomPoint] where [x] and [y] is rounded to int
56-
CustomPoint round() {
57-
final x = this.x is double ? this.x.round() : this.x;
58-
final y = this.y is double ? this.y.round() : this.y;
59-
return CustomPoint(x, y);
51+
/// Create new [CustomPoint] where [x] and [y] values are rounded up to int
52+
CustomPoint<int> ceil() {
53+
return CustomPoint<int>(x.ceil(), y.ceil());
6054
}
6155

62-
/// Create new [CustomPoint] with [x] and [y] multiplied by [n]
63-
CustomPoint multiplyBy(num n) {
64-
return CustomPoint(x * n, y * n);
56+
/// Create new [CustomPoint] where [x] and [y] values are rounded down to int
57+
CustomPoint<int> floor() {
58+
return CustomPoint<int>(x.floor(), y.floor());
6559
}
6660

6761
/// Create new [CustomPoint] whose [x] and [y] values are rotated by [radians]
6862
/// in clockwise fashion
69-
CustomPoint rotate(num radians) {
63+
CustomPoint<double> rotate(num radians) {
7064
if (radians != 0.0) {
7165
final cos = math.cos(radians);
7266
final sin = math.sin(radians);
7367
final nx = (cos * x) + (sin * y);
7468
final ny = (cos * y) - (sin * x);
7569

76-
return CustomPoint(nx, ny);
70+
return CustomPoint<double>(nx, ny);
7771
}
7872

79-
return this;
73+
return CustomPoint(x.toDouble(), y.toDouble());
8074
}
8175

82-
CustomPoint<U> cast<U extends num>() => CustomPoint<U>(x as U, y as U);
76+
CustomPoint<int> toIntPoint() => CustomPoint<int>(x.toInt(), y.toInt());
77+
78+
CustomPoint<double> toDoublePoint() =>
79+
CustomPoint<double>(x.toDouble(), y.toDouble());
8380

8481
@override
8582
String toString() => 'CustomPoint ($x, $y)';
83+
84+
/// Create new [CustomPoint] with [x] and [y] multiplied by [n]
85+
CustomPoint<T> multiplyBy(num n) {
86+
return this * n;
87+
}
8688
}

lib/src/geo/crs/crs.dart

+14-15
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,13 @@ abstract class Crs {
2323

2424
/// Converts a point on the sphere surface (with a certain zoom) in a
2525
/// map point.
26-
CustomPoint latLngToPoint(LatLng latlng, double zoom) {
26+
CustomPoint<double> latLngToPoint(LatLng latlng, double zoom) {
2727
try {
2828
final projectedPoint = projection.project(latlng);
2929
final scale = this.scale(zoom);
3030
return transformation.transform(projectedPoint, scale.toDouble());
3131
} catch (e) {
32-
return const CustomPoint(0.0, 0.0);
32+
return const CustomPoint(0, 0);
3333
}
3434
}
3535

@@ -46,8 +46,8 @@ abstract class Crs {
4646
}
4747

4848
/// Zoom to Scale function.
49-
num scale(double zoom) {
50-
return 256 * math.pow(2, zoom);
49+
double scale(double zoom) {
50+
return 256.0 * math.pow(2, zoom);
5151
}
5252

5353
/// Scale to Zoom function.
@@ -236,15 +236,15 @@ class Proj4Crs extends Crs {
236236
/// Converts a point on the sphere surface (with a certain zoom) in a
237237
/// map point.
238238
@override
239-
CustomPoint latLngToPoint(LatLng latlng, double zoom) {
239+
CustomPoint<double> latLngToPoint(LatLng latlng, double zoom) {
240240
try {
241241
final projectedPoint = projection.project(latlng);
242242
final scale = this.scale(zoom);
243243
final transformation = _getTransformationByZoom(zoom);
244244

245245
return transformation.transform(projectedPoint, scale.toDouble());
246246
} catch (e) {
247-
return const CustomPoint(0.0, 0.0);
247+
return const CustomPoint(0, 0);
248248
}
249249
}
250250

@@ -280,7 +280,7 @@ class Proj4Crs extends Crs {
280280

281281
/// Zoom to Scale function.
282282
@override
283-
num scale(double zoom) {
283+
double scale(double zoom) {
284284
final iZoom = zoom.floor();
285285
if (zoom == iZoom) {
286286
return _scales[iZoom];
@@ -346,7 +346,7 @@ abstract class Projection {
346346

347347
Bounds<double>? get bounds;
348348

349-
CustomPoint project(LatLng latlng);
349+
CustomPoint<double> project(LatLng latlng);
350350

351351
LatLng unproject(CustomPoint point);
352352

@@ -370,16 +370,15 @@ abstract class Projection {
370370

371371
class _LonLat extends Projection {
372372
static final Bounds<double> _bounds = Bounds<double>(
373-
const CustomPoint<double>(-180.0, -90.0),
374-
const CustomPoint<double>(180.0, 90.0));
373+
const CustomPoint<double>(-180, -90), const CustomPoint<double>(180, 90));
375374

376375
const _LonLat() : super();
377376

378377
@override
379378
Bounds<double> get bounds => _bounds;
380379

381380
@override
382-
CustomPoint project(LatLng latlng) {
381+
CustomPoint<double> project(LatLng latlng) {
383382
return CustomPoint(latlng.longitude, latlng.latitude);
384383
}
385384

@@ -405,7 +404,7 @@ class SphericalMercator extends Projection {
405404
Bounds<double> get bounds => _bounds;
406405

407406
@override
408-
CustomPoint project(LatLng latlng) {
407+
CustomPoint<double> project(LatLng latlng) {
409408
const d = math.pi / 180;
410409
const max = maxLatitude;
411410
final lat = math.max(math.min(max, latlng.latitude), -max);
@@ -439,7 +438,7 @@ class _Proj4Projection extends Projection {
439438
}) : epsg4326 = proj4.Projection.WGS84;
440439

441440
@override
442-
CustomPoint project(LatLng latlng) {
441+
CustomPoint<double> project(LatLng latlng) {
443442
final point = epsg4326.transform(
444443
proj4Projection, proj4.Point(x: latlng.longitude, y: latlng.latitude));
445444

@@ -463,14 +462,14 @@ class Transformation {
463462

464463
const Transformation(this.a, this.b, this.c, this.d);
465464

466-
CustomPoint transform(CustomPoint<num> point, double? scale) {
465+
CustomPoint<double> transform(CustomPoint point, double? scale) {
467466
scale ??= 1.0;
468467
final x = scale * (a * point.x + b);
469468
final y = scale * (c * point.y + d);
470469
return CustomPoint(x, y);
471470
}
472471

473-
CustomPoint untransform(CustomPoint point, double? scale) {
472+
CustomPoint<double> untransform(CustomPoint point, double? scale) {
474473
scale ??= 1.0;
475474
final x = (point.x / scale - b) / a;
476475
final y = (point.y / scale - d) / c;

lib/src/gestures/gestures.dart

+4-4
Original file line numberDiff line numberDiff line change
@@ -555,7 +555,7 @@ abstract class MapGestureMixin extends State<FlutterMap>
555555

556556
final direction = details.velocity.pixelsPerSecond / magnitude;
557557
final distance = (Offset.zero &
558-
Size(mapState.nonrotatedSize!.x, mapState.nonrotatedSize!.y))
558+
Size(mapState.nonrotatedSize.x, mapState.nonrotatedSize.y))
559559
.shortestSide;
560560

561561
final flingOffset = _focalStartLocal - _lastFocalLocal;
@@ -648,7 +648,7 @@ abstract class MapGestureMixin extends State<FlutterMap>
648648
LatLng _offsetToCrs(Offset offset, [double? zoom]) {
649649
final focalStartPt =
650650
mapState.project(mapState.center, zoom ?? mapState.zoom);
651-
final point = (_offsetToPoint(offset) - (mapState.nonrotatedSize! / 2.0))
651+
final point = (_offsetToPoint(offset) - (mapState.nonrotatedSize / 2.0))
652652
.rotate(mapState.rotationRad);
653653

654654
final newCenterPt = focalStartPt + point;
@@ -678,7 +678,7 @@ abstract class MapGestureMixin extends State<FlutterMap>
678678
List<dynamic> _getNewEventCenterZoomPosition(
679679
CustomPoint cursorPos, double newZoom) {
680680
// Calculate offset of mouse cursor from viewport center
681-
final viewCenter = mapState.nonrotatedSize! / 2;
681+
final viewCenter = mapState.nonrotatedSize / 2;
682682
final offset = (cursorPos - viewCenter).rotate(mapState.rotationRad);
683683
// Match new center coordinate to mouse cursor position
684684
final scale = mapState.getZoomScale(newZoom, mapState.zoom);
@@ -818,7 +818,7 @@ abstract class MapGestureMixin extends State<FlutterMap>
818818
closeFlingAnimationController(event.source);
819819
}
820820

821-
CustomPoint _offsetToPoint(Offset offset) {
821+
CustomPoint<double> _offsetToPoint(Offset offset) {
822822
return CustomPoint(offset.dx, offset.dy);
823823
}
824824

lib/src/layer/tile_layer/tile_range.dart

+3-4
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,12 @@ class DiscreteTileRange extends TileRange {
3333
}) {
3434
final Bounds<int> bounds;
3535
if (pixelBounds.min == pixelBounds.max) {
36-
final minAndMax = (pixelBounds.min / tileSize).floor().cast<int>();
36+
final minAndMax = (pixelBounds.min / tileSize).floor();
3737
bounds = Bounds<int>(minAndMax, minAndMax);
3838
} else {
3939
bounds = Bounds<int>(
40-
(pixelBounds.min / tileSize).floor().cast<int>(),
41-
(pixelBounds.max / tileSize).ceil().cast<int>() -
42-
const CustomPoint(1, 1),
40+
(pixelBounds.min / tileSize).floor(),
41+
(pixelBounds.max / tileSize).ceil() - const CustomPoint(1, 1),
4342
);
4443
}
4544

0 commit comments

Comments
 (0)