moveSeparator method
Implementation
MoveSeparatorResult moveSeparator(
String oldText,
String newText,
TextSelection newValueTextSelection,
) {
final int initialSeparatorPosition = oldText.indexOf(_decimalSeparator);
List<int> separatorPositions = newText.indexesOf(_decimalSeparator);
int oldBaseSelectionPosition = newValueTextSelection.baseOffset;
int oldExtentSelectionPosition = newValueTextSelection.extentOffset;
// If there is more than one separator, only keep the first one
if (separatorPositions.length > 2) {
newText = newText.replaceAllIgnoreFirst(_decimalSeparator, '');
separatorPositions = newText.indexesOf(_decimalSeparator);
}
if (separatorPositions.length == 2) {
// Move to the new separator
if (separatorPositions[0] == initialSeparatorPosition) {
newText = newText.removeCharacterAt(initialSeparatorPosition);
// Move the cursor to the new position
if (oldBaseSelectionPosition == oldExtentSelectionPosition &&
separatorPositions[1] == (oldExtentSelectionPosition - 1)) {
oldBaseSelectionPosition--;
oldExtentSelectionPosition--;
}
} else {
newText = newText.removeCharacterAt(separatorPositions[1]);
}
}
return MoveSeparatorResult(
newText: newText,
newBasePosition: oldBaseSelectionPosition,
newExtentPosition: oldExtentSelectionPosition,
);
}