codegrue / card_settings

A flutter package for building card based forms.
https://pub.dartlang.org/packages/card_settings
MIT License
555 stars 103 forks source link

How to localize CardSettingsDouble? #70

Closed NTMS2017 closed 4 years ago

NTMS2017 commented 5 years ago

I need to localize CardSettingsDouble to Turkish due to decimal point. Unfortunately is not working correctly on iOS. Android has no problem. But for iOS keyboard shows only ' , ' sign.

I put this in my code but this is not a correct solution. If I use TextInputType.numberWithOptions(decimal: true) the decimal point shows as a ' , '. Example if I type 12,75 it shows as a 12 only. If I type 12.75 and it shows 12.75 in showResults(). { _buildResultsRow('Toplam Tutar', model.toplamTutar, linebreak: true), }

I need to convert , too . because my server accepts dot as decimal points. Any idea how to correct this?

keyboardAction: TextInputAction.done,
        keyboardType: Platform.isIOS
            ? TextInputType.numberWithOptions(decimal: true, signed: true)
            : TextInputType.numberWithOptions(decimal: true),
codegrue commented 4 years ago

I assume this will require a localization formatter inside the Double widget. If anyone know what to do I will add it.

robertoltrocha commented 4 years ago

As far as I understand programming you cannot change the format of a number. You must work with a string and then transform it into the 1000000.00 format Computers don't work with symbols inside the number, it's always just "." as a decimal separator and so you must use a mask to display it in the 1.000.000,00 format....

I use the CardSettingsCurrency class

I cloned the CardSettingsCurrency class and created a new class called CardSettingsCurrencyV2 and added the two lines below. I believe it is not an elegant solution, but it worked. I use the thousands separator '.' and decimal separator '.'

with double number, remove the symbol money and unit

`String _safeValidator(String value) { //Add this line value = value.replaceAll(widget.thousandSeparator, "").replaceAll(widget.decimalSeparator, "."); if (widget.validator == null) return null; return widget.validator(intelligentCast(value)); }

void _safeOnSaved(String value) { if (widget.onSaved == null) return; //Add this line value = value.replaceAll(widget.thousandSeparator, "").replaceAll(widget.decimalSeparator, "."); widget.onSaved(intelligentCast(value)); }

CardSettingsCurrencyV2( showMaterialonIOS: true, key: _valorKey, label: "Valor", currencyName: 'Real', currencySymbol: 'R\$', enabled: true, decimalSeparator: ",", thousandSeparator: ".", autofocus: false, contentAlign: TextAlign.left, initialValue: widget._certificadoAvulso.valor, autovalidate: false, validator: (value) { if (value == null) return 'campo é obrigatório.'; return null; }, onSaved: (value) { print('OnSaved: $value'); widget._certificadoAvulso.valor = value; }, onChanged: (value) { print('Onchange: $value'); setState(() { widget._certificadoAvulso.valor = value; }); }),`

codegrue commented 4 years ago

Thank you for posting this workaround. I'm actively trying to solve this internally to CardSettings so this hack isn't needed. By that I mean not by using the cast methods but rather so the value returns a double and not a string with delimiters.

codegrue commented 4 years ago

Note, the fix will be implemented as soon as the pattern_formatter package works correctly with international numbers.

https://github.com/hnvn/flutter_pattern_formatter/issues/7

codegrue commented 4 years ago

I committed a fix for this and Phone as well. Would you be able to test the github build before I publish?

codegrue commented 4 years ago

Fixed in version 1.9.8