Created
August 9, 2022 06:23
-
-
Save demirdev/da90aa37f079263f111dec328aca06e6 to your computer and use it in GitHub Desktop.
Custom Tousands Formatter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/services.dart'; | |
import 'package:intl/intl.dart'; | |
import 'package:pattern_formatter/pattern_formatter.dart'; | |
/* | |
Hello #flutter #developers, I used pattern_formatter package | |
to format prices written in TextFormField. | |
When user types 0,0 then value formatted to 0 immediately. | |
For fix this issue I wrote CustomThousandsFormatter. | |
*/ | |
class CustomThousandsFormatter extends ThousandsFormatter { | |
CustomThousandsFormatter( | |
{NumberFormat? formatter, bool allowFraction = false}) | |
: super(formatter: formatter, allowFraction: allowFraction); | |
@override | |
TextEditingValue formatEditUpdate( | |
TextEditingValue oldValue, TextEditingValue newValue) { | |
// dont thouch these inputs | |
final _skipValues = [ | |
'0${numberFormat.symbols.DECIMAL_SEP}0', // 0,0 | |
'0${numberFormat.symbols.DECIMAL_SEP}00', // 0,00 | |
'0${numberFormat.symbols.DECIMAL_SEP}000', // 0,000 | |
'0${numberFormat.symbols.DECIMAL_SEP}0000', // 0,0000 | |
]; | |
if (_skipValues.contains(newValue.text)) { | |
return newValue; | |
} | |
return super.formatEditUpdate(oldValue, newValue); | |
} | |
} | |
final numberFormat = NumberFormat.decimalPattern('tr_TR'); | |
final customThousandsFormatter = | |
CustomThousandsFormatter(formatter: numberFormat, allowFraction: true); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment