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
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-barThe following code helped me to hide the keyboard.
Unfocused from current focus so that keyboard will dismiss.
You enter the above code in other screens except for
SearchScreen()
.Extra
Warp the
MaterialApp
withGestureDetector
and enter the above codeI hope your problem will be solved. Thank you!