Flt
Flt

Reputation: 91

Flutter TextFormField Cursor Reset To First position of letter after onChanged

I need some help regarding flutter textformfield This is my code for the textfield controller. The problem is when I type new word,the cursor position is moved automatically from right to left (reset)(before first letter inside box). How I can make the cursor work as usual at the end of current text. I have read few solutions from stack overflow but it 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;
            },
          ),
        ],
      ),
    );
  }

Upvotes: 3

Views: 1424

Answers (1)

Nedim Karavdic
Nedim Karavdic

Reputation: 252

yourController.text = yourString;

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

Upvotes: 2

Related Questions