ElevatedButton(
onPressed: () {
// This code will hide the mobile keyboard screen
FocusManager.instance.primaryFocus?.unfocus();
},
child: const Text('Hide Keyboard'),
)
The above code can be used if you are using Flutter version 2 or above. It is written with null safety that will dismiss the keyboard when executed on mobile devices.
We have created a textbox and a button where the keyboard screen will be seen on focus and when you click on the 'hide keyboard' button the keyboard screen will hide.
The code that can be used to hide the keyboard screen is as below.
FocusManager.instance.primaryFocus?.unfocus();
Full code example
Column(
children: [
const TextField(
autofocus: true,
),
ElevatedButton(
onPressed: () {
FocusManager.instance.primaryFocus?.unfocus();
},
child: const Text('Hide Keyboard'),
)
],
)
Explanation:
0 Comments