top of page

[Flutter/Dart] Format string with TextField


What want to do

I want to create an input form using TextField.

For example, if the input content is a monetary amount, I would like to display it in 3-digit delimiters with a ¥ prefix.

Rather than having the user enter these numbers, I would like the user to just enter the numbers and the app will automatically format them.



Method

This can be achieved by inheriting TextInputFormatter and adding the desired formatting process.

See for example this article .


Below I will explain what I personally encountered.



Input to formatEditUpdate


The input to formatEditUpdate, that is, the argument oldValue, is the formatted string.

Nnot the number entered by the user.



Value of TextEditingValue.selection


Recalculate the number of characters after formatting.


Originally I set TextEditingValue.selection as newValue.selection, but in this case

if the number of characters after formatting becomes shorter, it will reference outside the range, and the following error occurred in formatEditUpdate


Range start <int> is out of text of length <int>



Response to input value ""


Even if TextField.keybordType is set to TextInputType.number, there is a possibility that “” will appear.

For example, when all input is deleted.



TextField.maxLength value


If the formatting process changes the number of characters (for example, when adding ¥ or ,), set the length after formatting.









Recent Posts

See All

Let's do our best with our partner:​ ChatReminder

iphone6.5p2.png

It is an application that achieves goals in a chat format with partners.

google-play-badge.png
Download_on_the_App_Store_Badge_JP_RGB_blk_100317.png

Let's do our best with our partner:​ ChatReminder

納品:iPhone6.5①.png

It is an application that achieves goals in a chat format with partners.

google-play-badge.png
Download_on_the_App_Store_Badge_JP_RGB_blk_100317.png

Theme diary: Decide the theme and record for each genre

It is a diary application that allows you to post and record with themes and sub-themes for each genre.

google-play-badge.png
Download_on_the_App_Store_Badge_JP_RGB_blk_100317.png
bottom of page