Image Credits — Upgraded Points

Here is a really simple way to implement a credit card formatter behavior. As stated in the Flutter documentation:

To create custom formatters, extend the TextInputFormatter class and implement the formatEditUpdate method.

The following code will let you specify a mask and a separator string to implement the desired behavior:

To implement the credit card behavior, we can use the mask “xxxx-xxxx-xxxx-xxxx” and the separator “-”, like this:

This will look like this:

I hope this was useful and a good starting point so you can extend this code and implement your desired behavior. Thank you very much for reading.