Skip to content

Commit

Permalink
feat: add dottedSpacingFactor to customize dotted polyline spacing (#…
Browse files Browse the repository at this point in the history
…1845)

* add dottedSpacingFactor to Polyline for customizable dotted polyline spacing

* change name of dottedSpacingFactor to segmentSpacingFactor

* update polyline example page

* Minor documentation improvement
Minor formatting improvement

* Added `Polyline.segmentSpacingFactor` to `Polyline.==` and `hashCode`

---------

Co-authored-by: JaffaKetchup <[email protected]>
  • Loading branch information
gnassro and JaffaKetchup authored Mar 12, 2024
1 parent b0590fb commit a36c59a
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
18 changes: 18 additions & 0 deletions example/lib/pages/polyline.dart
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,24 @@ class _PolylinePageState extends State<PolylinePage> {
subtitle: 'Solid translucent color fill, with different color outline',
),
),
Polyline(
points: const [
LatLng(43.864797, 11.7112939),
LatLng(36.7948545, 10.2256785),
LatLng(35.566530, 5.584283),
],
strokeWidth: 10,
color: Colors.blueAccent,
isDotted: true,
segmentSpacingFactor: 3,
borderStrokeWidth: 8,
borderColor: Colors.blue.withOpacity(0.5),
hitValue: (
title: 'Blue Dotted Line with Custom Spacing',
subtitle:
'Dotted line with segment spacing controlled by `segmentSpacingFactor`',
),
),
];
late final _polylines =
Map.fromEntries(_polylinesRaw.map((e) => MapEntry(e.hitValue, e)));
Expand Down
2 changes: 1 addition & 1 deletion lib/src/layer/polyline_layer/painter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ class _PolylinePainter<R extends Object> extends CustomPainter {
final borderRadius = (borderPaint?.strokeWidth ?? 0) / 2;

if (isDotted) {
final spacing = strokeWidth * 1.5;
final spacing = strokeWidth * polyline.segmentSpacingFactor;
if (borderPaint != null && filterPaint != null) {
_paintDottedLine(borderPath, offsets, borderRadius, spacing);
_paintDottedLine(filterPath, offsets, radius, spacing);
Expand Down
12 changes: 12 additions & 0 deletions lib/src/layer/polyline_layer/polyline.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,15 @@ class Polyline<R extends Object> {
/// The width of the stroke
final double strokeWidth;

/// The multiplier used to calculate the spacing between segments in a dotted/
/// dashed polyline
///
/// A value of 1.0 will result in spacing equal to the `strokeWidth`.
/// Increasing the value increases the spacing with respect to `strokeWidth`.
///
/// Defaults to 1.5.
final double segmentSpacingFactor;

/// The color of the line stroke.
final Color color;

Expand Down Expand Up @@ -58,6 +67,7 @@ class Polyline<R extends Object> {
this.strokeJoin = StrokeJoin.round,
this.useStrokeWidthInMeter = false,
this.hitValue,
this.segmentSpacingFactor = 1.5,
});

@override
Expand All @@ -69,6 +79,7 @@ class Polyline<R extends Object> {
borderStrokeWidth == other.borderStrokeWidth &&
borderColor == other.borderColor &&
isDotted == other.isDotted &&
segmentSpacingFactor == other.segmentSpacingFactor &&
strokeCap == other.strokeCap &&
strokeJoin == other.strokeJoin &&
useStrokeWidthInMeter == other.useStrokeWidthInMeter &&
Expand All @@ -90,6 +101,7 @@ class Polyline<R extends Object> {
gradientColors,
colorsStop,
isDotted,
segmentSpacingFactor,
strokeCap,
strokeJoin,
useStrokeWidthInMeter,
Expand Down

0 comments on commit a36c59a

Please sign in to comment.