skip to Main Content

I know how to manually make the keyboard go away but that’s now what I want, let me explain:

I have a BottomNavigationBar using which I can switch between 5 screens.

This is the code for the body:

body: GetBuilder<MainTabBarController>(builder: (_) {
        return IndexedStack(
          index: controller.pageIndex,
          children: controller.tabPages,
        );
      }),

This is tabPages in the GetxController:

List<Widget> tabPages = [
    HomeScreen(),
    SearchScreen(),
    const Placeholder(color: Colors.orange),
    const Placeholder(color: Colors.green),
    const Placeholder(color: Colors.indigo),
  ];

Now the thing is, my SearchScreen has a textfield with autofocus set to true, so that whenever I open the SearchScreen, the keyboard should popup automatically. But the problem that I am having here is that, as soon as the "tabPages" is initializes (which happens after the login as I come to the MainTabBarScreen() ), the SearchScreen also gets initialized and it brings up the keyboard even though I am only on the HomeScreen.

I hope I properly explained the issue here, let me know if anymore information is needed. Thank you!

2

Answers


  1. Chosen as BEST ANSWER

    I removed the autofocus: true from the search-textfield and I added a focusNode to it which only gives the focus to the textfield once I select the search-screen from the bottom-navigation-bar


  2. The following code helped me to hide the keyboard.

      @override
      void initState() {
        FocusManager.instance.primaryFocus?.unfocus();
        super.initState();
      }
    

    Unfocused from current focus so that keyboard will dismiss.

    You enter the above code in other screens except for SearchScreen().

    Extra

    GestureDetector(
          onTap: () {
            
            FocusScopeNode currentfocus = FocusScope.of(context);
           
            if (!currentfocus.hasPrimaryFocus &&
                currentfocus.focusedChild != null) {
             
              FocusManager.instance.primaryFocus?.unfocus();
            }
          },
          child: MaterialApp(),
        )
    

    Warp the MaterialApp with GestureDetector and enter the above code

    I hope your problem will be solved. Thank you!

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search