phone_formatter 0.0.2+2 copy "phone_formatter: ^0.0.2+2" to clipboard
phone_formatter: ^0.0.2+2 copied to clipboard

A simple and flexible phone number formatter for Flutter, supporting mask and set of masks (dynamic country code detection).

πŸ“± phone_formatter #

A simple and flexible phone number formatter for Flutter, supporting mask and set of masks (dynamic country code detection).


πŸš€ Usecases #

βœ… Support for dynamic masks for different country codes
βœ… Ability to set a fixed format for a specific country
βœ… Input limited to digits and allowed characters
βœ… Protection against deleting the country code
βœ… Automatic insertion of digits into the first available position
βœ… Easy integration with TextFormField and TextField


πŸ“¦ Installation #

Add the dependency in pubspec.yaml:

dependencies:
  phone_formatter: ^0.0.2+2

βš™οΈ API #

Mask configuration for one country.

PhoneFormatterConfig #

Field Type Description
mask String Format mask, e.g. (###) ### ##-## or ### ### ####
countryCode String Country code, e.g. +7 or +1
phoneMask String Combines countryCode and mask
contryCodeOffset int Length offset relative to the country code

PhoneFormatter #

Contructor Description
PhoneFormatter({required PhoneFormatterConfig config}) Fixed mask
PhoneFormatter.array({required List<PhoneFormatterConfig> configVariants}) Set of allowed masks

Required methods for correct formatter operation #

  @override
  void initState() {
    // Automatically sets the value with a mask. Required for the PhoneFormatter constructor variant.
    controller.value = formatter.formatEditUpdate(TextEditingValue.empty, TextEditingValue.empty);

    // Ensures the cursor moves to the first free cell on focus.
    focusNode.addListener(() {
      if (focusNode.hasFocus) {
        controller.selection = TextSelection.collapsed(
          offset: formatter.getFirstUnderline(controller.text),
        );
      }
    });
    super.initState();
  }

πŸ§ͺ Formatter behavior #

  • When entering a digit at the end of the string, it is automatically inserted into the first free position (_)
  • When pasting a number from the clipboard, the formatter removes extra characters and plus signs
  • When a config is active, it is impossible to delete the country code
  • All invalid characters (except +, (, ), -, _, space) are automatically removed

πŸ“Έ Example usage #

  • +7 (___) ___ __-__ β†’ +7 (999) 123 45-67
  • +1 ___ ___ ____ β†’ +1 555 111 2222
  • +92 ___ _______ β†’ +92 300 1234567
4
likes
150
points
4
downloads

Publisher

verified publishershoshi.tech

Weekly Downloads

A simple and flexible phone number formatter for Flutter, supporting mask and set of masks (dynamic country code detection).

Homepage
Repository (GitHub)
View/report issues

Documentation

API reference

License

BSD-3-Clause (license)

Dependencies

flutter

More

Packages that depend on phone_formatter