Skip to content

fix: onToggle fires when tapping already-active switch (#101)#102

Open
Vasusn wants to merge 1 commit intoPramodJoshi:masterfrom
Vasusn:fix/issue-101-ontoggle-fires-on-same-switch
Open

fix: onToggle fires when tapping already-active switch (#101)#102
Vasusn wants to merge 1 commit intoPramodJoshi:masterfrom
Vasusn:fix/issue-101-ontoggle-fires-on-same-switch

Conversation

@Vasusn
Copy link
Copy Markdown

@Vasusn Vasusn commented Apr 26, 2026

When changeOnTap is true and doubleTapDisable is false, tapping the already-selected switch would incorrectly fire onToggle even though the selection did not change. Add an early-return guard in _handleOnTap to skip the callback when the tapped index equals the current initialLabelIndex.

The existing doubleTapDisable behaviour is preserved: when that flag is true, tapping the active switch intentionally deselects it (index → null) and fires onToggle(null) as before.

Fixes #101

Copilot AI review requested due to automatic review settings April 26, 2026 09:39
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Fixes an issue where ToggleSwitch.onToggle was invoked on taps of the already-selected switch even though the selected index did not change (when changeOnTap: true and doubleTapDisable: false), aligning behavior with issue #101.

Changes:

  • Add an early-return guard in _handleOnTap to skip onToggle when the tapped index is already active (and doubleTapDisable is off).
  • Add widget tests to confirm onToggle is not called for no-op taps, and that doubleTapDisable: true still deselects and calls onToggle(null).

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
lib/toggle_switch.dart Adds an early-return guard in tap handling to prevent redundant onToggle calls when selection doesn’t change.
test/toggle_switch_test.dart Adds regression tests for the no-op tap case and preserves the doubleTapDisable deselect behavior.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread test/toggle_switch_test.dart Outdated
Comment thread lib/toggle_switch.dart
When changeOnTap is true and doubleTapDisable is false, tapping the
already-selected switch would incorrectly fire onToggle even though the
selection did not change. Add an early-return guard in _handleOnTap to
skip the callback when the tapped index equals the current
initialLabelIndex.

The existing doubleTapDisable behaviour is preserved: when that flag is
true, tapping the active switch intentionally deselects it (index → null)
and fires onToggle(null) as before.

Fixes PramodJoshi#101
@Vasusn Vasusn force-pushed the fix/issue-101-ontoggle-fires-on-same-switch branch from 20cc53f to 6b6b5d2 Compare April 26, 2026 09:51
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.

onToggle is triggered when selected

2 participants