Add ThousandsSeparatorTextInputFormatter#188243
Conversation
Adds a locale-agnostic TextInputFormatter to flutter/services that inserts a grouping separator into the integer part of the input as the user types (for example 1000000 -> 1,000,000). The separator, group size, and decimal handling are explicit parameters rather than derived from a locale, so the formatter has no dependency on the intl package and composes with the existing FilteringTextInputFormatter via TextField.inputFormatters. The selection is adjusted so the caret stays next to the same character after regrouping, and grouping is skipped while an IME composing region is active. Addresses flutter#188152
|
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. |
There was a problem hiding this comment.
Code Review
This pull request introduces ThousandsSeparatorTextInputFormatter, a new TextInputFormatter that formats large numbers with grouping separators as the user types, along with comprehensive unit tests. Feedback on the implementation suggests simplifying the separator-stripping and character-counting logic by using replaceAll instead of manual loops. Additionally, it is recommended to enhance the selection handling to preserve selection ranges and directions rather than collapsing them.
- Replace the manual separator-stripping loop and the character-counting loop with String.replaceAll for readability. - Map both ends of the selection so a non-empty selection and its direction are preserved instead of being collapsed to a single caret. - Add a test covering non-collapsed selection preservation.
This PR adds
ThousandsSeparatorTextInputFormatter, a locale-agnosticTextInputFormatterinflutter/servicesthat inserts a grouping separator into the integer part of the input as the user types (for example, formatting1000000as1,000,000).Why this approach, following the discussion in #188152:
TextInputType— it plugs into the existingTextFieldviainputFormatters, the pattern recommended when the earlierNumberFieldproposal (NumberField #114358) was closed.separator,groupSize,allowDecimal, anddecimalSeparatorare explicit parameters rather than derived from a locale, so the formatter has no dependency on theintlpackage and stays insideflutter/services. The caller chooses values appropriate to the user's locale.TextEditingValue.selectionis adjusted so the caret stays next to the same character after regrouping, reformatting is skipped while an IME composing region is active, and non-Western digits (e.g. Persian/Arabic-Indic) are grouped without extra configuration.It composes with the existing
FilteringTextInputFormatter.digitsOnlyto restrict input to digits.Fixes #188152
Pre-launch Checklist
///).