I'm trying to create a mention feature, so I have extended the TextEditingController class for styling the entered text - change the color to blue and replace the entered string to the contact's name.
I'm facing a problem with the cursor when changing the text (setting a different text in TextSpan). If I enter a string with the exact length as the original one, the cursor acts normally, but if I enter a string with a smaller or longer length, the cursor doesn't follow the entered text, but the original one.
Example:
If the original text was @John
and I have replaced it to John Snow
, the cursor is located before S
and not after W
.
@override
TextSpan buildTextSpan({TextStyle style, bool withComposing}) {
var children = <InlineSpan>[];
text.splitMapJoin(
mentionPattern,
onMatch: (Match match) {
children.add(
TextSpan(
text: "Text replacer",
style: style.merge(MainStyle.LinkSmallTextStyle),
),
);
},
onNonMatch: (String text) {
children.add(TextSpan(text: text, style: style));
return '';
},
);
return TextSpan(style: style, children: children);
}