Flutter Credit Card Text Formatter

Sebas Arriola
CodeChai
Published in
2 min readAug 26, 2018
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.

The Flutter Pub is a medium publication to bring you the latest and amazing resources such as articles, videos, codes, podcasts etc. about this great technology to teach you how to build beautiful apps with it. You can find us on Facebook, Twitter, and Medium or learn more about us here. We’d love to connect! And if you are a writer interested in writing for us, then you can do so through these guidelines.

--

--