Skip to content

Commit

Permalink
make polywidget clickable
Browse files Browse the repository at this point in the history
  • Loading branch information
TimBaumgart committed Sep 7, 2023
1 parent e503568 commit 9175fb9
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 17 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,8 @@
## 0.0.9

* disable automatic deployment as it does not work
yet (https://github.com/dart-lang/setup-dart/issues/68)
yet (https://github.com/dart-lang/setup-dart/issues/68)

## 0.1.0

* make polywidget clickable by using Positioned() instead of Transform.translate()
29 changes: 16 additions & 13 deletions lib/polywidget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,13 @@ class PolyWidget extends StatelessWidget {

@override
Widget build(BuildContext context) {
final mapState = FlutterMapState.of(context);
Offset centerOffset = mapState.getOffsetFromOrigin(center);
double width =
_calcLength(mapState, center, centerOffset, widthInMeters, 90);
double height =
_calcLength(mapState, center, centerOffset, heightInMeters, 180);
final map = MapCamera.of(context);
Offset centerOffset = map.getOffsetFromOrigin(center);
double width = _calcLength(map, center, centerOffset, widthInMeters, 90);
double height = _calcLength(map, center, centerOffset, heightInMeters, 180);

int turns = _calcTurns(
width, height, mapState.rotation + angle, forceOrientation, noRotation);
width, height, map.rotation + angle, forceOrientation, noRotation);
double rotation = angle - (turns * 90);

if (turns.isOdd) {
Expand All @@ -46,8 +44,13 @@ class PolyWidget extends StatelessWidget {
height = temp;
}

return Transform.translate(
offset: centerOffset.translate(-width / 2, -height / 2),
Offset offset = centerOffset.translate(-width / 2, -height / 2);

return Positioned(
left: offset.dx,
top: offset.dy,
width: width,
height: height,
child: Transform.rotate(
angle: degToRadian(rotation),
child: SizedBox(
Expand Down Expand Up @@ -97,14 +100,14 @@ class PolyWidget extends StatelessWidget {

/// calculates the current screen distance for [lengthInMeters]
double _calcLength(
FlutterMapState mapState,
MapCamera mapCamera,
LatLng center,
Offset centerOffset,
int lengthInMeters,
int angleRad,
int angle,
) {
LatLng latLng = const Distance().offset(center, lengthInMeters, angleRad);
Offset offset = mapState.getOffsetFromOrigin(latLng);
LatLng latLng = const Distance().offset(center, lengthInMeters, angle);
Offset offset = mapCamera.getOffsetFromOrigin(latLng);
double width =
Offset(offset.dx - centerOffset.dx, offset.dy - centerOffset.dy)
.distance;
Expand Down
1 change: 0 additions & 1 deletion lib/tile_layer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ class PolyWidgetLayer extends StatelessWidget {

@override
Widget build(BuildContext context) {
// final mapState = FlutterMapState.of(context);
return Stack(
children: polyWidgets,
);
Expand Down
4 changes: 2 additions & 2 deletions pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: flutter_map_polywidget
description: flutter_map_polywidget is a flutter_map plugin for displaying any widget on the map.
version: 0.0.9
version: 0.1.0-dev.1
homepage: https://github.com/TimBaumgart/flutter_map_polywidget

environment:
Expand All @@ -10,7 +10,7 @@ environment:
dependencies:
flutter:
sdk: flutter
flutter_map: ^5.0.0
flutter_map: ^6.0.0-dev.3
latlong2: ^0.9.0

dev_dependencies:
Expand Down

0 comments on commit 9175fb9

Please sign in to comment.