@@ -8,6 +8,7 @@ import 'package:latlong/latlong.dart';
88
99class 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
8692class 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