Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion lib/src/chart/bar_chart/bar_chart_painter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -943,7 +943,12 @@ class BarChartPainter extends AxisChartPainter<BarChartData> {
final stackItem = nearestBarRod.rodStackItems[stackIndex];
final fromPixel = getPixelY(stackItem.fromY, viewSize, holder);
final toPixel = getPixelY(stackItem.toY, viewSize, holder);
if (touchedPoint.dy <= fromPixel && touchedPoint.dy >= toPixel) {
// A negative-direction stack item has fromY > toY, in which case
// fromPixel < toPixel (Flutter Y grows downward). Compare against
// the [min, max] range so the check works in either orientation.
final topPixel = min(fromPixel, toPixel);
final bottomPixel = max(fromPixel, toPixel);
if (touchedPoint.dy >= topPixel && touchedPoint.dy <= bottomPixel) {
touchedStackIndex = stackIndex;
touchedStack = stackItem;
break;
Expand Down
42 changes: 42 additions & 0 deletions test/chart/bar_chart/bar_chart_painter_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3398,6 +3398,48 @@ void main() {
expect(result1.touchedRodDataIndex, 0);
},
);

test('detects touches on downward (negative) stack items', () {
const viewSize = Size(200, 100);
final barGroups = [
BarChartGroupData(
x: 0,
barRods: [
BarChartRodData(
fromY: 0,
toY: -10,
width: 20,
color: const Color(0xFF0000FF),
rodStackItems: [
BarChartRodStackItem(0, -10, const Color(0xFFFF0000)),
],
),
],
),
];
final (minY, maxY) = BarChartHelper().calculateMaxAxisValues(barGroups);
final data = BarChartData(
barGroups: barGroups,
titlesData: const FlTitlesData(show: false),
alignment: BarChartAlignment.center,
barTouchData: const BarTouchData(handleBuiltInTouches: true),
minY: minY,
maxY: maxY,
);

final painter = BarChartPainter();
final holder =
PaintHolder<BarChartData>(data, data, TextScaler.noScaling);

// The single downward rod (fromY: 0, toY: -10) spans the full viewport
// height in pixels. A touch in the middle of the chart should land on
// the rod's only stack item.
final result =
painter.handleTouch(const Offset(100, 50), viewSize, holder);
expect(result, isNotNull);
expect(result!.touchedStackItemIndex, 0);
expect(result.touchedStackItem, isNotNull);
});
});

group('drawExtraLines()', () {
Expand Down
Loading