Skip to content

Commit

Permalink
Remove prevent changes in the middle of value.(#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
gtgalone committed Jun 20, 2022
1 parent a60e273 commit 07441a2
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 16 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,7 @@
# 2.1.6

* Add code to prevent changes in the middle of value.

# 2.1.7

* Remove prevent changes in the middle of value.(#34) Huge Thanks! @yoseptara
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ https://pub.dev/packages/currency_text_input_formatter
### Add pubspec.yaml
``` yaml
dependencies:
currency_text_input_formatter: ^2.1.6
currency_text_input_formatter: ^2.1.7
```
### Solving Intl package conflict
Add this code end of pubspec.yaml.
Expand Down
2 changes: 1 addition & 1 deletion example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ packages:
path: ".."
relative: true
source: path
version: "2.1.6"
version: "2.1.7"
fake_async:
dependency: transitive
description:
Expand Down
14 changes: 1 addition & 13 deletions lib/currency_text_input_formatter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ class CurrencyTextInputFormatter extends TextInputFormatter {
num _newNum = 0;
String _newString = '';
bool _isNegative = false;
int _currentOffset = 0;

void _formatter(String newText) {
final NumberFormat format = NumberFormat.currency(
Expand Down Expand Up @@ -120,14 +119,6 @@ class CurrencyTextInputFormatter extends TextInputFormatter {
_isNegative = newValue.text.startsWith('-');
String newText = newValue.text.replaceAll(RegExp('[^0-9]'), '');

// If users try to insert value in the middle of value,
// It will not be able and the cursor moves to the end of value.
if (oldValue.selection.baseOffset != _currentOffset) {
return oldValue.copyWith(
selection: TextSelection.collapsed(offset: _currentOffset),
);
}

// If the user wants to remove a digit, but the last character of the
// formatted text is not a digit (for example, "1,00 €"), we need to remove
// the digit manually.
Expand All @@ -139,18 +130,15 @@ class CurrencyTextInputFormatter extends TextInputFormatter {
_formatter(newText);

if (newText.trim() == '' || newText == '00' || newText == '000') {
_currentOffset = _isNegative ? 1 : 0;
return TextEditingValue(
text: _isNegative ? '-' : '',
selection: TextSelection.collapsed(offset: _isNegative ? 1 : 0),
);
}

_currentOffset = _newString.length;

return TextEditingValue(
text: _newString,
selection: TextSelection.collapsed(offset: _currentOffset),
selection: TextSelection.collapsed(offset: _newString.length),
);
}

Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: currency_text_input_formatter
description: Currency Text Input Formatter for Flutter. Use it easy and simple for your flutter app.
version: 2.1.6
version: 2.1.7
homepage: https://github.com/gtgalone/currency_text_input_formatter

environment:
Expand Down

0 comments on commit 07441a2

Please sign in to comment.