Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TextInputType.numberWithOptions doesn't show comma or dot

Tags:

flutter

TextInputType.numberWithOptions(decimal: true),

I'm using the above code as a keyboardType in my TextFormField but in some devices, the device doesn't display comma or dot. Screenshot is given below:

keyboard image

Thinking of using this package: https://pub.dev/packages/virtual_keyboard

Any idea on how to solve it?

This doesn't solve my problem because in my case, it's happening on Android. Flutter TextField with number keyboard, comma is needed instead of period (Only iOS)

like image 618
mirkancal Avatar asked Nov 11 '19 13:11

mirkancal


2 Answers

This parameter will solve the problem

keyboardType: TextInputType.numberWithOptions(decimal: true),
like image 107
Andrew Avatar answered Sep 28 '22 04:09

Andrew


It's a well known Android issue in Samsung Keyboards
https://github.com/flutter/flutter/issues/61175

TLDR;

Usually the work-around is to fallback to text keyboard.

I developed a package with this work-around to easy get the keyboard vendor name and then make some crafty if samsung then TextInputType.text
package: keyboard_name
However, it don't solves the problem.

Long and cooler way

First: Format Inputters
note: I didn't find a way to enable the comma

Convert the inputted dot to comma and don't worry about the disabled comma
To do this you need to extend a Text Input Formatter

class CommaFormatter extends TextInputFormatter {
  @override
  TextEditingValue formatEditUpdate(
    TextEditingValue oldValue,
    TextEditingValue newValue,
  ) {
    String _text = newValue.text;
    //This is only if you need signed numbers. Will convert the first '.'(dot) to '-'(minus)
    //if (_text.isNotEmpty && _text[0] == '.') 
    //  _text = _text.replaceFirst('.', '-');
    return newValue.copyWith(
      text: _text.replaceAll('.', ','),
    );
  }
}

Also, you can put another smartass regex to format after this one

TextFormField(
    inputFormatters: [
        CommaFormatter(),
        FilteringTextInputFormatter.allow(
                        RegExp(
                          //r'^[-]{0,1}[0-9]*[,]?[0-9]*', //signed regex    
                          r'^[0-9]*[,]?[0-9]*', 
                        ),
                      ),
    ],
    /.../
    }
like image 22
Bruno Bee Nahorny Avatar answered Sep 28 '22 05:09

Bruno Bee Nahorny