diff --git a/example/lib/pages/polyline.dart b/example/lib/pages/polyline.dart index 97bb2f06c..8fd1ee355 100644 --- a/example/lib/pages/polyline.dart +++ b/example/lib/pages/polyline.dart @@ -14,6 +14,13 @@ class PolylinePage extends StatelessWidget { LatLng(53.3498, -6.2603), LatLng(48.8566, 2.3522), ]; + + var pointsGradient = [ + LatLng(55.5, -0.09), + LatLng(54.3498, -6.2603), + LatLng(52.8566, 2.3522), + ]; + return Scaffold( appBar: AppBar(title: Text('Polylines')), drawer: buildDrawer(context, PolylinePage.route), @@ -43,7 +50,20 @@ class PolylinePage extends StatelessWidget { strokeWidth: 4.0, color: Colors.purple), ], - ) + ), + PolylineLayerOptions( + polylines: [ + Polyline( + points: pointsGradient, + strokeWidth: 4.0, + gradientColors: [ + Color(0xffE40203), + Color(0xffFEED00), + Color(0xff007E2D), + ], + ), + ], + ), ], ), ), diff --git a/lib/src/layer/polyline_layer.dart b/lib/src/layer/polyline_layer.dart index d92699205..aca41c77a 100644 --- a/lib/src/layer/polyline_layer.dart +++ b/lib/src/layer/polyline_layer.dart @@ -8,6 +8,7 @@ import 'package:latlong/latlong.dart'; class PolylineLayerOptions extends LayerOptions { final List polylines; + PolylineLayerOptions({this.polylines = const [], rebuild}) : super(rebuild: rebuild); } @@ -19,13 +20,18 @@ class Polyline { final Color color; final double borderStrokeWidth; final Color borderColor; + final List gradientColors; + final List colorsStop; final bool isDotted; + Polyline({ this.points, this.strokeWidth = 1.0, this.color = const Color(0xFF00FF00), this.borderStrokeWidth = 0.0, this.borderColor = const Color(0xFFFFFF00), + this.gradientColors, + this.colorsStop, this.isDotted = false, }); } @@ -85,6 +91,7 @@ class PolylineLayer extends StatelessWidget { class PolylinePainter extends CustomPainter { final Polyline polylineOpt; + PolylinePainter(this.polylineOpt); @override @@ -95,12 +102,19 @@ class PolylinePainter extends CustomPainter { final rect = Offset.zero & size; canvas.clipRect(rect); final paint = Paint() - ..color = polylineOpt.color ..strokeWidth = polylineOpt.strokeWidth ..strokeCap = StrokeCap.round ..strokeJoin = StrokeJoin.round ..blendMode = BlendMode.src; + if (polylineOpt.gradientColors == null) { + paint.color = polylineOpt.color; + } else { + polylineOpt.gradientColors.isNotEmpty + ? paint.shader = _paintGradient() + : paint.color = polylineOpt.color; + } + final filterPaint = Paint() ..color = polylineOpt.borderColor.withAlpha(255) ..strokeWidth = polylineOpt.strokeWidth @@ -177,6 +191,23 @@ class PolylinePainter extends CustomPainter { } } + ui.Gradient _paintGradient() => ui.Gradient.linear(polylineOpt.offsets.first, + polylineOpt.offsets.last, polylineOpt.gradientColors, _getColorsStop()); + + List _getColorsStop() => (polylineOpt.colorsStop != null && + polylineOpt.colorsStop.length == polylineOpt.gradientColors.length) + ? polylineOpt.colorsStop + : _calculateColorsStop(); + + List _calculateColorsStop() { + final colorsStopInterval = 1.0 / polylineOpt.gradientColors.length; + return polylineOpt.gradientColors + .map((gradientColor) => + polylineOpt.gradientColors.indexOf(gradientColor) * + colorsStopInterval) + .toList(); + } + @override bool shouldRepaint(PolylinePainter other) => false; }