Skip to content

fix: Detect touches on downward (negative) BarChartRodStackItems#2103

Open
ultramcu wants to merge 1 commit into
imaNNeo:mainfrom
ultramcu:fix/bar-stack-downward-touch
Open

fix: Detect touches on downward (negative) BarChartRodStackItems#2103
ultramcu wants to merge 1 commit into
imaNNeo:mainfrom
ultramcu:fix/bar-stack-downward-touch

Conversation

@ultramcu
Copy link
Copy Markdown

@ultramcu ultramcu commented May 26, 2026

Description

BarChartPainter.handleTouch iterates rodStackItems and checks
dy <= fromPixel && dy >= toPixel to decide which stack segment was touched.
That inequality silently fails for downward (negative) stack items, where
fromY > toY and therefore fromPixel < toPixel in screen space (Flutter Y
grows downward).

As a result, BarTouchResponse.touchedStackItem was always null and
touchedStackItemIndex was always -1 for negative rod stacks, which breaks
tooltips and getTooltipItem / touch callbacks on common financial /
loss-style bar charts.

This PR makes the range check orientation-agnostic by computing
topPixel = min(fromPixel, toPixel) and bottomPixel = max(fromPixel, toPixel),
then testing dy >= topPixel && dy <= bottomPixel. Upward behaviour is
unchanged (same bounds, same inclusive range); downward behaviour is now correct.

A regression test is added in the existing handleTouch() group: a single
group with one downward rod (fromY: 0, toY: -10) and one stack item
spanning the whole rod — a touch in the middle of the chart now returns
touchedStackItemIndex == 0; before the fix it returned -1 (verified
fail-before / pass-after locally).

Checklist

  • I have followed the Contributor Guide when preparing my PR.
  • I have updated/added tests for ALL new/updated/fixed functionality.
  • [-] I have updated/added relevant documentation and added dartdoc comments with ///. (Internal-loop fix; no public API surface change.)
  • [-] I have updated/added relevant examples in example. (Bug fix in existing behaviour; example app unchanged.)

Breaking Change?

  • Yes, this PR is a breaking change.
  • No, this PR is not a breaking change.

Related Issues

Closes #2104.

BarChartPainter.handleTouch iterated rodStackItems and checked
`dy <= fromPixel && dy >= toPixel` to decide which stack segment was
touched. That inequality silently fails for downward (negative) stack
items, where fromY > toY and therefore fromPixel < toPixel in screen
space (Flutter Y grows downward). As a result, BarTouchResponse's
touchedStackItem was always null and touchedStackItemIndex was always -1
for negative rod stacks, breaking tooltip and touch callbacks on common
financial / loss-style bar charts.

Compute the touch range with min/max so the check is orientation-agnostic
and works for both upward and downward stacks. Upward behavior is
unchanged (same min/max bounds, same inclusive range).

Adds a regression test in the existing handleTouch() group: a single
group with one downward rod (fromY: 0, toY: -10) and one stack item; a
touch in the middle of the chart returns touchedStackItemIndex == 0
(was -1 before the fix).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: BarChart touchedStackItemIndex always -1 for downward (negative) rod stack items

1 participant