Skip to content
Snippets Groups Projects
Commit 2d6d1f8e authored by Sara Savanovic Djordjevic's avatar Sara Savanovic Djordjevic
Browse files

update: InteractivePolygon

parent 056e99b2
No related branches found
No related tags found
1 merge request!3Choropleth map implementation
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import '../marker_handler/marker_data.dart'; import '../marker_handler/marker_data.dart';
import 'package:latlong2/latlong.dart' as latLng;
// InteractivePolygon returns a Polygon object wrapped in a GestureDetector // InteractivePolygon returns a Polygon object wrapped in a CustomPaint,
// in order to make the Polygon clickable // which is then wrapped in a GestureDetector. This is in order to make the Polygon clickable
class InteractivePolygon extends StatelessWidget { class InteractivePolygon extends StatelessWidget {
final List<Offset> points; final List<latLng.LatLng> points;
final void Function(Measurement)? onTap; final void Function(Measurement)? onTap;
final Color color; final Color color;
final double strokeWidth; final double strokeWidth;
...@@ -24,7 +25,8 @@ class InteractivePolygon extends StatelessWidget { ...@@ -24,7 +25,8 @@ class InteractivePolygon extends StatelessWidget {
child: CustomPaint( child: CustomPaint(
painter: PolygonPainter( painter: PolygonPainter(
points: points, points: points,
color: color, bodyColor: color,
borderColor: Colors.deepOrange,
strokeWidth: strokeWidth, strokeWidth: strokeWidth,
), ),
), ),
...@@ -35,39 +37,81 @@ class InteractivePolygon extends StatelessWidget { ...@@ -35,39 +37,81 @@ class InteractivePolygon extends StatelessWidget {
// PolygonPainter takes the points, color, and stroke width from a // PolygonPainter takes the points, color, and stroke width from a
// object of type InteractivePolygon, and renders it // object of type InteractivePolygon, and renders it
class PolygonPainter extends CustomPainter { class PolygonPainter extends CustomPainter {
final List<Offset> points; final List<latLng.LatLng> points;
final Color color; final Color bodyColor;
final Color borderColor;
final double strokeWidth; final double strokeWidth;
PolygonPainter({ PolygonPainter({
required this.points, required this.points,
required this.color, required this.bodyColor,
required this.strokeWidth, required this.strokeWidth,
required this.borderColor,
}); });
// paint() iterates through all the vertices of the polygon and connects // paint() iterates through all the vertices of the polygon and connects
// each sequential pair with a line // each sequential pair with a line
@override @override
void paint(Canvas canvas, Size size) { void paint(Canvas canvas, Size size) {
final paint = Paint()
final Paint body = Paint();
body
..color = bodyColor
..style = PaintingStyle.fill
..strokeWidth = 0;
final Paint border = Paint();
border
..color = borderColor
..style = PaintingStyle.stroke
..strokeCap = StrokeCap.round
..strokeWidth = 1.0;
final path = Path();
if (points.isNotEmpty) {
final firstPoint = points.first;
path.moveTo(firstPoint.latitude, firstPoint.longitude);
for (var i = 1; i < points.length; i++) {
final point = points[i];
path.lineTo(point.latitude, point.longitude);
}
path.close();
canvas.drawPath(path, body); // Render body
canvas.drawPath(path, border); // Render border
}
else {
print("Error in rendering polygon. No points provided");
}
/*final paint = Paint()
..color = color ..color = color
..style = PaintingStyle.stroke ..style = PaintingStyle.stroke
..strokeWidth = strokeWidth; ..strokeWidth = strokeWidth;
final path = Path(); final path = Path();
path.moveTo(points[0].dx, points[0].dy); if (points.isNotEmpty) {
for (var i = 1; i < points.length; i++) { final firstPoint = points.first;
path.lineTo(points[i].dx, points[i].dy); // Render line between vertices path.moveTo(firstPoint.latitude, firstPoint.longitude);
}
path.close();
canvas.drawPath(path, paint); for (var i = 1; i < points.length; i++) {
final point = points[i];
path.lineTo(point.latitude, point.longitude);
}
path.close();
canvas.drawPath(path, paint);
}
else {
print("Error in rendering polygon. No points provided");
}*/
} }
@override @override
bool shouldRepaint(PolygonPainter oldDelegate) { bool shouldRepaint(PolygonPainter oldDelegate) {
return oldDelegate.points != points || return oldDelegate.points != points ||
oldDelegate.color != color || oldDelegate.bodyColor != bodyColor ||
oldDelegate.strokeWidth != strokeWidth; oldDelegate.strokeWidth != strokeWidth;
} }
} }
\ No newline at end of file
...@@ -6,6 +6,7 @@ import 'stat_charts.dart'; ...@@ -6,6 +6,7 @@ import 'stat_charts.dart';
import 'sat_layer.dart'; import 'sat_layer.dart';
import 'package:flutter_map/flutter_map.dart'; import 'package:flutter_map/flutter_map.dart';
import 'interactive_polygon.dart'; import 'interactive_polygon.dart';
import 'package:latlong2/latlong.dart' as latLng;
/// MapContainerWidget is the main widget that contains the map with all /// MapContainerWidget is the main widget that contains the map with all
...@@ -69,21 +70,9 @@ class _MapContainerWidgetState extends State<MapContainerWidget> { ...@@ -69,21 +70,9 @@ class _MapContainerWidgetState extends State<MapContainerWidget> {
urlTemplate: "https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png", urlTemplate: "https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png",
subdomains: const ['a', 'b', 'c'], subdomains: const ['a', 'b', 'c'],
), ),
/*PolygonLayer(
polygons: widget.markerList.map((Measurement measurement) {
return Polygon(
points: points, // Use list of corner coordinates to render polygon
color: Colors.blue.withOpacity(0.5),
isFilled: true,
);
}).toList(),
),*/
// Custom polygon class for clickable polygons // Custom polygon class for clickable polygons
InteractivePolygon( InteractivePolygon(
// Map corner coordinates for each measurement to Offset object points: widget.markerList.expand((measurement) => measurement.corners).toList(),
points: widget.markerList.expand((measurement) => measurement.corners.map((corner) =>
Offset(corner.latitude, corner.longitude))).toList(),
onTap: (Measurement measurement) { // Pass current measurement to onTap onTap: (Measurement measurement) { // Pass current measurement to onTap
setState(() { setState(() {
selectedMarker = measurement; // Set selectedMarker to clicked measurement selectedMarker = measurement; // Set selectedMarker to clicked measurement
...@@ -103,10 +92,10 @@ class _MapContainerWidgetState extends State<MapContainerWidget> { ...@@ -103,10 +92,10 @@ class _MapContainerWidgetState extends State<MapContainerWidget> {
selectedMarker = measurement; selectedMarker = measurement;
}); });
}, },
child: const Icon( child: Icon(
Icons.severe_cold, Icons.severe_cold,
color: Colors.blue, color: measurement == selectedMarker ? Colors.green : Colors.blue,
size: 30.0, size: measurement == selectedMarker ? 40.0 : 30.0,
), ),
), ),
); );
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment