Skip to content

Add ThousandsSeparatorTextInputFormatter#188243

Open
aiden30015 wants to merge 3 commits into
flutter:masterfrom
aiden30015:thousands-separator-formatter
Open

Add ThousandsSeparatorTextInputFormatter#188243
aiden30015 wants to merge 3 commits into
flutter:masterfrom
aiden30015:thousands-separator-formatter

Conversation

@aiden30015

@aiden30015 aiden30015 commented Jun 19, 2026

Copy link
Copy Markdown

This PR adds ThousandsSeparatorTextInputFormatter, a locale-agnostic TextInputFormatter in flutter/services that inserts a grouping separator into the integer part of the input as the user types (for example, formatting 1000000 as 1,000,000).

Why this approach, following the discussion in #188152:

  • It is a composable primitive, not a new widget or a flag on TextInputType — it plugs into the existing TextField via inputFormatters, the pattern recommended when the earlier NumberField proposal (NumberField #114358) was closed.
  • The separator, groupSize, allowDecimal, and decimalSeparator are explicit parameters rather than derived from a locale, so the formatter has no dependency on the intl package and stays inside flutter/services. The caller chooses values appropriate to the user's locale.
  • The TextEditingValue.selection is 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.digitsOnly to restrict input to digits.

Fixes #188152

Pre-launch Checklist

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
@google-cla

google-cla Bot commented Jun 19, 2026

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. labels Jun 19, 2026

@gemini-code-assist gemini-code-assist Bot left a comment

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.

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.

Comment thread packages/flutter/lib/src/services/text_formatter.dart Outdated
Comment thread packages/flutter/lib/src/services/text_formatter.dart
Comment thread packages/flutter/lib/src/services/text_formatter.dart Outdated
- 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.
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.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Framework should provide thousands separator for input fields with large numbers

1 participant