Skip to content

fix: call onSubmitted once when editing is completed - #114

Open
palmoni5 wants to merge 6 commits into
ubuntu-flutter-community:mainfrom
palmoni5:fix/submit-once
Open

palmoni5 wants to merge 6 commits into
ubuntu-flutter-community:mainfrom
palmoni5:fix/submit-once

Conversation

@palmoni5

Copy link
Copy Markdown

Stacked on #113 (textual dependency only; contains its commits); review only the last commit.

Problem

Done/Enter called onSubmitted from the text field and again from the blur listener; the order of the two differs by platform.

Fix

Add a protected submitValue() used by the Material and Cupertino text fields. Together with the focus listener it records which path already submitted the current text.

Behavior

onSubmitted is called once on Done.

Tests

Done submits only once after focus loss, focus loss does not resubmit after Done, Done after focus loss does not resubmit.

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.
The formatter and bindings getters built a new SpinFormatter and a new
shortcut map on every build. A new inputFormatters instance and a new
CallbackShortcuts map make the text field and shortcut manager treat
the configuration as changed on each rebuild.

Create them in initState and recreate them only when min, max or
decimals (formatter) or the presence of pageStep (bindings) change.
…arent

didUpdateWidget only handled a changed value, and initState used the
initial value as given:

- Replacing the focusNode kept listening to (and reporting focus of) the
  old node; an internally owned node was never released.
- An initial value outside [min, max] was shown and reported as is.
- Changing min/max no longer clamped the current value, and changing
  decimals or digits did not reformat the text.
- A controlled parent that echoes onChanged back into value replaced
  the text on every keystroke, turning "3.4" into "3.40" and moving
  the cursor while the user was typing.

Rebind the focus listener when the node changes, clamp the initial value
and the value after a bounds change (without calling onChanged), reformat
on decimals/digits changes, and keep the user's text while focused when
the parent echoes the value it already shows.
Pressing Done/Enter submits the text and also moves the focus away, so
onSubmitted was called twice: once from the text field's onSubmitted and
once from the focus listener. Depending on the platform, the focus
listener can run before or after the text field callback.

Route the text field's onSubmitted through a protected submitValue()
that, together with the focus listener, remembers which path already
submitted the current text, so each editing completion reports once.
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