I try to check input with onChange
method in TextField
but after replacing text with TextEditingController
cursor move to start of TextField
.
This problem occurs only on the Android
platform.
Code
TextField(
controller: textEditController,
onChanged: (content) {
textEditController.text = checkNumber(content);
},)
flutter version
[✓] Flutter (Channel master, v1.2.2-pre.41, on Mac OS X 10.14.3 18D109, locale
en-IR)
[✓] Android toolchain - develop for Android devices (Android SDK version 28.0.3)
Use the SetCursorPosition method to specify where the next write operation in the console window is to begin. If the specified cursor position is outside the area that is currently visible in the console window, the window origin changes automatically to make the cursor visible.
Answers 13 : of how to set cursor position at the end of the value in flutter in textfield. You need a FocusNode and set TextSelection to place the cursor.
TextEditingController class Null safety. A controller for an editable text field. Whenever the user modifies a text field with an associated TextEditingController, the text field updates value and the controller notifies its listeners.
Set the selection using the TextEditingController
TextField(
controller: textEditController,
onChanged: (content) {
textEditController..text = checkNumber(content)
..selection = TextSelection.collapsed(offset: 0);
},
)
The accepted solution didn't work for me - as I was setting both text and selection I needed to instead set value.
The TextEditingController class documentation states:
The text or selection properties can be set from within a listener added to this controller. If both properties need to be changed then the controller's value should be set instead.
The documentation also has a relevant example that includes the following:
void initState() {
_controller.addListener(() {
final text = _controller.text.toLowerCase();
_controller.value = _controller.value.copyWith(
text: text,
selection: TextSelection(baseOffset: text.length, extentOffset: text.length),
composing: TextRange.empty,
);
});
super.initState();
}
This forces the entered text to be lower case and keeps the cursor at the end of the input.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With