Home > Software design >  Can I disable the automatic keyboard pop-up of the SearchDelegate Class?
Can I disable the automatic keyboard pop-up of the SearchDelegate Class?

Time:12-14

Im using the SearchDelegate Flutter class. It automatically opens the keyboard when I use the showSearch Method.

I figured that I could close it right away within one of the overwritten Methods. But I don't want it to pop-up in the first place. Only when taping the search bar.

I know that the SearchDelegate class has a _focusNode attribute but I don't know how to work with it.

Any ideas? Thanks in advance.

CodePudding user response:

As of today (december 2022), going strictly by your question : no, you can't.

Dart fields with a leading underscore _ are not just a naming convention, they allow to mark fields as private. Such fields will only be available to :

  • The .dart file, when declared at top-level.
  • The parent Dart object, when declared at scope-level.

Thus, you may not access the _focusNode field, nor the _SearchPageRoute<T>, _SearchPage<T> or _SearchPageState<T> classes.

In conclusion, you can either deal with this limitation or create your own custom implementation of this search page. You could either build it from scratch, or by forking the existing showSearch implementation and modifying to fit your needs.

CodePudding user response:

You can use the AutofocusScope widget to prevent the keyboard from automatically opening when the showSearch method is called. This widget prevents any children within it from being focused automatically.

Here's an example of how you can use it:

AutofocusScope(
  child: showSearch(
    context: context,
    delegate: MySearchDelegate(),
  ),
),

You can then focus the search bar manually by using the FocusNode that is provided to the buildSuggestions and buildResults methods of the SearchDelegate class. You can use the FocusScope.of(context).requestFocus(focusNode) method to focus the search bar.

Here's an example of how you can do that:

@override
Widget buildSuggestions(BuildContext context) {
  // Focus the search bar when the suggestions are shown.
  FocusScope.of(context).requestFocus(focusNode);

  // Build the suggestions...
}

I hope that helps! Let me know if you have any other questions.

  • Related