title: Flutter: Cursor Position Shift to left in TextField after SetState is called
body: <p>I have a textField in which if there is no value then border color changes to red after the user put value I have called the setState method to change the border color to grey again.</p>
  <p>but as soon as the user put a value and setState is called the cursor position shift to the beginning or left, I have tried many approaches to correct it but nothing works.</p>
  <p>Code to my textField :-</p>
  <pre><code>TextField(
                controller: textEditingController,
                onChanged: (text) {
                  //print(textEditingController.selection);
  
                  if (text.length == 0) {
                    addbuttonDisable = true;
                    firstTime = true;
                    setState(() {
                      _validate = true;
                      currentIndexCheck = index;
                    });
                  }
                  if (text.length &gt;= 1) {
                    addbuttonDisable = false;
                  }
                  if (text.length &gt;= 1 &amp;&amp; firstTime) {       &lt;&lt;== this is the condition after which cursor shift to left.
                    setState(() {
                      _validate = false;
                      firstTime = false;
                      });
                    textEditingController.value.copyWith(  &lt;&lt;== approach i have tried to change cursor positon.
                        selection: TextSelection.fromPosition(
                            TextPosition(offset: text.length+2)));
  
                  }
                  if (lstOfStrings.length &gt; index) {
                    lstOfStrings.removeAt(index);
                    lstOfStrings.insert(index, text);
                  } else {
                    lstOfStrings.add(text);
                  }
                },
                decoration: InputDecoration(
                  //errorText: _validate? &quot;value can't be null&quot;: null,
                  border: InputBorder.none,
                  contentPadding: EdgeInsets.only(left: 2, right: 2, bottom: 2.5),
                  isDense: true,
                ),
                maxLines: 1,
              ),
  </code></pre>
  <p>if the condition which causes the problem is commented everything works. Please suggest me a solution or if I am doing something wrong here. What is want is that the cursor remains to end of user inputs words.</p>