flutter button click hide keyboard 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: remove focus textfield flutter
import 'package:flutter/material.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return GestureDetector(
onTap: () {
FocusScopeNode currentFocus = FocusScope.of(context);
if (!currentFocus.hasPrimaryFocus) {
currentFocus.unfocus();
}
},
child: MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(),
),
);
}
}
Example 3: 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();
},),
],
)
);
}
}