Created
July 31, 2021 19:42
-
-
Save nkenna/87dd828c27771b4b7a36a7327587635c to your computer and use it in GitHub Desktop.
stateless widget to dismiss keyboard when user taps outside of textfield
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:flutter/material.dart'; | |
class DismissKeyboard extends StatelessWidget { | |
final Widget? child; | |
DismissKeyboard({this.child}); | |
@override | |
Widget build(BuildContext context) { | |
return GestureDetector( | |
onTap: () { | |
FocusScopeNode currentFocus = FocusScope.of(context); | |
if (!currentFocus.hasPrimaryFocus && | |
currentFocus.focusedChild != null) { | |
FocusManager.instance.primaryFocus!.unfocus(); | |
} | |
}, | |
child: child, | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment