From b30db04fe28be5536bc2132629561f6b4fba92d6 Mon Sep 17 00:00:00 2001 From: Jens Reinemuth Date: Mon, 6 Jan 2025 12:55:24 +0100 Subject: [PATCH] Stand 2025-01-06 --- lib/src/widgets/map/map_widget.dart | 193 +++++++++++++++------------- 1 file changed, 105 insertions(+), 88 deletions(-) diff --git a/lib/src/widgets/map/map_widget.dart b/lib/src/widgets/map/map_widget.dart index 45a6a9f..033c27b 100644 --- a/lib/src/widgets/map/map_widget.dart +++ b/lib/src/widgets/map/map_widget.dart @@ -7,40 +7,48 @@ import '../../consts/consts.dart'; import '../../packages/ambito_theme/ambito_theme.dart'; class MapWidget extends StatefulWidget { - const MapWidget( - {super.key, - required this.markers, - required this.polygons, - required this.polylines}); + const MapWidget({ + super.key, + required this.initialPosition, // Make initial position required + this.markers = const {}, // Provide default empty sets + this.polygons = const {}, + this.polylines = const {}, + }); + final LatLng initialPosition; // Use LatLng directly final Set markers; final Set polygons; final Set polylines; @override - State createState() => MapWidgetState(); + State createState() => _MapWidgetState(); // Use shorter syntax } -class MapWidgetState extends State { - final Completer _controller = - Completer(); - +class _MapWidgetState extends State { + final Completer _controller = Completer(); bool _drawPolygon = false; int _counter = 0; - static CameraPosition _initialPos = const CameraPosition( - target: LatLng(0, 0), - zoom: 14.4746, - ); + late CameraPosition _initialCameraPosition; // Use late for initialization @override void initState() { - setState(() { - _initialPos = - CameraPosition(target: widget.markers.first.position, zoom: 14); - }); - 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 @@ -49,79 +57,88 @@ class MapWidgetState extends State { return ClipRRect( borderRadius: BorderRadius.circular(10.0), child: SizedBox( - width: double.infinity, - child: Column( - children: [ - Expanded( - child: GoogleMap( - onTap: (position) { - 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, - mapType: MapType.hybrid, - markers: widget.markers, - polygons: widget.polygons, - polylines: widget.polylines, - initialCameraPosition: _initialPos, - onMapCreated: (GoogleMapController controller) { - _controller.complete(controller); - }, + width: double.infinity, + child: Column( + children: [ + Expanded( + child: GoogleMap( + onTap: _handleMapTap, // Extract onTap logic + mapToolbarEnabled: true, + mapType: MapType.hybrid, + markers: widget.markers, + polygons: widget.polygons, + polylines: widget.polylines, + initialCameraPosition: _initialCameraPosition, + onMapCreated: (GoogleMapController controller) { + _controller.complete(controller); + }, + ), + ), + Row( + children: [ + const Spacer(), + IconButton( + onPressed: () => setState( + () => _drawPolygon = !_drawPolygon), // Shorter syntax + icon: Icon( + Icons.polyline_outlined, + color: _drawPolygon + ? theme.currentColorScheme.primary + : theme.currentColorScheme.outline, + ), ), - ), - Row( - children: [ - const Spacer(), - IconButton( - onPressed: () { - setState(() { - _drawPolygon = !_drawPolygon; - }); - }, - icon: Icon( - Icons.polyline_outlined, - color: (_drawPolygon - ? theme.currentColorScheme.primary - : theme.currentColorScheme.outline), - )) - ], - ), - ], - )), + ], + ), + ], + ), + ), ); } - onTapMarker(String counter) { - logger.d(counter); - if (counter == 'polygon_0') { + void _handleMapTap(LatLng position) { + final AmbitoTheme theme = getTheme(context); + 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'); } }