From 4ebd5168946ec7cc41cbc92f6c10d05c55cdbc33 Mon Sep 17 00:00:00 2001 From: Rory Stephenson Date: Mon, 19 Jun 2023 10:40:27 +0200 Subject: [PATCH] Pre-calculate Marker's Anchor This allows Marker to accept the same anchor argument as MarkerLayer, anchorPos, whilst still precalculating the Anchor so it isn't calculated on every build. Co-authored-by: Luka S --- example/lib/pages/markers.dart | 9 +++------ lib/src/layer/marker_layer.dart | 15 +++++++++------ 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/example/lib/pages/markers.dart b/example/lib/pages/markers.dart index 2300ede69..df79fedc4 100644 --- a/example/lib/pages/markers.dart +++ b/example/lib/pages/markers.dart @@ -129,8 +129,7 @@ class MarkerPageState extends State { 47.18664724067855, -1.5436768515939427), width: 64, height: 64, - anchor: Anchor.fromPos( - AnchorPos.align(AnchorAlign.left), 64, 64), + anchorPos: AnchorPos.align(AnchorAlign.left), builder: (context) => const ColoredBox( color: Colors.lightBlue, child: Align( @@ -144,8 +143,7 @@ class MarkerPageState extends State { 47.18664724067855, -1.5436768515939427), width: 64, height: 64, - anchor: Anchor.fromPos( - AnchorPos.align(AnchorAlign.right), 64, 64), + anchorPos: AnchorPos.align(AnchorAlign.right), builder: (context) => const ColoredBox( color: Colors.pink, child: Align( @@ -158,8 +156,7 @@ class MarkerPageState extends State { point: const LatLng( 47.18664724067855, -1.5436768515939427), rotate: false, - anchor: Anchor.fromPos( - AnchorPos.align(AnchorAlign.center), 30, 30), + anchorPos: AnchorPos.align(AnchorAlign.center), builder: (context) => const ColoredBox(color: Colors.black), ), diff --git a/lib/src/layer/marker_layer.dart b/lib/src/layer/marker_layer.dart index e523b4b9f..b94afdd89 100644 --- a/lib/src/layer/marker_layer.dart +++ b/lib/src/layer/marker_layer.dart @@ -10,11 +10,13 @@ import 'package:latlong2/latlong.dart'; /// Can be defined exactly (using [AnchorPos.exactly] with an [Anchor]) or in /// a relative alignment (using [AnchorPos.align] with an [AnchorAlign]). class AnchorPos { + static const defaultAnchorPos = AnchorPos.align(AnchorAlign.center); + final Anchor? anchor; final AnchorAlign? alignment; - AnchorPos.exactly(Anchor this.anchor) : alignment = null; - AnchorPos.align(AnchorAlign this.alignment) : anchor = null; + const AnchorPos.exactly(Anchor this.anchor) : alignment = null; + const AnchorPos.align(AnchorAlign this.alignment) : anchor = null; } /// Exact alignment for a [Marker.builder] widget relative to the center @@ -26,7 +28,7 @@ class Anchor { final double left; final double top; - Anchor(this.left, this.top); + const Anchor(this.left, this.top); factory Anchor.fromPos(AnchorPos pos, double width, double height) { if (pos.anchor case final anchor?) return anchor; @@ -131,11 +133,12 @@ class Marker { required this.builder, this.width = 30.0, this.height = 30.0, - this.anchor, + AnchorPos? anchorPos, this.rotate, this.rotateOrigin, this.rotateAlignment, - }); + }) : anchor = + anchorPos == null ? null : Anchor.fromPos(anchorPos, width, height); } class MarkerLayer extends StatelessWidget { @@ -202,7 +205,7 @@ class MarkerLayer extends StatelessWidget { // unlike the map coordinates. final anchor = marker.anchor ?? Anchor.fromPos( - anchorPos ?? AnchorPos.align(AnchorAlign.center), + anchorPos ?? AnchorPos.defaultAnchorPos, marker.width, marker.height, );