handleTouch method

List<TouchLineBarSpot>? handleTouch(
  1. Offset localPosition,
  2. Size size,
  3. PaintHolder<LineChartData> holder
)

Makes a LineTouchResponse based on the provided localPosition

Processes localPosition and checks the elements of the chart that are near the offset, then makes a LineTouchResponse from the elements that has been touched.

Implementation

List<TouchLineBarSpot>? handleTouch(
  Offset localPosition,
  Size size,
  PaintHolder<LineChartData> holder,
) {
  final data = holder.data;

  /// it holds list of nearest touched spots of each line
  /// and we use it to draw touch stuff on them
  final touchedSpots = <TouchLineBarSpot>[];

  /// draw each line independently on the chart
  for (var i = 0; i < data.lineBarsData.length; i++) {
    final barData = data.lineBarsData[i];

    // find the nearest spot on touch area in this bar line
    final foundTouchedSpot =
        getNearestTouchedSpot(size, localPosition, barData, i, holder);
    if (foundTouchedSpot != null) {
      touchedSpots.add(foundTouchedSpot);
    }
  }

  touchedSpots.sort((a, b) => a.distance.compareTo(b.distance));

  return touchedSpots.isEmpty ? null : touchedSpots;
}