Skip to content

Commit

Permalink
Migrate to Dart 3.0 (#1517)
Browse files Browse the repository at this point in the history
#1512 (5fb3b3b) bumped the minimum Flutter SDK to 3.10, but did not change the minimum Dart SDK. This PR changes the Dart SDK to 3.0 minimum, enabling the new features and performance benefits.

In addition, this PR:

* Removes the Tuple dependency in favour of Records
* Uses new pattern matching switches in some locations
* Seals the `TileDisplay` class to benefit from new pattern matching switches, instead of switching on `runtimeType`
* Deprecates `TileUpdateTransformers.alwaysLoadAndPrune`, as it should be unnecessary compared to `ignoreTapEvents`
* Improves some documentation
* Includes some other more minor improvements/changes

This PR also fixes the failing Android builds by upgrading Gradle.
  • Loading branch information
josxha authored May 15, 2023
1 parent 7538c66 commit 9a13031
Show file tree
Hide file tree
Showing 21 changed files with 171 additions and 188 deletions.
2 changes: 1 addition & 1 deletion example/android/build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
buildscript {
ext.kotlin_version = '1.6.20'
ext.kotlin_version = '1.8.21'
repositories {
google()
jcenter()
Expand Down
2 changes: 1 addition & 1 deletion example/android/gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.2-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-7.6-all.zip
2 changes: 1 addition & 1 deletion example/lib/pages/marker_anchor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class MarkerAnchorPage extends StatefulWidget {
}

class MarkerAnchorPageState extends State<MarkerAnchorPage> {
late AnchorPos<dynamic> anchorPos;
late AnchorPos anchorPos;

@override
void initState() {
Expand Down
2 changes: 1 addition & 1 deletion example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ publish_to: "none"
version: 5.0.0

environment:
sdk: ">=2.18.0 <4.0.0"
sdk: ">=3.0.0 <4.0.0"
flutter: ">=3.10.0"

dependencies:
Expand Down
5 changes: 0 additions & 5 deletions lib/src/core/point.dart
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,4 @@ class CustomPoint<T extends num> extends math.Point<T> {

@override
String toString() => 'CustomPoint ($x, $y)';

/// Create new [CustomPoint] with [x] and [y] multiplied by [n]
CustomPoint<T> multiplyBy(num n) {
return this * n;
}
}
19 changes: 9 additions & 10 deletions lib/src/geo/crs/crs.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import 'package:flutter_map/src/core/point.dart';
import 'package:latlong2/latlong.dart';
import 'package:meta/meta.dart';
import 'package:proj4dart/proj4dart.dart' as proj4;
import 'package:tuple/tuple.dart';

/// An abstract representation of a
/// [Coordinate Reference System](https://bit.ly/3iVKpja).
Expand Down Expand Up @@ -68,9 +67,9 @@ abstract class Crs {

bool get infinite;

Tuple2<double, double>? get wrapLng;
(double, double)? get wrapLng;

Tuple2<double, double>? get wrapLat;
(double, double)? get wrapLat;
}

// Custom CRS for non geographical maps
Expand All @@ -93,21 +92,21 @@ class CrsSimple extends Crs {
bool get infinite => false;

@override
Tuple2<double, double>? get wrapLat => null;
(double, double)? get wrapLat => null;

@override
Tuple2<double, double>? get wrapLng => null;
(double, double)? get wrapLng => null;
}

abstract class Earth extends Crs {
@override
bool get infinite => false;

@override
final Tuple2<double, double> wrapLng = const Tuple2(-180, 180);
final (double, double) wrapLng = const (-180, 180);

@override
final Tuple2<double, double>? wrapLat = null;
final (double, double)? wrapLat = null;

const Earth() : super();
}
Expand All @@ -132,7 +131,7 @@ class Epsg3857 extends Earth {

// Epsg3857 seems to have latitude limits. https://epsg.io/3857
//@override
//Tuple2<double, double> get wrapLat => const Tuple2(-85.06, 85.06);
//(double, double) get wrapLat => const (-85.06, 85.06);
}

/// A common CRS among GIS enthusiasts. Uses simple Equirectangular projection.
Expand Down Expand Up @@ -167,10 +166,10 @@ class Proj4Crs extends Crs {
final bool infinite;

@override
final Tuple2<double, double>? wrapLat = null;
final (double, double)? wrapLat = null;

@override
final Tuple2<double, double>? wrapLng = null;
final (double, double)? wrapLng = null;

final List<Transformation>? _transformations;

Expand Down
80 changes: 38 additions & 42 deletions lib/src/gestures/map_events.dart
Original file line number Diff line number Diff line change
Expand Up @@ -72,48 +72,44 @@ abstract class MapEventWithMove extends MapEvent {
required bool hasGesture,
required MapEventSource source,
String? id,
}) {
switch (source) {
case MapEventSource.flingAnimationController:
return MapEventFlingAnimation(
center: oldCenter,
zoom: oldZoom,
targetCenter: targetCenter,
targetZoom: targetZoom,
source: source,
);
case MapEventSource.doubleTapZoomAnimationController:
return MapEventDoubleTapZoom(
center: oldCenter,
zoom: oldZoom,
targetCenter: targetCenter,
targetZoom: targetZoom,
source: source,
);
case MapEventSource.scrollWheel:
return MapEventScrollWheelZoom(
center: oldCenter,
zoom: oldZoom,
targetCenter: targetCenter,
targetZoom: targetZoom,
source: source,
);
case MapEventSource.onDrag:
case MapEventSource.onMultiFinger:
case MapEventSource.mapController:
case MapEventSource.custom:
return MapEventMove(
id: id,
center: oldCenter,
zoom: oldZoom,
targetCenter: targetCenter,
targetZoom: targetZoom,
source: source,
);
default:
return null;
}
}
}) =>
switch (source) {
MapEventSource.flingAnimationController => MapEventFlingAnimation(
center: oldCenter,
zoom: oldZoom,
targetCenter: targetCenter,
targetZoom: targetZoom,
source: source,
),
MapEventSource.doubleTapZoomAnimationController =>
MapEventDoubleTapZoom(
center: oldCenter,
zoom: oldZoom,
targetCenter: targetCenter,
targetZoom: targetZoom,
source: source,
),
MapEventSource.scrollWheel => MapEventScrollWheelZoom(
center: oldCenter,
zoom: oldZoom,
targetCenter: targetCenter,
targetZoom: targetZoom,
source: source,
),
MapEventSource.onDrag ||
MapEventSource.onMultiFinger ||
MapEventSource.mapController ||
MapEventSource.custom =>
MapEventMove(
id: id,
center: oldCenter,
zoom: oldZoom,
targetCenter: targetCenter,
targetZoom: targetZoom,
source: source,
),
_ => null,
};
}

/// Event which is fired when map is tapped
Expand Down
2 changes: 1 addition & 1 deletion lib/src/layer/attribution_layer/animation.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import 'package:flutter_map/src/layer/attribution_layer/rich.dart';
/// [AnimatedSwitcher], but its curve and duration can still be customized
/// through [buttonDuration] and [buttonCurve].
///
/// Can be extensivley customized by implementing a custom
/// Can be extensively customized by implementing a custom
/// [RichAttributionWidgetAnimation], or the prebuilt [FadeRAWA] and
/// [ScaleRAWA] animations can be used with limited customization.
abstract class RichAttributionWidgetAnimation {
Expand Down
13 changes: 4 additions & 9 deletions lib/src/layer/label.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,10 @@ class Label {
bool rotate = false,
PolygonLabelPlacement labelPlacement = PolygonLabelPlacement.polylabel,
}) {
late Offset placementPoint;
switch (labelPlacement) {
case PolygonLabelPlacement.centroid:
placementPoint = _computeCentroid(points);
break;
case PolygonLabelPlacement.polylabel:
placementPoint = _computePolylabel(points);
break;
}
final placementPoint = switch (labelPlacement) {
PolygonLabelPlacement.centroid => _computeCentroid(points),
PolygonLabelPlacement.polylabel => _computePolylabel(points),
};

var dx = placementPoint.dx;
var dy = placementPoint.dy;
Expand Down
67 changes: 25 additions & 42 deletions lib/src/layer/marker_layer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,51 +14,34 @@ class Anchor {
: left = _leftOffset(width, alignOpt),
top = _topOffset(height, alignOpt);

static double _leftOffset(double width, AnchorAlign alignOpt) {
switch (alignOpt) {
case AnchorAlign.left:
return 0;
case AnchorAlign.right:
return width;
case AnchorAlign.top:
case AnchorAlign.bottom:
case AnchorAlign.center:
default:
return width / 2;
}
}

static double _topOffset(double height, AnchorAlign alignOpt) {
switch (alignOpt) {
case AnchorAlign.top:
return 0;
case AnchorAlign.bottom:
return height;
case AnchorAlign.left:
case AnchorAlign.right:
case AnchorAlign.center:
default:
return height / 2;
}
}

factory Anchor.forPos(AnchorPos<dynamic>? pos, double width, double height) {
static double _leftOffset(double width, AnchorAlign alignOpt) =>
switch (alignOpt) {
AnchorAlign.left => 0,
AnchorAlign.right => width,
_ => width / 2,
};

static double _topOffset(double height, AnchorAlign alignOpt) =>
switch (alignOpt) {
AnchorAlign.top => 0,
AnchorAlign.bottom => height,
_ => height / 2,
};

factory Anchor.forPos(AnchorPos? pos, double width, double height) {
if (pos == null) return Anchor._(width, height, AnchorAlign.none);
if (pos.value is AnchorAlign) {
return Anchor._(width, height, pos.value as AnchorAlign);
}
if (pos.value is Anchor) return pos.value as Anchor;
throw Exception('Unsupported AnchorPos value type: ${pos.runtimeType}.');
if (pos.alignment case final align?) return Anchor._(width, height, align);
if (pos.anchor case final anchor?) return anchor;
throw Exception();
}
}

class AnchorPos<T> {
AnchorPos._(this.value);
T value;
static AnchorPos<Anchor> exactly(Anchor anchor) =>
AnchorPos<Anchor>._(anchor);
static AnchorPos<AnchorAlign> align(AnchorAlign alignOpt) =>
AnchorPos<AnchorAlign>._(alignOpt);
class AnchorPos {
final Anchor? anchor;
final AnchorAlign? alignment;

AnchorPos.exactly(this.anchor) : alignment = null;
AnchorPos.align(this.alignment) : anchor = null;
}

enum AnchorAlign {
Expand Down Expand Up @@ -119,7 +102,7 @@ class Marker {
this.rotate,
this.rotateOrigin,
this.rotateAlignment,
AnchorPos<dynamic>? anchorPos,
AnchorPos? anchorPos,
}) : anchor = Anchor.forPos(anchorPos, width, height);
}

Expand Down
25 changes: 10 additions & 15 deletions lib/src/layer/tile_layer/tile_bounds/tile_bounds.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import 'package:flutter_map/src/geo/latlng_bounds.dart';
import 'package:flutter_map/src/layer/tile_layer/tile_bounds/tile_bounds_at_zoom.dart';
import 'package:flutter_map/src/layer/tile_layer/tile_range.dart';
import 'package:latlong2/latlong.dart';
import 'package:tuple/tuple.dart';

abstract class TileBounds {
final Crs crs;
Expand Down Expand Up @@ -124,30 +123,26 @@ class WrappedTileBounds extends TileBounds {

final tzDouble = zoom.toDouble();

Tuple2<int, int>? wrapX;
if (crs.wrapLng != null) {
(int, int)? wrapX;
if (crs.wrapLng case final wrapLng?) {
final wrapXMin =
(crs.latLngToPoint(LatLng(0, crs.wrapLng!.item1), tzDouble).x /
_tileSize)
(crs.latLngToPoint(LatLng(0, wrapLng.$1), tzDouble).x / _tileSize)
.floor();
final wrapXMax =
(crs.latLngToPoint(LatLng(0, crs.wrapLng!.item2), tzDouble).x /
_tileSize)
(crs.latLngToPoint(LatLng(0, wrapLng.$2), tzDouble).x / _tileSize)
.ceil();
wrapX = Tuple2(wrapXMin, wrapXMax - 1);
wrapX = (wrapXMin, wrapXMax - 1);
}

Tuple2<int, int>? wrapY;
if (crs.wrapLat != null) {
(int, int)? wrapY;
if (crs.wrapLat case final wrapLat?) {
final wrapYMin =
(crs.latLngToPoint(LatLng(crs.wrapLat!.item1, 0), tzDouble).y /
_tileSize)
(crs.latLngToPoint(LatLng(wrapLat.$1, 0), tzDouble).y / _tileSize)
.floor();
final wrapYMax =
(crs.latLngToPoint(LatLng(crs.wrapLat!.item2, 0), tzDouble).y /
_tileSize)
(crs.latLngToPoint(LatLng(wrapLat.$2, 0), tzDouble).y / _tileSize)
.ceil();
wrapY = Tuple2(wrapYMin, wrapYMax - 1);
wrapY = (wrapYMin, wrapYMax - 1);
}

return WrappedTileBoundsAtZoom(
Expand Down
11 changes: 5 additions & 6 deletions lib/src/layer/tile_layer/tile_bounds/tile_bounds_at_zoom.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import 'package:flutter_map/src/core/point.dart';
import 'package:flutter_map/src/layer/tile_layer/tile_coordinates.dart';
import 'package:flutter_map/src/layer/tile_layer/tile_range.dart';
import 'package:tuple/tuple.dart';

abstract class TileBoundsAtZoom {
const TileBoundsAtZoom();
Expand Down Expand Up @@ -46,8 +45,8 @@ class DiscreteTileBoundsAtZoom extends TileBoundsAtZoom {
class WrappedTileBoundsAtZoom extends TileBoundsAtZoom {
final DiscreteTileRange tileRange;
final bool wrappedAxisIsAlwaysInBounds;
final Tuple2<int, int>? wrapX;
final Tuple2<int, int>? wrapY;
final (int, int)? wrapX;
final (int, int)? wrapY;

const WrappedTileBoundsAtZoom({
required this.tileRange,
Expand Down Expand Up @@ -120,9 +119,9 @@ class WrappedTileBoundsAtZoom extends TileBoundsAtZoom {
}

/// Wrap [x] to be within [range] inclusive.
int _wrapInt(int x, Tuple2<int, int> range) {
final d = range.item2 + 1 - range.item1;
return ((x - range.item1) % d + d) % d + range.item1;
int _wrapInt(int x, (int, int) range) {
final d = range.$2 + 1 - range.$1;
return ((x - range.$1) % d + d) % d + range.$1;
}

@override
Expand Down
2 changes: 1 addition & 1 deletion lib/src/layer/tile_layer/tile_coordinates.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import 'package:flutter_map/flutter_map.dart';
class TileCoordinates extends CustomPoint<int> {
final int z;

const TileCoordinates(int x, int y, this.z) : super(x, y);
const TileCoordinates(super.x, super.y, this.z);

String get key => '$x:$y:$z';

Expand Down
Loading

0 comments on commit 9a13031

Please sign in to comment.