Stand 2025-01-06

This commit is contained in:
Jens Reinemuth 2025-01-06 12:55:24 +01:00
parent 16f1442564
commit b30db04fe2

View file

@ -7,40 +7,48 @@ import '../../consts/consts.dart';
import '../../packages/ambito_theme/ambito_theme.dart'; import '../../packages/ambito_theme/ambito_theme.dart';
class MapWidget extends StatefulWidget { class MapWidget extends StatefulWidget {
const MapWidget( const MapWidget({
{super.key, super.key,
required this.markers, required this.initialPosition, // Make initial position required
required this.polygons, this.markers = const {}, // Provide default empty sets
required this.polylines}); this.polygons = const {},
this.polylines = const {},
});
final LatLng initialPosition; // Use LatLng directly
final Set<Marker> markers; final Set<Marker> markers;
final Set<Polygon> polygons; final Set<Polygon> polygons;
final Set<Polyline> polylines; final Set<Polyline> polylines;
@override @override
State<StatefulWidget> createState() => MapWidgetState(); State<MapWidget> createState() => _MapWidgetState(); // Use shorter syntax
} }
class MapWidgetState extends State<MapWidget> { class _MapWidgetState extends State<MapWidget> {
final Completer<GoogleMapController> _controller = final Completer<GoogleMapController> _controller = Completer();
Completer<GoogleMapController>();
bool _drawPolygon = false; bool _drawPolygon = false;
int _counter = 0; int _counter = 0;
static CameraPosition _initialPos = const CameraPosition( late CameraPosition _initialCameraPosition; // Use late for initialization
target: LatLng(0, 0),
zoom: 14.4746,
);
@override @override
void initState() { void initState() {
setState(() {
_initialPos =
CameraPosition(target: widget.markers.first.position, zoom: 14);
});
super.initState(); super.initState();
_initialCameraPosition = CameraPosition(
target: widget.initialPosition,
zoom: 14,
);
}
@override
void didUpdateWidget(covariant MapWidget oldWidget) {
super.didUpdateWidget(oldWidget);
if (oldWidget.initialPosition != widget.initialPosition) {
_initialCameraPosition = CameraPosition(
target: widget.initialPosition,
zoom: 14,
);
}
} }
@override @override
@ -54,44 +62,13 @@ class MapWidgetState extends State<MapWidget> {
children: [ children: [
Expanded( Expanded(
child: GoogleMap( child: GoogleMap(
onTap: (position) { onTap: _handleMapTap, // Extract onTap logic
if (_drawPolygon == true) {
setState(() {
widget.markers.add(
Marker(
onTap: onTapMarker('polygon_$_counter'),
markerId: MarkerId('polygon_$_counter'),
position: position,
),
);
if (_counter > 0) {
logger.i('add polyline');
widget.polylines.add(
Polyline(
polylineId: PolylineId('polyline_$_counter'),
color: theme.currentColorScheme.primary,
width: 2,
points: [
widget.markers
.where((el) =>
el.markerId ==
MarkerId('polygon_${_counter - 1}'))
.first
.position,
position
]),
);
}
_counter++;
});
}
},
mapToolbarEnabled: true, mapToolbarEnabled: true,
mapType: MapType.hybrid, mapType: MapType.hybrid,
markers: widget.markers, markers: widget.markers,
polygons: widget.polygons, polygons: widget.polygons,
polylines: widget.polylines, polylines: widget.polylines,
initialCameraPosition: _initialPos, initialCameraPosition: _initialCameraPosition,
onMapCreated: (GoogleMapController controller) { onMapCreated: (GoogleMapController controller) {
_controller.complete(controller); _controller.complete(controller);
}, },
@ -101,27 +78,67 @@ class MapWidgetState extends State<MapWidget> {
children: [ children: [
const Spacer(), const Spacer(),
IconButton( IconButton(
onPressed: () { onPressed: () => setState(
setState(() { () => _drawPolygon = !_drawPolygon), // Shorter syntax
_drawPolygon = !_drawPolygon;
});
},
icon: Icon( icon: Icon(
Icons.polyline_outlined, Icons.polyline_outlined,
color: (_drawPolygon color: _drawPolygon
? theme.currentColorScheme.primary ? theme.currentColorScheme.primary
: theme.currentColorScheme.outline), : theme.currentColorScheme.outline,
)) ),
),
], ],
), ),
], ],
)), ),
),
); );
} }
onTapMarker(String counter) { void _handleMapTap(LatLng position) {
logger.d(counter); final AmbitoTheme theme = getTheme(context);
if (counter == 'polygon_0') { if (_drawPolygon) {
setState(() {
final markerId = MarkerId('polygon_$_counter');
widget.markers.add(
Marker(
onTap: () => _onTapMarker(markerId.value), // Use closure for onTap
markerId: markerId,
position: position,
),
);
if (_counter > 0) {
final previousMarkerId = MarkerId('polygon_${_counter - 1}');
final previousMarker = widget.markers.firstWhere(
(el) => el.markerId == previousMarkerId,
orElse: () => Marker(
markerId: const MarkerId('invalid'),
position: const LatLng(
0, 0)), // Provide a default in case it is not found
);
if (previousMarker.markerId.value != 'invalid') {
widget.polylines.add(
Polyline(
polylineId: PolylineId('polyline_$_counter'),
color: theme.currentColorScheme.primary,
width: 2,
points: [previousMarker.position, position],
),
);
} else {
logger.w('Previous marker not found');
}
}
_counter++;
});
}
}
void _onTapMarker(String markerId) {
logger.d(markerId);
if (markerId == 'polygon_0') {
logger.d('first pressed'); logger.d('first pressed');
} }
} }