9

When using numeric keyboard it decides whether to separate the decimals with dot or comma based on the region settings of the phone. For some reason Apple has decided that in the Netherlands it is a comma, yet everybody separates decimals with a dot here.

Changing the region of the phone works, yet not a viable solution.

Is there a way to change ios region globally/fixed?

1
  • I came to this question because I also need a dot Instead of a comma (because I wanted to use it for typing an IP address. But in the Netherlands a comma is the decimal separator so I'm not really sure what you mean by 'here'. I just want to warn you that if you override the default behavior you might confuse some people. Commented Jun 22, 2020 at 17:56

1 Answer 1

15

Ok, finally found a solution. You can't replace the comma or dot, yet you can use TextFormatter. Example:

class CommaTextInputFormatter extends TextInputFormatter {
  @override
  TextEditingValue formatEditUpdate(
    TextEditingValue oldValue,
    TextEditingValue newValue,
  ) {
    String truncated = newValue.text;
    TextSelection newSelection = newValue.selection;

    if (newValue.text.contains(',')) {
      truncated = newValue.text.replaceFirst(RegExp(','), '.');
    }
    return TextEditingValue(
      text: truncated,
      selection: newSelection,
    );
  }
}
Sign up to request clarification or add additional context in comments.

3 Comments

What to do if I have "," as a thousand separator?
How can I use it inside a TextFormField? An error The element type 'Type' can't be assigned to the list type 'TextInputFormatter'appears when I include it in InputFormatters.
@DavidL you use it like: TextFormField(inputFormatters: <TextInputFormatter>[ CommaTextInputFormatter(), ], ),

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.