title: Flutter – formatting phone number text field
body: <p>I am trying to make a text field that properly formats a phone number. I have tried using</p>
  
  <p><code>NumberFormat("+# ### ### ####");</code></p>
  
  <p>But it doesn't retain spaces</p>
  
  <p>I have tried simplifying it by just adding a <code>+</code> to the front but I cannot backspace when I set the offset.</p>
  
  <pre><code>class PhoneInputFormatter extends TextInputFormatter {
    TextEditingValue formatEditUpdate(
        TextEditingValue oldValue, TextEditingValue newValue) {
      final text = newValue.text.replaceAll(RegExp(r'\D'), '');
      final offset = text.length + 1;
  
      return newValue.copyWith(
        text: text.length &gt;= 1 ? '+$text' : '',
        selection: TextSelection.collapsed(offset: offset),
      );
    }
  }
  </code></pre>
  
  <p>Any help would be appreciated</p>