Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to move the focus to the TagTextField when a button is pressed #82

Open
ektay opened this issue Mar 5, 2024 · 1 comment
Open

Comments

@ektay
Copy link

ektay commented Mar 5, 2024

I have a button, on pressing the button the focus should go to the TextFieldTags so that the cursor blinks on the textfield. I tried creating a focus node and passed that focus note to the input field controller , that brings the focus to the TextFieldTags as soon button is pressed but it doesn't work as expected because the TextFieldTags is using its own focus node. How do I get the focus on the textfieldTags so that the cursor blinks on the textfield on a button press.
Here is the code snippet :
Stack(
children: [
TagsList(callback: (tags) {
selectedTagsList = tags;
},
focusNode: _tagsFocusNode,
),
if(_showAddTagButton)
APButton(
fixedSize: Size(100, 30),
onPressed: () {
setState(() {
_showAddTagButton = false;
});
_tagsFocusNode.requestFocus();
},
child: Text(
'+ Add Tag',
),
),
],
),

In the TextTagField I want to get the focus here is the code - child: TextFieldTags(
textEditingController: ttec,
focusNode: tfn,
textfieldTagsController: _controller,
textSeparators: const [' '],
letterCase: LetterCase.normal,
validator: (String tag) {
if (tagsList.contains(tag)) {
return null; // Allow the tag to be created
} else {
return 'Invalid Tag'; // Prevent the tag from being created
}
},
inputfieldBuilder:
(context, tec, fn, error, onChanged, onSubmitted) {
return ((context, sc, tags, onTagDelete) {
return Padding(
padding: const EdgeInsets.symmetric(horizontal: 5.0),
child: TextField(
controller: tec,
focusNode: fn,
......

@ektay ektay changed the title How to show the cursor on the TagTextField when a button is pressed How to move the focus to the TagTextField when a button is pressed Mar 5, 2024
@eyoeldefare
Copy link
Owner

eyoeldefare commented Mar 6, 2024

Use the controller. Eg: _myTagController.getFocusNode?.requestFocus();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants