Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to programmatically make keyboard textfield open

Tags:

Hi I was wondering if it was possible in flutter to programatically open up the keyboard as well as have the cursor and the textfield ready type straight away.

I already know how to pull up the keyboard FocusScope.of(context).requestFocus(FocusNode());

But I also need to know how to make the textfield ready to type without the users having to tap the textfield. as in

Lets say I have a textfield:

TextField(
  controller: textEditingController,
);

I would like to use code to the effect below so that the user dons't have to tap the textfield

textEditingController.openTextField()//Pseudo code

: Edit -----------------------------------

Bit bad of me but I forgot to add the focus node as a parameter on the textfield

Within your class add

final FocusNode _focusNode = FocusNode();

then add to textfield

TextField(
  ...
  focusNode:_focusNode,
  ...
);

then call it by running

_focusNode.requestFocus();
like image 411
GILO Avatar asked Jul 14 '20 12:07

GILO


People also ask

How do you show the keyboard automatically for a TextField in Flutter?

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.

How do you open and close the keyboard on a Flutter?

TextField is a very common widget in Flutter. When you click on the TextField it opens up the on-screen keyboard. To hide/dismiss the keyboard you have to press the back button in Android and the done button (inside the onscreen keyboard) in iOS.


1 Answers

First, you need to define FocusNode variable and assign it to your TextField like this :

//in header class
FocusNode focusNode  = FocusNode ();

//in build method
TextField(focusNode: focusNode,)

Then use this code:

FocusScope.of(context).requestFocus(focusNode);
like image 192
farouk osama Avatar answered Sep 20 '22 11:09

farouk osama