Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed bug in AsYouTypeFormatter #439

Merged
merged 1 commit into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## [0.7.5]
* Fixed bug in `AsYouTypeFormatter` that could throw a `RangeError` if the user typed non digit characters

## [0.7.4]
* Updated minimum os version on iOS
* Updated dependencies
Expand Down
86 changes: 48 additions & 38 deletions lib/src/utils/formatter/as_you_type_formatter.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:math';

import 'package:flutter/services.dart';
import 'package:intl_phone_number_input/src/utils/phone_number/phone_number_util.dart';

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

final _ = newValueText
.substring(
oldValue.selection.start == -1 ? 0 : oldValue.selection.start,
newValue.selection.end == -1 ? 0 : newValue.selection.end)
.replaceAll(separatorChars, '');
int rawCursorPosition = newValue.selection.end;

int digitsBeforeCursor = 0, digitsAfterCursor = 0;

if (rawCursorPosition > 0 && rawCursorPosition <= newValueText.length) {
final rawTextBeforeCursor = newValueText
.substring(0, rawCursorPosition)
.replaceAll(separatorChars, '');
final rawTextAfterCursor = newValueText
.substring(rawCursorPosition)
.replaceAll(separatorChars, '');

digitsBeforeCursor = rawTextBeforeCursor.length;
digitsAfterCursor = rawTextAfterCursor.length;
}

String textToParse = dialCode + rawText;

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

int offset =
newValue.selection.end == -1 ? 0 : newValue.selection.end;

if (separatorChars.hasMatch(parsedText)) {
String valueInInputIndex = parsedText[offset - 1];
int newCursorPosition = 0;

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

if (offsetDifference < 2) {
if (separatorChars.hasMatch(valueInInputIndex)) {
offset += 1;
if (allowedChars.hasMatch(parsedText[startCursor])) {
if (digitsBeforeCursor > 0) {
digitsBeforeCursor--;
} else {
bool isLastChar;
try {
var _ = newValueText[newValue.selection.end];
isLastChar = false;
} on RangeError {
isLastChar = true;
}
if (isLastChar) {
offset += offsetDifference;
}
newCursorPosition = startCursor + 1;
break;
}
} else {
if (parsedText.length > offset - 1) {
if (separatorChars.hasMatch(valueInInputIndex)) {
offset += 1;
}
}

final endCursor = parsedText.length - 1 - i;

if (allowedChars.hasMatch(parsedText[endCursor])) {
if (digitsAfterCursor > 0) {
digitsAfterCursor--;
} else {
newCursorPosition = endCursor + 1;
break;
}
}
}

this.onInputFormatted(
TextEditingValue(
text: parsedText,
selection: TextSelection.collapsed(offset: offset),
),
);
}

newCursorPosition = min(max(newCursorPosition, 0), parsedText.length);

this.onInputFormatted(
TextEditingValue(
text: parsedText,
selection: TextSelection.collapsed(offset: newCursorPosition),
),
);
},
);
}

return newValue;
}

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: intl_phone_number_input
description: A simple and customizable flutter package for inputting phone number in intl / international format uses Google's libphonenumber.
version: 0.7.4
version: 0.7.5
homepage: https://github.com/natintosh/intl-phone-number-input

environment:
Expand Down