2024-12-07 20:53:13 +01:00
|
|
|
import 'dart:async';
|
|
|
|
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:google_maps_flutter/google_maps_flutter.dart';
|
|
|
|
|
2024-12-13 09:41:26 +01:00
|
|
|
import '../../consts/consts.dart';
|
2024-12-09 14:04:43 +01:00
|
|
|
import '../../packages/ambito_theme/ambito_theme.dart';
|
|
|
|
|
2024-12-07 20:53:13 +01:00
|
|
|
class MapWidget extends StatefulWidget {
|
2024-12-13 09:41:26 +01:00
|
|
|
const MapWidget(
|
|
|
|
{super.key,
|
|
|
|
required this.markers,
|
|
|
|
required this.polygons,
|
|
|
|
required this.polylines});
|
2024-12-07 20:53:13 +01:00
|
|
|
|
2024-12-13 09:41:26 +01:00
|
|
|
final Set<Marker> markers;
|
|
|
|
final Set<Polygon> polygons;
|
|
|
|
final Set<Polyline> polylines;
|
2024-12-07 20:53:13 +01:00
|
|
|
|
|
|
|
@override
|
|
|
|
State<StatefulWidget> createState() => MapWidgetState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class MapWidgetState extends State<MapWidget> {
|
|
|
|
final Completer<GoogleMapController> _controller =
|
|
|
|
Completer<GoogleMapController>();
|
|
|
|
|
2024-12-09 14:04:43 +01:00
|
|
|
bool _drawPolygon = false;
|
|
|
|
int _counter = 0;
|
2024-12-09 12:17:12 +01:00
|
|
|
|
2024-12-07 20:53:13 +01:00
|
|
|
static CameraPosition _initialPos = const CameraPosition(
|
|
|
|
target: LatLng(0, 0),
|
|
|
|
zoom: 14.4746,
|
|
|
|
);
|
|
|
|
|
|
|
|
@override
|
|
|
|
void initState() {
|
2024-12-13 09:41:26 +01:00
|
|
|
setState(() {
|
|
|
|
_initialPos =
|
|
|
|
CameraPosition(target: widget.markers.first.position, zoom: 14);
|
|
|
|
});
|
|
|
|
|
2024-12-07 20:53:13 +01:00
|
|
|
super.initState();
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2024-12-09 14:04:43 +01:00
|
|
|
final AmbitoTheme theme = getTheme(context);
|
2024-12-07 20:53:13 +01:00
|
|
|
return ClipRRect(
|
|
|
|
borderRadius: BorderRadius.circular(10.0),
|
|
|
|
child: SizedBox(
|
2024-12-09 14:04:43 +01:00
|
|
|
width: double.infinity,
|
|
|
|
child: Column(
|
|
|
|
children: [
|
|
|
|
Row(
|
|
|
|
children: [
|
2024-12-13 09:41:26 +01:00
|
|
|
const Spacer(),
|
2024-12-09 14:04:43 +01:00
|
|
|
IconButton(
|
|
|
|
onPressed: () {
|
|
|
|
setState(() {
|
|
|
|
_drawPolygon = !_drawPolygon;
|
|
|
|
});
|
|
|
|
},
|
|
|
|
icon: Icon(
|
|
|
|
Icons.polyline_outlined,
|
|
|
|
color: (_drawPolygon
|
|
|
|
? theme.currentColorScheme.primary
|
|
|
|
: theme.currentColorScheme.outline),
|
|
|
|
))
|
|
|
|
],
|
|
|
|
),
|
|
|
|
Expanded(
|
|
|
|
child: GoogleMap(
|
|
|
|
onTap: (position) {
|
|
|
|
if (_drawPolygon == true) {
|
|
|
|
setState(() {
|
2024-12-13 09:41:26 +01:00
|
|
|
widget.markers.add(
|
|
|
|
Marker(
|
|
|
|
onTap: onTapMarker('polygon_$_counter'),
|
2024-12-09 14:04:43 +01:00
|
|
|
markerId: MarkerId('polygon_$_counter'),
|
2024-12-13 09:41:26 +01:00
|
|
|
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
|
|
|
|
]),
|
|
|
|
);
|
|
|
|
}
|
2024-12-09 14:04:43 +01:00
|
|
|
_counter++;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
mapToolbarEnabled: true,
|
|
|
|
mapType: MapType.hybrid,
|
2024-12-13 09:41:26 +01:00
|
|
|
markers: widget.markers,
|
|
|
|
polygons: widget.polygons,
|
|
|
|
polylines: widget.polylines,
|
2024-12-09 14:04:43 +01:00
|
|
|
initialCameraPosition: _initialPos,
|
|
|
|
onMapCreated: (GoogleMapController controller) {
|
|
|
|
_controller.complete(controller);
|
|
|
|
},
|
|
|
|
),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
)),
|
2024-12-07 20:53:13 +01:00
|
|
|
);
|
|
|
|
}
|
2024-12-13 09:41:26 +01:00
|
|
|
|
|
|
|
onTapMarker(String counter) {
|
|
|
|
logger.d(counter);
|
|
|
|
if (counter == 'polygon_0') {
|
|
|
|
logger.d('first pressed');
|
|
|
|
}
|
|
|
|
}
|
2024-12-07 20:53:13 +01:00
|
|
|
}
|