@@ -19,13 +19,17 @@ class Polyline {
1919 final Color color;
2020 final double borderStrokeWidth;
2121 final Color borderColor;
22+ final List <Color > gradientColors;
23+ final List <double > colorsStop;
2224 final bool isDotted;
2325 Polyline ({
2426 this .points,
2527 this .strokeWidth = 1.0 ,
2628 this .color = const Color (0xFF00FF00 ),
2729 this .borderStrokeWidth = 0.0 ,
2830 this .borderColor = const Color (0xFFFFFF00 ),
31+ this .gradientColors,
32+ this .colorsStop,
2933 this .isDotted = false ,
3034 });
3135}
@@ -85,6 +89,7 @@ class PolylineLayer extends StatelessWidget {
8589
8690class PolylinePainter extends CustomPainter {
8791 final Polyline polylineOpt;
92+
8893 PolylinePainter (this .polylineOpt);
8994
9095 @override
@@ -95,12 +100,17 @@ class PolylinePainter extends CustomPainter {
95100 final rect = Offset .zero & size;
96101 canvas.clipRect (rect);
97102 final paint = Paint ()
98- ..color = polylineOpt.color
99103 ..strokeWidth = polylineOpt.strokeWidth
100104 ..strokeCap = StrokeCap .round
101105 ..strokeJoin = StrokeJoin .round
102106 ..blendMode = BlendMode .src;
103107
108+ if (polylineOpt.gradientColors == null ) {
109+ paint.color = polylineOpt.color;
110+ } else {
111+ paint.shader = _paintGradient ();
112+ }
113+
104114 final filterPaint = Paint ()
105115 ..color = polylineOpt.borderColor.withAlpha (255 )
106116 ..strokeWidth = polylineOpt.strokeWidth
@@ -177,6 +187,23 @@ class PolylinePainter extends CustomPainter {
177187 }
178188 }
179189
190+ ui.Gradient _paintGradient () => ui.Gradient .linear (polylineOpt.offsets.first,
191+ polylineOpt.offsets.last, polylineOpt.gradientColors, _getColorsStop ());
192+
193+ List <double > _getColorsStop () => (polylineOpt.colorsStop != null &&
194+ polylineOpt.colorsStop.length == polylineOpt.gradientColors.length)
195+ ? polylineOpt.colorsStop
196+ : _calculateColorsStop ();
197+
198+ List <double > _calculateColorsStop () {
199+ final colorsStopInterval = 1.0 / polylineOpt.gradientColors.length;
200+ return polylineOpt.gradientColors
201+ .map ((gradientColor) =>
202+ polylineOpt.gradientColors.indexOf (gradientColor) *
203+ colorsStopInterval)
204+ .toList ();
205+ }
206+
180207 @override
181208 bool shouldRepaint (PolylinePainter other) => false ;
182209}
0 commit comments