Stand 2025-01-06
This commit is contained in:
parent
16f1442564
commit
b30db04fe2
1 changed files with 105 additions and 88 deletions
|
@ -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
|
||||||
|
@ -49,79 +57,88 @@ class MapWidgetState extends State<MapWidget> {
|
||||||
return ClipRRect(
|
return ClipRRect(
|
||||||
borderRadius: BorderRadius.circular(10.0),
|
borderRadius: BorderRadius.circular(10.0),
|
||||||
child: SizedBox(
|
child: SizedBox(
|
||||||
width: double.infinity,
|
width: double.infinity,
|
||||||
child: Column(
|
child: Column(
|
||||||
children: [
|
children: [
|
||||||
Expanded(
|
Expanded(
|
||||||
child: GoogleMap(
|
child: GoogleMap(
|
||||||
onTap: (position) {
|
onTap: _handleMapTap, // Extract onTap logic
|
||||||
if (_drawPolygon == true) {
|
mapToolbarEnabled: true,
|
||||||
setState(() {
|
mapType: MapType.hybrid,
|
||||||
widget.markers.add(
|
markers: widget.markers,
|
||||||
Marker(
|
polygons: widget.polygons,
|
||||||
onTap: onTapMarker('polygon_$_counter'),
|
polylines: widget.polylines,
|
||||||
markerId: MarkerId('polygon_$_counter'),
|
initialCameraPosition: _initialCameraPosition,
|
||||||
position: position,
|
onMapCreated: (GoogleMapController controller) {
|
||||||
),
|
_controller.complete(controller);
|
||||||
);
|
},
|
||||||
if (_counter > 0) {
|
),
|
||||||
logger.i('add polyline');
|
),
|
||||||
widget.polylines.add(
|
Row(
|
||||||
Polyline(
|
children: [
|
||||||
polylineId: PolylineId('polyline_$_counter'),
|
const Spacer(),
|
||||||
color: theme.currentColorScheme.primary,
|
IconButton(
|
||||||
width: 2,
|
onPressed: () => setState(
|
||||||
points: [
|
() => _drawPolygon = !_drawPolygon), // Shorter syntax
|
||||||
widget.markers
|
icon: Icon(
|
||||||
.where((el) =>
|
Icons.polyline_outlined,
|
||||||
el.markerId ==
|
color: _drawPolygon
|
||||||
MarkerId('polygon_${_counter - 1}'))
|
? theme.currentColorScheme.primary
|
||||||
.first
|
: theme.currentColorScheme.outline,
|
||||||
.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);
|
|
||||||
},
|
|
||||||
),
|
),
|
||||||
),
|
],
|
||||||
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) {
|
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');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue