Skip to content

fix: Correct touched section index in PieChart with zero-value sections#2067

Open
mahmoodhamdi wants to merge 1 commit intoimaNNeo:mainfrom
mahmoodhamdi:fix/issue-1632-pie-touch-zero-sections
Open

fix: Correct touched section index in PieChart with zero-value sections#2067
mahmoodhamdi wants to merge 1 commit intoimaNNeo:mainfrom
mahmoodhamdi:fix/issue-1632-pie-touch-zero-sections

Conversation

@mahmoodhamdi
Copy link
Copy Markdown

Problem

When a PieChart has sections with zero values (e.g., [0, 0, 100, 200, 300]), touching any visible section returns the wrong touchedSectionIndex. For example, touching the section with value 100 (index 2) incorrectly returns index 0.

Root Cause

The drawSections method skips zero-value sections with continue, so they aren't drawn. However, handleTouch does not skip them — it generates a section path with a zero sweep angle, which can incorrectly match a touch point before the actual visible section is checked.

Solution

Added the same if (section.value == 0) continue; check to the handleTouch loop, matching the behavior of drawSections.

Testing

Added a unit test with sections [0, 0, 100, 200, 300] that verifies:

  • Touching section with value 100 returns index 2
  • Touching section with value 200 returns index 3
  • Touching section with value 300 returns index 4
  • Touching center returns -1

Checklist

  • flutter analyze passes with zero warnings
  • flutter test passes
  • dart format . applied
  • CHANGELOG.md updated

Closes #1632

The handleTouch method did not skip zero-value sections, unlike
drawSections which uses `continue` for sections with value == 0.
This caused a path with zero sweep angle to be generated and
potentially matched, returning wrong indices.

Added the same skip logic to handleTouch and a unit test covering
sections like [0, 0, 100, 200, 300].

Closes imaNNeo#1632
@codecov
Copy link
Copy Markdown

codecov bot commented Apr 10, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 92.92%. Comparing base (d3a8ac2) to head (d7308cb).
⚠️ Report is 3 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #2067   +/-   ##
=======================================
  Coverage   92.92%   92.92%           
=======================================
  Files          50       50           
  Lines        3873     3874    +1     
=======================================
+ Hits         3599     3600    +1     
  Misses        274      274           
Flag Coverage Δ
flutter 92.92% <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 Sentry.
📢 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.

Comment thread CHANGELOG.md
@@ -1,4 +1,5 @@
## newVersion
* **BUGFIX** (by @mahmoodhamdi) Fix incorrect touched section index in PieChart when some sections have zero values, #1632
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't need to add the changelog line in this file anymore.
We'll generate it based on the commit history from now on.

@imaNNeo
Copy link
Copy Markdown
Owner

imaNNeo commented Apr 10, 2026

Good catch!
It's ready to merge.
Please remove your changelog in CHANGELOG.md, and update your branch.
Then we're good to go!

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.

PieChart: touched section index is incorrect if some values are zero

2 participants