Skip to content

Commit f2377e2

Browse files
SebWojdjohnpryan
authored andcommitted
polyline with gradient (#452)
* add polyline with gradient * resolve gradientColors empty case * code formatting * code foramtting
1 parent ba91892 commit f2377e2

File tree

2 files changed

+53
-2
lines changed

2 files changed

+53
-2
lines changed

example/lib/pages/polyline.dart

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@ class PolylinePage extends StatelessWidget {
1414
LatLng(53.3498, -6.2603),
1515
LatLng(48.8566, 2.3522),
1616
];
17+
18+
var pointsGradient = <LatLng>[
19+
LatLng(55.5, -0.09),
20+
LatLng(54.3498, -6.2603),
21+
LatLng(52.8566, 2.3522),
22+
];
23+
1724
return Scaffold(
1825
appBar: AppBar(title: Text('Polylines')),
1926
drawer: buildDrawer(context, PolylinePage.route),
@@ -43,7 +50,20 @@ class PolylinePage extends StatelessWidget {
4350
strokeWidth: 4.0,
4451
color: Colors.purple),
4552
],
46-
)
53+
),
54+
PolylineLayerOptions(
55+
polylines: [
56+
Polyline(
57+
points: pointsGradient,
58+
strokeWidth: 4.0,
59+
gradientColors: [
60+
Color(0xffE40203),
61+
Color(0xffFEED00),
62+
Color(0xff007E2D),
63+
],
64+
),
65+
],
66+
),
4767
],
4868
),
4969
),

lib/src/layer/polyline_layer.dart

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import 'package:latlong/latlong.dart';
88

99
class PolylineLayerOptions extends LayerOptions {
1010
final List<Polyline> polylines;
11+
1112
PolylineLayerOptions({this.polylines = const [], rebuild})
1213
: super(rebuild: rebuild);
1314
}
@@ -19,13 +20,18 @@ class Polyline {
1920
final Color color;
2021
final double borderStrokeWidth;
2122
final Color borderColor;
23+
final List<Color> gradientColors;
24+
final List<double> colorsStop;
2225
final bool isDotted;
26+
2327
Polyline({
2428
this.points,
2529
this.strokeWidth = 1.0,
2630
this.color = const Color(0xFF00FF00),
2731
this.borderStrokeWidth = 0.0,
2832
this.borderColor = const Color(0xFFFFFF00),
33+
this.gradientColors,
34+
this.colorsStop,
2935
this.isDotted = false,
3036
});
3137
}
@@ -85,6 +91,7 @@ class PolylineLayer extends StatelessWidget {
8591

8692
class PolylinePainter extends CustomPainter {
8793
final Polyline polylineOpt;
94+
8895
PolylinePainter(this.polylineOpt);
8996

9097
@override
@@ -95,12 +102,19 @@ class PolylinePainter extends CustomPainter {
95102
final rect = Offset.zero & size;
96103
canvas.clipRect(rect);
97104
final paint = Paint()
98-
..color = polylineOpt.color
99105
..strokeWidth = polylineOpt.strokeWidth
100106
..strokeCap = StrokeCap.round
101107
..strokeJoin = StrokeJoin.round
102108
..blendMode = BlendMode.src;
103109

110+
if (polylineOpt.gradientColors == null) {
111+
paint.color = polylineOpt.color;
112+
} else {
113+
polylineOpt.gradientColors.isNotEmpty
114+
? paint.shader = _paintGradient()
115+
: paint.color = polylineOpt.color;
116+
}
117+
104118
final filterPaint = Paint()
105119
..color = polylineOpt.borderColor.withAlpha(255)
106120
..strokeWidth = polylineOpt.strokeWidth
@@ -177,6 +191,23 @@ class PolylinePainter extends CustomPainter {
177191
}
178192
}
179193

194+
ui.Gradient _paintGradient() => ui.Gradient.linear(polylineOpt.offsets.first,
195+
polylineOpt.offsets.last, polylineOpt.gradientColors, _getColorsStop());
196+
197+
List<double> _getColorsStop() => (polylineOpt.colorsStop != null &&
198+
polylineOpt.colorsStop.length == polylineOpt.gradientColors.length)
199+
? polylineOpt.colorsStop
200+
: _calculateColorsStop();
201+
202+
List<double> _calculateColorsStop() {
203+
final colorsStopInterval = 1.0 / polylineOpt.gradientColors.length;
204+
return polylineOpt.gradientColors
205+
.map((gradientColor) =>
206+
polylineOpt.gradientColors.indexOf(gradientColor) *
207+
colorsStopInterval)
208+
.toList();
209+
}
210+
180211
@override
181212
bool shouldRepaint(PolylinePainter other) => false;
182213
}

0 commit comments

Comments
 (0)