From b54df05569c3a48ad6341d4844e323aa9b7fce55 Mon Sep 17 00:00:00 2001 From: Stephen Vaudreuil <55300279+PvtPuddles@users.noreply.github.com> Date: Fri, 15 May 2026 09:15:23 -0400 Subject: [PATCH] Improved performance of `LineChartPainter.getNearestTouchedSpot` --- .../chart/line_chart/line_chart_painter.dart | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/lib/src/chart/line_chart/line_chart_painter.dart b/lib/src/chart/line_chart/line_chart_painter.dart index 3932f6d3c..6b10bb51b 100644 --- a/lib/src/chart/line_chart/line_chart_painter.dart +++ b/lib/src/chart/line_chart/line_chart_painter.dart @@ -1385,7 +1385,7 @@ class LineChartPainter extends AxisChartPainter { } /// Find the nearest spot (based on distanceCalculator) - final sortedSpots = []; + FlSpot? nearestSpot; double? smallestDistance; for (final spot in barData.spots) { if (spot.isNull()) continue; @@ -1398,22 +1398,24 @@ class LineChartPainter extends AxisChartPainter { ); if (distance <= data.lineTouchData.touchSpotThreshold) { - smallestDistance ??= distance; + if (nearestSpot == null) { + nearestSpot = spot; + smallestDistance = distance; + continue; + } - if (distance < smallestDistance) { - sortedSpots.insert(0, spot); + if (distance < smallestDistance!) { + nearestSpot = spot; smallestDistance = distance; - } else { - sortedSpots.add(spot); } } } - if (sortedSpots.isNotEmpty) { + if (nearestSpot != null) { return TouchLineBarSpot( barData, barDataPosition, - sortedSpots.first, + nearestSpot, smallestDistance!, ); } else {