Skip to content

feat: add showCountryCode parameter to PhoneInputFormatter#182

Open
bernardoveras wants to merge 2 commits intocaseyryan:masterfrom
bernardoveras:master
Open

feat: add showCountryCode parameter to PhoneInputFormatter#182
bernardoveras wants to merge 2 commits intocaseyryan:masterfrom
bernardoveras:master

Conversation

@bernardoveras
Copy link

Summary

  • Added showCountryCode parameter (default: true) to PhoneInputFormatter, formatAsPhoneNumber, and the isFilled / unmasked getters.
  • When false, the formatted output displays only the local portion of the number (e.g. (11) 99999-9999) while unmasked always returns the full international number (e.g. +5511999999999).
  • Extracted duplicated strip-and-trim logic from _applyMask and formatAsPhoneNumber into a single private _resolveCountryCodeDisplay helper.
  • Fixed a latent negative-offset assertion crash in formatEditUpdate by clamping selectionEnd — this could trigger when showCountryCode: false produces a masked value shorter than the input and the cursor is positioned early in the text.

Usage

// Default — country code visible (backward compatible)
PhoneInputFormatter(showCountryCode: true)
// input: '+5511999999999' → masked:   '+55 (11) 99999-9999'
//                        → unmasked: '+5511999999999'

// Hide country code
PhoneInputFormatter(showCountryCode: false)
// input: '+5511999999999' → masked:   '(11) 99999-9999'
//                        → unmasked: '+5511999999999'  ← full number preserved

// Top-level helper
formatAsPhoneNumber('+5511999999999', showCountryCode: false);
// → '(11) 99999-9999'

// Combined with defaultCountryCode
PhoneInputFormatter(defaultCountryCode: 'BR', showCountryCode: false)
// input: '11999999999' → masked:   '(11) 99999-9999'
//                      → unmasked: '+5511999999999'

Breaking changes

None. showCountryCode defaults to true, preserving all existing behaviour exactly.

Test plan

  • flutter test test/flutter_multi_formatter_test.dart — all existing tests pass; 9 new showCountryCode tests added covering BR, US, partial input, unmasked getter, formatAsPhoneNumber, isPhoneValid, and the defaultCountryCode combination.
  • flutter analyze lib/formatters/phone_input_formatter.dart — no issues.
  • Manual: attach PhoneInputFormatter(showCountryCode: false) to a TextField, enter a Brazilian number (+5511999999999) and confirm display shows (11) 99999-9999 while formatter.unmasked returns +5511999999999.

Add a showCountryCode flag (default: true) to PhoneInputFormatter,
formatAsPhoneNumber, and isPhoneValid for API consistency.

When set to false, the formatted output displays only the local part of
the phone number (e.g. '(11) 99999-9999' instead of '+55 (11) 99999-9999')
while the unmasked getter always returns the full international number.

Also fixes a latent negative-offset assertion crash in formatEditUpdate
by clamping selectionEnd, and extracts the duplicated strip-and-trim
logic into a private _resolveCountryCodeDisplay helper.
…arameter

feat: add showCountryCode parameter to PhoneInputFormatter
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