Skip to content

Commit eaae5e1

Browse files
authored
Merge pull request #439 from GauteHaugen/develop
Fixed bug in AsYouTypeFormatter
2 parents 99c726e + 123d418 commit eaae5e1

File tree

3 files changed

+52
-39
lines changed

3 files changed

+52
-39
lines changed

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
## [0.7.5]
2+
* Fixed bug in `AsYouTypeFormatter` that could throw a `RangeError` if the user typed non digit characters
3+
14
## [0.7.4]
25
* Updated minimum os version on iOS
36
* Updated dependencies

lib/src/utils/formatter/as_you_type_formatter.dart

+48-38
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import 'dart:math';
2+
13
import 'package:flutter/services.dart';
24
import 'package:intl_phone_number_input/src/utils/phone_number/phone_number_util.dart';
35

@@ -38,61 +40,69 @@ class AsYouTypeFormatter extends TextInputFormatter {
3840
if (newValueLength > 0 && newValueLength > oldValueLength) {
3941
String newValueText = newValue.text;
4042
String rawText = newValueText.replaceAll(separatorChars, '');
41-
String textToParse = dialCode + rawText;
4243

43-
final _ = newValueText
44-
.substring(
45-
oldValue.selection.start == -1 ? 0 : oldValue.selection.start,
46-
newValue.selection.end == -1 ? 0 : newValue.selection.end)
47-
.replaceAll(separatorChars, '');
44+
int rawCursorPosition = newValue.selection.end;
45+
46+
int digitsBeforeCursor = 0, digitsAfterCursor = 0;
47+
48+
if (rawCursorPosition > 0 && rawCursorPosition <= newValueText.length) {
49+
final rawTextBeforeCursor = newValueText
50+
.substring(0, rawCursorPosition)
51+
.replaceAll(separatorChars, '');
52+
final rawTextAfterCursor = newValueText
53+
.substring(rawCursorPosition)
54+
.replaceAll(separatorChars, '');
55+
56+
digitsBeforeCursor = rawTextBeforeCursor.length;
57+
digitsAfterCursor = rawTextAfterCursor.length;
58+
}
59+
60+
String textToParse = dialCode + rawText;
4861

4962
formatAsYouType(input: textToParse).then(
5063
(String? value) {
5164
String parsedText = parsePhoneNumber(value);
5265

53-
int offset =
54-
newValue.selection.end == -1 ? 0 : newValue.selection.end;
55-
56-
if (separatorChars.hasMatch(parsedText)) {
57-
String valueInInputIndex = parsedText[offset - 1];
66+
int newCursorPosition = 0;
5867

59-
if (offset < parsedText.length) {
60-
int offsetDifference = parsedText.length - offset;
68+
if (digitsBeforeCursor > 0 || digitsAfterCursor > 0) {
69+
for (var i = 0; i < parsedText.length; i++) {
70+
final startCursor = i;
6171

62-
if (offsetDifference < 2) {
63-
if (separatorChars.hasMatch(valueInInputIndex)) {
64-
offset += 1;
72+
if (allowedChars.hasMatch(parsedText[startCursor])) {
73+
if (digitsBeforeCursor > 0) {
74+
digitsBeforeCursor--;
6575
} else {
66-
bool isLastChar;
67-
try {
68-
var _ = newValueText[newValue.selection.end];
69-
isLastChar = false;
70-
} on RangeError {
71-
isLastChar = true;
72-
}
73-
if (isLastChar) {
74-
offset += offsetDifference;
75-
}
76+
newCursorPosition = startCursor + 1;
77+
break;
7678
}
77-
} else {
78-
if (parsedText.length > offset - 1) {
79-
if (separatorChars.hasMatch(valueInInputIndex)) {
80-
offset += 1;
81-
}
79+
}
80+
81+
final endCursor = parsedText.length - 1 - i;
82+
83+
if (allowedChars.hasMatch(parsedText[endCursor])) {
84+
if (digitsAfterCursor > 0) {
85+
digitsAfterCursor--;
86+
} else {
87+
newCursorPosition = endCursor + 1;
88+
break;
8289
}
8390
}
8491
}
85-
86-
this.onInputFormatted(
87-
TextEditingValue(
88-
text: parsedText,
89-
selection: TextSelection.collapsed(offset: offset),
90-
),
91-
);
9292
}
93+
94+
newCursorPosition = min(max(newCursorPosition, 0), parsedText.length);
95+
96+
this.onInputFormatted(
97+
TextEditingValue(
98+
text: parsedText,
99+
selection: TextSelection.collapsed(offset: newCursorPosition),
100+
),
101+
);
93102
},
94103
);
95104
}
105+
96106
return newValue;
97107
}
98108

pubspec.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: intl_phone_number_input
22
description: A simple and customizable flutter package for inputting phone number in intl / international format uses Google's libphonenumber.
3-
version: 0.7.4
3+
version: 0.7.5
44
homepage: https://github.com/natintosh/intl-phone-number-input
55

66
environment:

0 commit comments

Comments
 (0)