Skip to content

PhoneInputFormatter incorrectly converts Brazilian area code 83 to 73 #179

@MarcosPereira1

Description

@MarcosPereira1

Bug Description

The PhoneInputFormatter has a bug where it incorrectly converts the Brazilian area code 83 (Paraíba state) to 73 (Bahia state) during phone number formatting.

Steps to Reproduce

  1. Create a PhoneInputFormatter with Brazilian settings:

    final formatter = PhoneInputFormatter(
      allowEndlessPhone: false,
      defaultCountryCode: 'BR',
    );
  2. Try to format a phone number starting with area code 83:

    final result = formatter.formatEditUpdate(
      TextEditingValue(),
      TextEditingValue(text: '83987654321'),
    );
  3. Observe the result

Expected Behavior

  • Input: 83987654321
  • Expected output: (83) 98765-4321

Actual Behavior

  • Input: 83987654321
  • Actual output: (73) 98765-4321

The area code is incorrectly changed from 83 to 73.

bug.mp4

Impact

This affects users from Paraíba state (Brazil) who cannot correctly input their phone numbers. Area code 83 is a valid Brazilian area code assigned to Paraíba state by ANATEL (Brazilian telecommunications agency).

Environment

  • Package: flutter_multi_formatter
  • Versions tested: 2.10.0, 2.12.0, 2.13.10 (bug exists in all tested versions)
  • Flutter: 3.24.3
  • Dart: 3.5.3
  • Platform: iOS/Android

Additional Context

  • Area code 83 is officially assigned to Paraíba state, Brazil
  • Area code 73 is assigned to Bahia state, Brazil
  • This appears to be a mapping issue within the library's phone formatting logic
  • The bug occurs both during step-by-step typing and when formatting complete numbers

Workaround

We've implemented a temporary wrapper that detects and fixes this specific case:

class CustomPhoneInputFormatter extends TextInputFormatter {
  final PhoneInputFormatter _baseFormatter;

  // ... constructor

  @override
  TextEditingValue formatEditUpdate(TextEditingValue oldValue, TextEditingValue newValue) {
    final baseResult = _baseFormatter.formatEditUpdate(oldValue, newValue);
    
    // Fix the specific bug: DDD 83 being changed to 73
    if (_shouldFixDDD83(newValue.text, baseResult.text)) {
      final correctedText = baseResult.text.replaceFirst('(73', '(83');
      return TextEditingValue(
        text: correctedText,
        selection: TextSelection.collapsed(offset: correctedText.length),
      );
    }
    return baseResult;
  }

  bool _shouldFixDDD83(String originalText, String formattedText) {
    final original = originalText.replaceAll(RegExp(r'[^\d]'), '');
    final formatted = formattedText.replaceAll(RegExp(r'[^\d]'), '');
    return original.startsWith('83') && formatted.startsWith('73');
  }
}

Test Case

void main() {
  test('PhoneInputFormatter should preserve area code 83', () {
    final formatter = PhoneInputFormatter(
      allowEndlessPhone: false,
      defaultCountryCode: 'BR',
    );

    final result = formatter.formatEditUpdate(
      TextEditingValue(),
      TextEditingValue(text: '83987654321'),
    );

    expect(result.text, contains('83')); // Should contain 83
    expect(result.text, isNot(contains('73'))); // Should NOT contain 73
  });
}

Would appreciate a fix for this issue as it affects Brazilian users from Paraíba state.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions