I have a simple flutter app where when I tap on the search icon, it should show me CupertinoSearchTextField() with the keyboard.

The good thing is that CupertinoSearchTextField() is showing when I tap the search icon, as you can see in the 2nd image, but the problem is that the keyboard does not pop up when I tap the search icon.

The search text field and the keyboard must show together so that the user can type directly.

-> Keep in mind, the user can still type, but user has to tap into the search field to activate the keyboard.

This is what my code looks like -

iOSAppBar(BuildContext context) {
    return CupertinoNavigationBar(
      backgroundColor: AppColors.blue,
      middle: state ? appBarTitle() : appBarSearchBox(),
      trailing: state ? appBarSearchIcon() : appBarCancelText(),
    );
  }

//this is the search field
Widget appBarSearchBox() {
    return CupertinoSearchTextField();
  }

enter image description here enter image description here


Solution 1: Lars

Fist, create a focus node like this:

FocusNode yourFocusNode = FocusNode();

Then, add it to you CupertinoTextField

CupertinoSerchTextField(focusNode: yourFocusNode)

Then you can call

FocusScope.of(context).requestFocus(yourFocusNode);

or, if that does not work, try

yourFocusNode.requestFocus();

anywhere in your code to request focus. Don't forget to dispose your focusNode:

@override
void dispose() {
  myFocusNode.dispose();

  super.dispose();
}