Skip to content

Instantly share code, notes, and snippets.

@natintosh
Last active December 16, 2019 09:11
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save natintosh/716d36674e59cbf18e1d69e790b1221d to your computer and use it in GitHub Desktop.
Save natintosh/716d36674e59cbf18e1d69e790b1221d to your computer and use it in GitHub Desktop.
import 'package:flutter/services.dart';
import 'package:string_mask/string_mask.dart';
class PhoneMaskInputFormatter extends TextInputFormatter {
final String mask;
final String escapeChar;
final RegExp regExp = RegExp(r'[^\d]+');
Map<int, String> _separatorMap;
PhoneMaskInputFormatter({this.mask, this.escapeChar = r'[\d]+'}) {
assert(mask != null || mask.isNotEmpty);
final entries = regExp
.allMatches(mask)
.map((match) => MapEntry(match.start, match.group(0)));
_separatorMap = Map.fromEntries(entries);
}
@override
TextEditingValue formatEditUpdate(
TextEditingValue oldValue, TextEditingValue newValue) {
int oldValueLength = oldValue.text.length;
int newValueLength = newValue.text.length;
final StringBuffer newText = StringBuffer();
if (newValueLength > 0 && newValueLength > oldValueLength) {
bool shouldSeparate = mask[newValueLength - 1].contains(regExp);
if (newValueLength > mask.length) {
newText.write(oldValue.text);
return oldValue.copyWith(
text: newText.toString(),
selection: TextSelection.collapsed(offset: newText.length),
);
}
if (newValueLength < mask.length && shouldSeparate) {
String separator = _separatorMap[newValueLength - 1];
newText.write(oldValue.text +
separator +
newValue.text.substring(newValueLength - 1));
return newValue.copyWith(
text: newText.toString(),
selection: TextSelection.collapsed(offset: newText.length),
);
}
}
return newValue;
}
TextEditingValue applyMask(TextEditingValue value) {
final StringBuffer newText = StringBuffer();
String newMask = mask.replaceAll(RegExp(r'[\d]'), '#');
var stringMask = StringMask(newMask);
String newValue = stringMask.apply(value?.text);
newText.write(newValue);
return value.copyWith(
text: newText.toString(),
selection: TextSelection.collapsed(offset: newText.length),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment