Skip to content

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

Merged
imaNNeo merged 2 commits into
imaNNeo:mainfrom
ultramcu:fix/bar-stack-downward-touch
Oct 19, 2026
Merged

imaNNeo merged 2 commits into
imaNNeo:mainfrom
ultramcu:fix/bar-stack-downward-touch

Conversation

@ultramcu

@ultramcu ultramcu commented May 26, 2026 •

Copy link
Copy Markdown
Contributor

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).
@codecov

codecov Bot commented Oct 13, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 94.13%. Comparing base (8f0ab4a) to head (b0e2a66).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #2103   +/-   ##
=======================================
  Coverage   94.13%   94.13%           
=======================================
  Files          54       54           
  Lines        4669     4671    +2     
=======================================
+ Hits         4395     4397    +2     
  Misses        274      274           
Flag Coverage Δ
flutter 94.13% <100.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@imaNNeo
imaNNeo merged commit 426caf1 into imaNNeo:main Oct 19, 2026
5 checks passed
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

2 participants