Hello friends,
I'm going to show you how I resolved the selection/caret updating issue for Flutter TextField when cursor goes to the very beginning instead of the position you specify.
I was creating a money formating text field and faced this issue as many of us and here is how I resolved it:
String odlValue="";
@override
void initState() {
tbPurchasePrice.addListener((){
var cursor = tbPurchasePrice.selection;
String val = tbPurchasePrice.text;
if((odlValue.length < val.length && val.length > 3) ||
(odlValue.length > val.length && val.length > 4)){
val = val.replaceAll(",", "");
val = val.substring(0, val.length - 3) + "," + val.substring(val.length - 3);
int d = val.length - tbPurchasePrice.text.length;
odlValue = val;
tbPurchasePrice.text = val;
Future.delayed(
Duration(milliseconds: 5),
() {
tbPurchasePrice.selection = new TextSelection(
baseOffset: cursor.baseOffset + d,
extentOffset: cursor.extentOffset + d,
affinity: cursor.affinity, isDirectional: cursor.isDirectional);
});
}
else if(odlValue.length > val.length && val.length <= 4 && val.indexOf(",") >= 0){
tbPurchasePrice.text = val.replaceAll(",", "");
odlValue = val;
Future.delayed(
Duration(milliseconds: 5),
() {
tbPurchasePrice.selection = new TextSelection(
baseOffset: cursor.baseOffset - 1,
extentOffset: cursor.extentOffset - 1,
affinity: cursor.affinity, isDirectional: cursor.isDirectional);
});
}
odlValue = val;
});
super.initState();
}
.......
child: TextFormField(
decoration: InputDecoration( prefix: Text(r"$")),
controller: tbPurchasePrice,
keyboardType: TextInputType.numberWithOptions(decimal: true),
style: Theme.of(context).textTheme.headline,
)
.......
so it puts $ sign on the beginning, then it puts comma in the first thousand place and it use delayed function call to set/update the selection property so it would not go to the beginning of the textfield but to the same place where it has been before text updating or where you need it to be.
Thank you and see you ;)

1vqHSTrq1GEoEF7QsL8dhmJfRMDVxhv2y