getClip method

  1. @override
Path getClip(
  1. Size size
)
override

Returns a description of the clip given that the render object being clipped is of the given size.

Implementation

@override
Path getClip(Size size) {
  Path path = Path();
  String d = '';

  double minLats = polygon.points.map((LatLng e) => e.latitude).toList().min;
  double minLngs = polygon.points.map((LatLng e) => e.longitude).toList().min;
  double maxLats = polygon.points.map((LatLng e) => e.latitude).toList().max;
  double maxLngs = polygon.points.map((LatLng e) => e.longitude).toList().max;

  polygon.points.forEachIndexed((int i, LatLng e) {
    double lng = (e.longitude - minLngs) / (maxLngs - minLngs) * size.width;
    double lat = (maxLats - e.latitude) / (maxLats - minLats) * size.height;
    if (i == 0) {
      path.moveTo(lng, lat);
      d += 'M$lng $lat';
    } else {
      path.lineTo(lng, lat);
      d += 'L$lng $lat';
    }
  });
  d += 'Z';
  for (List<LatLng> hole in (polygon.holePointsList ?? <List<LatLng>>[])) {
    hole.forEachIndexed((int i, LatLng e) {
      double lng = (e.longitude - minLngs) / (maxLngs - minLngs) * size.width;
      double lat = (maxLats - e.latitude) / (maxLats - minLats) * size.height;
      if (i == 0) {
        path.moveTo(lng, lat);
        d += 'M$lng $lat';
      } else {
        path.lineTo(lng, lat);
        d += 'L$lng $lat';
      }
    });
    d += 'Z';
  }
  path.close();
  log(
    '<svg version="1.2" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 ${size.width.ceil()} ${size.height.ceil()}" width="50%"><path  fill-rule="evenodd" fill="brown" d="$d" /></svg>',
  );
  return path;
}