How to avoid focusnode use in textformfield in flutter? - flutter

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.

Related

How to clear the TextFormField externally in an Autocomplete widget?

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();
};

Flutter How do I know if the TextField has been (lose focus , focus out) or not?

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

How to show the Keyboard automatically for a Textfield in Flutter

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.

I want to get the value from a TextFormField when i press a RaisedButton in flutter

TextFormField has it's own property to perform actions, called "onFieldSubmited".
But i want to get the value from the TextFormField, when i press a RaisedButton or something.
How can i do it?
All you need is a TextEditingController
There is a cookbook for this in flutter's own website. Wonder how you missed it.
Use TextEditingController to get the value of a specific TextField.
https://flutter.dev/docs/cookbook/forms/text-field-changes
[...]
TextEditingController controllername = TextEditingController();
[...]
TextField(
controller: controllername, //...Rest of the code
)
Use the controllername.value to get the current value of a Textfield for that particular moment.

Position cursor at end of TextField when focused?

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],
),
...