Reputation: 571
Trying to make a TextFormField unfocus with a GestureDetector when user taps outside but I can't get it to work. onTap never fires.
class EditScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
body: new GestureDetector(
onTap: () {
print('this does not fire, why???????????');
// this is my attempt to unfocus textformfield when click away
FocusScope.of(context).requestFocus(new FocusNode());
},
child: SingleChildScrollView(
child: Column(
children: <Widget>[
TextFormField(
maxLines: null,
),
],
),
),
),
);
}
}
Upvotes: 5
Views: 7941
Reputation: 2045
Try wrap your Scaffold with Gesture Detector and then onTap function:
onTap: () {
FocusScopeNode currentFocus = FocusScope.of(context);
if (!currentFocus.hasPrimaryFocus) {
currentFocus.unfocus();
}
So it will fire everytime you tap the scaffold
Upvotes: 4