'Flutter TextFormField Cursor Reset To First position of letter after onChanged

I need some help regarding flutter textformfield This is my code on textfield controller function. The problem is when I type new word, cursor position will move from right to left (reset)(before first letter inside box). How I can make cursor work as usual at the end of current text. I have read few solution from stack overflow and still not working. Please help me. Thanks.

class BillingWidget extends StatelessWidget {
  final int pageIndex;
  final Function validateController;

  final formKey = new GlobalKey<FormState>();
  BillingWidget(this.billingDetails,this.pageIndex,this.validateController);

  final BillingDetails billingDetails;

  @override
  Widget build(BuildContext context) {
    return Form(
      key: formKey,
      onChanged: () {
        if (formKey.currentState.validate()) {
          validateController(pageIndex,false);
          formKey.currentState.save();

          final val = TextSelection.collapsed(offset: _textTEC.text.length);
          _textTEC.selection = val;
        }
        else {
          //prevent procced to next page if validation is not successful
          validateController(pageIndex,true);
        }
      },
      child: Column(
        children: [
          Padding(
            padding: const EdgeInsets.only(top: 20,bottom: 0),
            child: Align(
              alignment: Alignment.centerLeft,
              child: Text(
                "Maklumat Pembekal",
                textAlign: TextAlign.left,
                style: TextStyle(
                  decoration:TextDecoration.underline,
                  fontWeight: FontWeight.bold,
                  fontSize: 16,
                  color: Colors.grey.shade700,
                ),
              ),
            ),
          ),
          TextFormField(
            controller: billingDetails.companyNameTxtCtrl,
            maxLength: 30,
            decoration: InputDecoration(labelText: "Nama Syarikat"),
            validator: (String value) {
              return value.isEmpty ? 'Nama Syarikat Diperlukan' : null;
            },
            onSaved: (String value) {
              billingDetails.companyName = value;
              billingDetails.companyNameTxtCtrl.text = billingDetails.companyName;
            },
          ),
          TextFormField(
            controller: billingDetails.addressLine1TxtCtrl,
            maxLength: 30,
            decoration: InputDecoration(labelText: "Alamat Baris 1"),
            validator: (String value) {
              return value.isEmpty ? 'Alamat Baris tidak boleh kosong.' : null;
            },
            onSaved: (String value) {
              billingDetails.addressLine1 = value;
              billingDetails.addressLine1TxtCtrl.text = billingDetails.addressLine1;
            },
          ),
          TextFormField(
            controller: billingDetails.addressLine2TxtCtrl,
            maxLength: 30,
            decoration: InputDecoration(labelText: "Alamat Baris 2"),
            onSaved: (String value) {
              billingDetails.addressLine2 = value;
              billingDetails.addressLine2TxtCtrl.text = billingDetails.addressLine2;
            },
          ),
          TextFormField(
            controller: billingDetails.addressLine3TxtCtrl,
            maxLength: 30,
            decoration: InputDecoration(labelText: "Alamat Baris 3"),
            onSaved: (String value) {
              billingDetails.addressLine3 = value;
              billingDetails.addressLine3TxtCtrl.text = billingDetails.addressLine3;
            },
          ),
        ],
      ),
    );
  }


Solution 1:[1]

yourController.text = yourString;

yourController.selection = TextSelection.fromPosition(TextPosition(offset: yourController.text.length));

Solution 2:[2]

Do something like below

class TextEditingControllerWithCursorPosition extends TextEditingController {
  TextEditingControllerWithCursorPosition({String text}): super(text: text);

  @override
  set text(String newText) {
    value = value.copyWith(
      text: newText,
       selection: TextSelection.collapsed(offset: newText.length),
       //selection: TextSelection(baseOffset: newText.length, extentOffset: newText.length)); // For latest SDK
      composing: TextRange.empty,
    );
  }
}

And initialize your custom text controller

TextEditingController myCustomController = TextEditingControllerWithCursorPosition();

Usage

myCustomController.setTextAndSelection("your text");

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 General Grievance
Solution 2 Paresh Mangukiya