dismiss keyboard 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: flutter trigger show off keyboard

FocusScope.of(context).unfocus()

Example 3: how to automatically close keyboard in flutter

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

Example 4: how to dismiss keyboard in swift

override func viewDidLoad() {
    super.viewDidLoad()

    //Looks for single or multiple taps. 
    let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: "dismissKeyboard")

    //Uncomment the line below if you want the tap not not interfere and cancel other interactions.
    //tap.cancelsTouchesInView = false 

    view.addGestureRecognizer(tap)
}

//Calls this function when the tap is recognized.
@objc func dismissKeyboard() {
    //Causes the view (or one of its embedded text fields) to resign the first responder status.
    view.endEditing(true)
}

Example 5: how i can close keyboard in flutter

class YourPage extends StatefulWidget {
  createState() => _YourPageState();
}

class _YourPageState extends State<MobileHome> {

  FocusNode focusNode = new FocusNode();
  @override
  Widget build(BuildContext context) {
  return Scaffold(
  	body: Column(
      mainAxisAlignment: MainAxisAlignment.center,
      crossAxisAlignment: CrossAxisAlignment.center,
      children: [
        TextField(
          focusNode: focusNode,
        ),
        SizedBox(height: 10,),
        RaisedButton(child: Text("UP"),onPressed: (){
          FocusScope.of(context).requestFocus(focusNode);
        },),
        SizedBox(height: 10,),
        RaisedButton(child: Text("DOWN"),onPressed: (){
          focusNode.unfocus();
        },),    
      ],
    )
  );
  }
}

Example 6: android dismiss keyboard

InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(myEditText.getWindowToken(), 0);