I have a TextField which has a default text in it from the beginning. I want the cursor to be placed at the end of the existing text when the TextField receives focus (possibly with the condition that the text should not have already been modified from the default text).
I've tried setting the controller.selection which didn't work. I also tried using a FocusNode:
focusNode: FocusNode()..addListener((){
if( !record.isCommentModified )
record.textEditingController.selection = TextSelection.collapsed(offset: record.comment.length);
})
It does work, but one side effect is that multiple textfields look like they have focus at the same time. How can I prevent this? Or even better, is there a better way of achieving what I want?
If there's a value set on a TextField, by default the cursor is positioned at the end of the text/value when focused. Let me know if the behavior you've encountered was different.
TextFields need to have individual FocusNode. Since you're using a single FocusNode for multiple, once one TexField is focused it appears that there are multiple TextFields in focus.
As for tracking which TextField contains text, individual TextEditingController is needed as well. What I can suggest is to create a List of FocusNode and TextEditingController for the TextFields.
late List<FocusNode> focusNodeList;
late List<TextEditingController> textController;
#override
void initState() {
super.initState();
// Initialize both List
textController = List<TextEditingController>.generate(
length, (index) => TextEditingController());
focusNodeList = List<FocusNode>.generate(length, (index) {
var focusNode = FocusNode();
focusNode.addListener(() {
// Do something
});
return focusNode;
});
}
and set them on your TextField
TextField(
controller: textController[0],
focusNode: focusNodeList[0],
),
TextField(
controller: textController[1],
focusNode: focusNodeList[1],
),
...
Related
I have used focusNode to focus cursor on text form field. But I defined global textformfield widget. but used focus node in single screen only. Now the problem is automatically all the field cursor gets activated. because of that common focusnode in widget. How we can set that only in slogin screen focus node will work or else that condition will avoid.
FocusNode textFocusOnFirstField = FocusNode();
TextFormField(
// here use the variable in all the TextFormField
// enabled: isEnable, // here use the variable in all the TextFormField
focusNode: textFocusOnFirstField ,
readOnly: readOnly,
FocusScope.of(context).unfocus();
Use this in initState() so that it unfocuses all the textfield as soon as screen loads.
I have created a function which return 4 textfields in column and I have called that function from 4 different containers, now how do I know from which container textfields are being changed because all the textfields are common to different containers. Thanks in advance.
in general you can handle the changes in Textfield in two way
1- Using onChanged() callback
TextField(
onChanged: (text) {
print('First text field: $text');
},
),
2- Using a TextEditingController
you create a controller
final myController = TextEditingController();
Connect the TextEditingController to a text field.
TextField(
controller: myController,
),
Create a function to print the latest value.
void _printLatestValue() {
print('Second text field: ${myController.text}');
}
Listen to the controller for changes.
#override
void initState() {
super.initState();
// Start listening to changes.
myController.addListener(_printLatestValue);
}
in your case you can pass controllers to your function and listen to it
What about using keys. You can choose unique keys for each of the different textfields(maybe you can generate keys when you programmatically generate the textfields). Alternatively, you can assign unique keys to the containers as well.
flutter textfield has onChanged property, you can do inside onChanged
As you know, it is a basic requirement that; after the user makes an entry in an Autocomplete field (either by entering a new value or selecting an existing value from the dropdown list) and then press a 'Submit' button or 'Delete' button (say, to update the database); the old entry in the TextFormField should be cleared automatically for the next entry.
How can this be programmatically done in a simple way (for example, like Autocomplete.TextFormField.clear ) in Flutter?
I have tried several ways, but am unable to access/ modify the TextEditingController from an outside function.
Thank you in advance for any advice, please!
I am posting this for the benefit of whoever who reads this post, having a similar requirement.
A solution lies with flutter_typeahead widget, found here:
https://pub.dev/packages/flutter_typeahead
It could be used instead of RawAutocomplete or Autocomplete widgets.
You can use a RawAutocomplete to access the text editing controller from outside the Autocomplete. The RawAutocomplete is similar to Autocomplete, but it also provides the textEditingController and focusNode properties.
Note that if you pass in a textEditingController, you must also provide a non-null focusNode and fieldViewBuilder.
An example of modifying the textEditingController from outside the Autocomplete can be seen below.
final _controller = TextEditingController();
final _focusNode = FocusNode();
Widget build(BuildContext context) {
return RawAutocomplete<String>(
textEditingController: _controller,
focusNode: _focusNode,
fieldViewBuilder: (context, textEditingController, focusNode, onFieldSubmitted) =>
TextFormField(controller: _controller),
...
)};
//Then in your outside function
setState(() {
_controller.clear();
};
I have a page that contains multiple elements and it has a TextField widget and I want to know if I moved to another element to do something. Is there an event that is triggered when moving from one element to another? I tried onSubmitted but it didn't fulfill my request.
You need to use FocusNode
Declare your FocusNode : final FocusNode focusNode;
Pass it to TextField : TextField( ..., focusNode: focusNode, ... )
Add listener to FocusNode :
focusNode.addListener(() {
print("${focusNode.hasFocus}");
});
FocusNode has hasFocus , true means TextField has focus
I have a TextField in Flutter of which I want to automatically select the text and show the keyboard.
I can select the text through a TextEditingController, but even with a FocusNodes requestFocus the keyboard isn't shown, when the Widget opens.
How to automatically open the keyboard for a TextField?
You can use the autofocus:true property of the TextField:
Whether this text field should focus itself if nothing else is already focused.
So whenever the widget appears on screen, if theres nothing else with the keyboard focus, the focus will automatically be directed to it, thus opening the keyboard.
TextField(TextEditingController: controller,
focusNode: focusNode,
autofocus:true)
You can set the autofocus property on TextField to true:
TextField(
autofocus: true,
);
Hope it helps!
class yourWidget extends StatelessWidget {
FocusNode inputNode = FocusNode();
// to open keyboard call this function;
void openKeyboard(){
FocusScope.of(context).requestFocus(inputNode)
}
#override
Widget build(BuildContext context) {
TextFormField(
//assign the node like this
focusNode: inputNode,
autofocus:true,)
}
I have done this just using:
autofocus: true,
But if you want more control over your TextField / TextFormFeild keyboard you can use:
1. First declare a focus node object:
FocusNode focusNode = FocusNode(); // declear a focusNode object
2. On TextFeild / TextFormFeild, just do like below:
focusNode: focusNode, // assign focusNode object on focusNode value
autofocus: true, // make autofocus true for first auto open keyboard
3. Just call this function when you want to open your keyboard:
void openKeyboard () {
FocusScope.of(context).requestFocus(inputNode);
}
This is an example of how you can use it. Using that format you can open the keyboard automatically / you have complete control over whether or not you need to open the keyboard.
I hope this will fix your issue.