how to close keyboard when i tap on anyhwere on my screen flutter code example

Example 1: close keyboard on button click flutter

class _HomeState extends State<Home> {
 var currentFocus;
    
 unfocus() {
    currentFocus = FocusScope.of(context);

    if (!currentFocus.hasPrimaryFocus) {
      currentFocus.unfocus();
    }
  }
  
Widget build(BuildContext context) {
    return GestureDetector(
      onTap: unfocus,
      child: Scaffold(...)
      )
     }

Example 2: how to automatically close keyboard in flutter

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

Tags:

Java Example