Skip to content

fix: do not report incomplete input as zero, roll back to the last valid value - #111

Open
palmoni5 wants to merge 3 commits into
ubuntu-flutter-community:mainfrom
palmoni5:fix/partial-input-rollback
Open

palmoni5 wants to merge 3 commits into
ubuntu-flutter-community:mainfrom
palmoni5:fix/partial-input-rollback

Conversation

@palmoni5

Copy link
Copy Markdown

Stacked on #110 (contains its commits); review only the last commit.

Problem

An empty field, - or . was treated as 0 and reported with onChanged(0). A number below min while typing toward a valid one also called onChanged. The rollback value was stale: it came from the start of the focus session.

Fix

Incomplete text keeps the value and does not notify. An out-of-range number notifies only once it is in range. The rollback value is the last accepted typed or stepped value, and a canChange rejection restores the previous valid text.

Intentional behavior changes

  • onChanged is no longer called for empty or partial input, or for typing below min / above max.
  • After a canChange rejection, or on blur with invalid text, the field rolls back to the last valid typed value.
  • The shared input tests now expect the value to stay at 1 after clearing (hasValue(0) → hasValue(1)).

Tests

clearing input does not notify a false zero, steps update rollback value, accepted text updates the empty-input rollback, and the updated test_spinbox.dart.

CI on main currently fails in the Android example build and the publish dry-run; it goes green with #102.

setValue clamped the requested value and then formatted it with the
configured number of decimals. A NaN or infinite value was written into
the text field, and rounding could push a clamped value past a
fractional bound (max: 0.29 with decimals: 1 accepted 0.3).

Ignore non-finite values, round before validating, and drop a step whose
rounded value falls outside [min, max]. onSubmitted now receives the
already-rounded value.
A +/-, arrow or page step wrote the new text into the controller and
relied on the controller listener to update the value and call
onChanged. That listener ran inside setValue's setState, called
canChange a second time for the same value and triggered a second
setState.

Suppress the listener while the widget writes its own text, update the
value directly and call onChanged explicitly, once per step.
_updateController now shifts the selection by the oldValue it is given
(instead of the current value, which didUpdateWidget had already
replaced), clamps it to the new text, and clears any IME composing range
that referred to the old text.
…lid value

Text that does not parse as a number yet (an empty field, "-", ".")
was treated as 0: the state value became 0 and onChanged(0) was called
while the user was still typing. The same happened for a number below
min while typing towards a valid value (e.g. "1" on the way to "15"
with min: 10).

- Incomplete text keeps the current value and does not call onChanged.
- An out-of-range number updates the internal value but calls onChanged
  only once it is within [min, max].
- The rollback value used on blur/submit and after a canChange
  rejection is the last accepted value (typed or stepped), instead of
  the value from the previous focus session.
- A canChange rejection restores the previous text without re-entering
  the controller listener.

The shared input tests now expect the value to stay at 1 after the
field is cleared.
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.

1 participant