Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

refacto: added saveLayers parameter #1219

Merged
merged 2 commits into from
Apr 19, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 26 additions & 10 deletions lib/src/layer/polyline_layer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,27 @@ import 'package:flutter_map/src/map/map.dart';
import 'package:latlong2/latlong.dart';

class PolylineLayerOptions extends LayerOptions {
/// List of polylines to draw.
final List<Polyline> polylines;

final bool polylineCulling;

/// {@macro newPolylinePainter.saveLayers}
///
/// By default, this value is set to `false` to improve performance on
/// layers containing a lot of polylines.
///
/// You might want to set this to `true` if you get unwanted darker lines
/// where they overlap but, keep in mind that this might reduce the
/// performance of the layer.
final bool saveLayers;

PolylineLayerOptions({
Key? key,
this.polylines = const [],
this.polylineCulling = false,
Stream<Null>? rebuild,
this.saveLayers = false,
}) : super(key: key, rebuild: rebuild) {
if (polylineCulling) {
for (var polyline in polylines) {
Expand Down Expand Up @@ -100,7 +113,7 @@ class PolylineLayer extends StatelessWidget {
_fillOffsets(polylineOpt.offsets, polylineOpt.points);

polylines.add(CustomPaint(
painter: PolylinePainter(polylineOpt),
painter: PolylinePainter(polylineOpt, polylineOpts.saveLayers),
size: size,
));
}
Expand Down Expand Up @@ -132,7 +145,13 @@ class PolylineLayer extends StatelessWidget {
class PolylinePainter extends CustomPainter {
final Polyline polylineOpt;

PolylinePainter(this.polylineOpt);
/// {@template newPolylinePainter.saveLayers}
/// If `true`, the canvas will be updated on every frame by calling the
/// methods [Canvas.saveLayer] and [Canvas.restore].
/// {@endtemplate}
final bool saveLayers;

PolylinePainter(this.polylineOpt, this.saveLayers);

@override
void paint(Canvas canvas, Size size) {
Expand Down Expand Up @@ -178,26 +197,26 @@ class PolylinePainter extends CustomPainter {
var borderRadius = (borderPaint?.strokeWidth ?? 0) / 2;
if (polylineOpt.isDotted) {
var spacing = polylineOpt.strokeWidth * 1.5;
canvas.saveLayer(rect, Paint());
if (saveLayers) canvas.saveLayer(rect, Paint());
if (borderPaint != null && filterPaint != null) {
_paintDottedLine(
canvas, polylineOpt.offsets, borderRadius, spacing, borderPaint);
_paintDottedLine(
canvas, polylineOpt.offsets, radius, spacing, filterPaint);
}
_paintDottedLine(canvas, polylineOpt.offsets, radius, spacing, paint);
canvas.restore();
if (saveLayers) canvas.restore();
} else {
paint.style = PaintingStyle.stroke;
canvas.saveLayer(rect, Paint());
if (saveLayers) canvas.saveLayer(rect, Paint());
if (borderPaint != null && filterPaint != null) {
borderPaint.style = PaintingStyle.stroke;
_paintLine(canvas, polylineOpt.offsets, borderPaint);
filterPaint.style = PaintingStyle.stroke;
_paintLine(canvas, polylineOpt.offsets, filterPaint);
}
_paintLine(canvas, polylineOpt.offsets, paint);
canvas.restore();
if (saveLayers) canvas.restore();
}
}

Expand Down Expand Up @@ -228,10 +247,7 @@ class PolylinePainter extends CustomPainter {

void _paintLine(Canvas canvas, List<Offset> offsets, Paint paint) {
if (offsets.isNotEmpty) {
final path = ui.Path()..moveTo(offsets[0].dx, offsets[0].dy);
for (var offset in offsets) {
path.lineTo(offset.dx, offset.dy);
}
final path = ui.Path()..addPolygon(offsets, false);
canvas.drawPath(path, paint);
}
}
Expand Down