so easy solution for beginner here is the smoothest solution for you while you need to hide-keyboard when user tap on any area of screen. hope its help you a lot.
Step - 1 : you need to create this method in Global class,
this method wrap you main widget into GestureDetector so when user tap outside the textfield it will hide keyboard automatically
Widget hideKeyboardWhileTapOnScreen(BuildContext context, {MaterialApp child}){
return GestureDetector(
onTap: () {
if (Platform.isIOS) { //For iOS platform specific condition you can use as per requirement
SystemChannels.textInput.invokeMethod('TextInput.hide');
print("Keyboard Hide");
}
},
child: child,
);
}
this method wrap you main widget into Listener so when user touch and scroll up it will hide keyboard automatically
Widget hideKeyboardWhileTapOnScreen(BuildContext context, {MaterialApp child}){
return Listener(
onPointerUp: (_) {
if (Platform.isIOS) {
FocusScopeNode currentFocus = FocusScope.of(context);
if (!currentFocus.hasPrimaryFocus &&
currentFocus.focusedChild != null) {
FocusManager.instance.primaryFocus.unfocus();
print("Call keyboard listner call");
}
}
},
child: child,
);
}
Step - 2 : here is how to use Global method
@override
Widget build(BuildContext context) {
return hideKeyboardWhileTapOnScreen(context,
child: MaterialApp(
debugShowCheckedModeBanner: false, home: Scaffold(body: setAppbar())),
);
}
Widget setAppbar2() {
return MaterialApp(
debugShowCheckedModeBanner: false,
theme: ThemeData(primarySwatch: Colors.orange),
home: Scaffold(
resizeToAvoidBottomInset: false,
appBar: AppBar(),
),
);
}
onTap: () => FocusScope.of(context).requestFocus(new FocusNode());
– FriskListener
widget for access to direct touch events. – AssessListener
widget, can you please gimme an example? – Oleneolenka