fix: onToggle fires when tapping already-active switch (#101)#102
Open
Vasusn wants to merge 1 commit intoPramodJoshi:masterfrom
Open
fix: onToggle fires when tapping already-active switch (#101)#102Vasusn wants to merge 1 commit intoPramodJoshi:masterfrom
Vasusn wants to merge 1 commit intoPramodJoshi:masterfrom
Conversation
There was a problem hiding this comment.
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
_handleOnTapto skiponTogglewhen the tapped index is already active (anddoubleTapDisableis off). - Add widget tests to confirm
onToggleis not called for no-op taps, and thatdoubleTapDisable: truestill deselects and callsonToggle(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.
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
20cc53f to
6b6b5d2
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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