Skip to content

Commit

Permalink
Merge pull request #12 from fabricio-godoi/main
Browse files Browse the repository at this point in the history
cursor update for flutter 2.2
  • Loading branch information
LeandroNovak authored May 23, 2021
2 parents 72bcf72 + 001dc6d commit fe5dc8f
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 12 deletions.
6 changes: 3 additions & 3 deletions example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ packages:
name: async
url: "https://pub.dartlang.org"
source: hosted
version: "2.5.0"
version: "2.6.1"
boolean_selector:
dependency: transitive
description:
Expand Down Expand Up @@ -99,7 +99,7 @@ packages:
name: source_span
url: "https://pub.dartlang.org"
source: hosted
version: "1.8.0"
version: "1.8.1"
stack_trace:
dependency: transitive
description:
Expand Down Expand Up @@ -134,7 +134,7 @@ packages:
name: test_api
url: "https://pub.dartlang.org"
source: hosted
version: "0.2.19"
version: "0.3.0"
typed_data:
dependency: transitive
description:
Expand Down
14 changes: 9 additions & 5 deletions lib/src/masked_text_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,9 @@ class MaskedTextController extends TextEditingController {

previousMask = mask;
text = _lastUpdatedText;
_moveCursor(newCursor);
// To avoid concurrent cursor update uppon text update, it is desired
// to delay this update to ensure that the update occurs correctly
Future.delayed(Duration.zero).then((value) => _moveCursor(newCursor));
}

/// Moves cursor to the end of the text
Expand Down Expand Up @@ -251,14 +253,16 @@ class MaskedTextController extends TextEditingController {
// find new cursor position based on new mask
var countDown = oldUnmaskCursor + newChars;
var maskCount = 0;
for (var i = 0; i < newText.length && i < newMask.length; i++) {
for (var i = 0;
i < newText.length && i < newMask.length && countDown >= 0;
i++) {
if (!translator.containsKey(newMask[i])) {
maskCount++;
} else if (--countDown < 0) {
break;
} else {
countDown--;
}
}

return oldUnmaskCursor + maskCount + max<int>(newChars, 0);
return oldUnmaskCursor + maskCount + newChars;
}
}
6 changes: 3 additions & 3 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ packages:
name: async
url: "https://pub.dartlang.org"
source: hosted
version: "2.5.0"
version: "2.6.1"
boolean_selector:
dependency: transitive
description:
Expand Down Expand Up @@ -92,7 +92,7 @@ packages:
name: source_span
url: "https://pub.dartlang.org"
source: hosted
version: "1.8.0"
version: "1.8.1"
stack_trace:
dependency: transitive
description:
Expand Down Expand Up @@ -127,7 +127,7 @@ packages:
name: test_api
url: "https://pub.dartlang.org"
source: hosted
version: "0.2.19"
version: "0.3.0"
typed_data:
dependency: transitive
description:
Expand Down
4 changes: 3 additions & 1 deletion test/extended_masked_text_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ void main() {
expect(controller.text, '1234 **** **** 5678');
});

test('check cursor position after edit in specifc possition', () {
test('check cursor position after edit in specifc possition', () async {
final cpfController = MaskedTextController(
text: '12345678901',
mask: '000.000.000-00',
Expand All @@ -90,7 +90,9 @@ void main() {
cpfController.selection = TextSelection.fromPosition(
const TextPosition(offset: 1),
);

cpfController.text = '19345678901';
await Future.delayed(Duration.zero);
expect(cpfController.selection.baseOffset, 2);
});

Expand Down

0 comments on commit fe5dc8f

Please sign in to comment.