Skip to content

Commit

Permalink
Pre-calculate Marker's Anchor
Browse files Browse the repository at this point in the history
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 <github@jaffaketchup.dev>
  • Loading branch information
rorystephenson and JaffaKetchup committed Jun 19, 2023
1 parent 7a8705a commit 4ebd516
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 12 deletions.
9 changes: 3 additions & 6 deletions example/lib/pages/markers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,7 @@ class MarkerPageState extends State<MarkerPage> {
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(
Expand All @@ -144,8 +143,7 @@ class MarkerPageState extends State<MarkerPage> {
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(
Expand All @@ -158,8 +156,7 @@ class MarkerPageState extends State<MarkerPage> {
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),
),
Expand Down
15 changes: 9 additions & 6 deletions lib/src/layer/marker_layer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
);
Expand Down

0 comments on commit 4ebd516

Please sign in to comment.