Skip to content

Fixed so that editableText shows intermediate obscured characters when TextField reaches maxLength. - #144929

Closed
JSimonDev wants to merge 11 commits into
flutter:masterfrom
JSimonDev:fix141560
Closed

JSimonDev wants to merge 11 commits into
flutter:masterfrom
JSimonDev:fix141560

Conversation

@JSimonDev

@JSimonDev JSimonDev commented Mar 11, 2024 •

Copy link
Copy Markdown

This PR changes the condition of buildTextSpan to briefly show the password only when the TextField does not have a maxLength or the text is shorter than maxLength but does not show it in intermediate characters if the text has already reached the maxLength.

List which issues are fixed by this PR. You must list at least one issue. An issue is not required if the PR fixes something trivial like a typo.
Fixes #141560

Pre-launch Checklist

If you need help, consider asking for advice on the #hackers-new channel on Discord.

@google-cla

google-cla Bot commented Mar 11, 2024

Copy link
Copy Markdown

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@github-actions github-actions Bot added a: text input Entering text in a text field or keyboard related problems framework flutter/packages/flutter repository. See also f: labels. p: material_ui material_ui package in flutter/packages labels Mar 11, 2024
@justinmc justinmc added the team-text-input Owned by Text Input team label Mar 14, 2024

/// The maximum number of characters that can be entered in the text field.
/// If `null`, there is no maximum limit.
final int? maxLength;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think it would be better if we don't have to change the public API for this fix. Is it possible to change the logic over here so the obsured text doesn't get revealed if the text isn't changed?

@Piinks

Piinks commented May 8, 2024

Copy link
Copy Markdown
Contributor

Hey @JSimonDev would you like to continue working on this PR and respond to the feedback above?

@Piinks

Piinks commented Jun 5, 2024

Copy link
Copy Markdown
Contributor

Thank you for your contribution. I'm going to close this PR for now since there are outstanding comments, just to get this off our PR review queue. Please don't hesitate to re-open or submit a new PR if you have the time to address the review comments. Thanks!

@Piinks Piinks closed this Jun 5, 2024
gaaclarke pushed a commit to xRossKoh/flutter that referenced this pull request Nov 17, 2026
…tters (flutter#195022)

Fixes flutter#141560

### Description
When `EditableText.obscureText` is `true`,
`EditableTextState.updateEditingValue` decided whether to briefly reveal
the "newly typed" character (`value.text.length == _value.text.length +
1`) and recorded its index **before** running `_formatAndSetValue`.

When an `inputFormatter` (such as `LengthLimitingTextInputFormatter`
once `maxLength` is reached) rejected the newly inserted character,
`_value` stayed unchanged, yet `_obscureShowCharTicksPending` and
`_obscureLatestCharIndex` were still set. The reveal was then painted by
the next rebuild (for example a caret move), showing the **existing**
character at the caret in plain text. On Android this is the default
behaviour of `TextField(maxLength: …, obscureText: true)` because
`maxLength` defaults to `MaxLengthEnforcement.enforced` there.

This change moves `_formatAndSetValue` before the reveal decision and
only reveals when both:
1. exactly one character was added by the platform (`value.text.length
== oldValue.text.length + 1`), and
2. the formatted value kept that character (`_value.text.length ==
value.text.length`).

If a character was still revealed from a previous keystroke and the new
input is rejected, the state is cleared with a rebuild so the character
is re-obscured immediately. A rejected edit leaves the controller value
unchanged, so nothing else would trigger that rebuild.

This follows the direction given on the earlier attempt flutter#144929: fix the
logic in `updateEditingValue` rather than adding `maxLength` to
`EditableText`'s public API
(flutter#144929 (comment)).

Behaviour note: the reveal decision now looks at the *formatted* value.
A formatter that accepts a keystroke but changes the text length (for
example one that inserts separators) no longer reveals a character; a
same-length transform still reveals the transformed character.
Previously such cases revealed whatever character sat at the old caret
index.


### Verified on a device
Pixel 9 Pro emulator, Android 17 (API 37), `TextField(obscureText: true,
maxLength: 5)`: type `abcde`, move the caret to position 2, type `z`,
press →.

| Before (master) | After (this PR) |
|---|---|
| ![Before: the existing character at the caret is shown in plain text
after the caret
moves](https://gist.githubusercontent.com/okorohelijah/4ba9959cefac675030c088ce9cad6387/raw/141560_before_master_android17.gif)
| ![After: the field stays
obscured](https://gist.githubusercontent.com/okorohelijah/4ba9959cefac675030c088ce9cad6387/raw/141560_after_pr195022_android17.gif)
|

Recordings as MP4:
[before](https://gist.githubusercontent.com/okorohelijah/4ba9959cefac675030c088ce9cad6387/raw/141560_before_master_android17.mp4),
[after](https://gist.githubusercontent.com/okorohelijah/4ba9959cefac675030c088ce9cad6387/raw/141560_after_pr195022_android17.mp4).

## Pre-launch Checklist

- [x] I read the [Contributor Guide] and followed the process outlined
there for submitting PRs.
- [x] I read the [Tree Hygiene] wiki page, which explains my
responsibilities.
- [x] I read and followed the [Flutter Style Guide], including [Features
we expect every widget to implement].
- [x] I signed the [CLA].
- [x] I listed at least one issue that this PR fixes in the description
above.
- [x] I updated/added relevant documentation (doc comments with `///`).
- [x] I added new tests to check the change I am making, or this PR is
[test-exempt].
- [x] All existing and new tests are passing.

<!-- Links -->
[Contributor Guide]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview
[Tree Hygiene]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md
[Flutter Style Guide]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md
[Features we expect every widget to implement]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement
[CLA]: https://cla.developers.google.com/
[test-exempt]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests

---------

Co-authored-by: Jenn Magder <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

a: text input Entering text in a text field or keyboard related problems framework flutter/packages/flutter repository. See also f: labels. p: material_ui material_ui package in flutter/packages team-text-input Owned by Text Input team

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Flutter text field (on android) shows plain text character when max char limit reached and user attempts to insert in the middle of the string.

4 participants