Forked from guilherme-v/medium_04_dismiss_kb_ios.dart
Created
September 25, 2020 06:53
-
-
Save CoderJava/0e330d988922967f7b4778009c90da8a to your computer and use it in GitHub Desktop.
This file contains hidden or 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
void main() { | |
runApp(MyApp()); | |
} | |
class MyApp extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
title: 'Flutter Demo', | |
theme: ThemeData( | |
primarySwatch: Colors.blue, | |
visualDensity: VisualDensity.adaptivePlatformDensity, | |
), | |
builder: (context, child) => Scaffold( | |
// Global GestureDetector that will dismiss the keyboard | |
body: GestureDetector( | |
onTap: () { | |
// When running in iOS, dismiss the keyboard when any Tap happens outside a TextField | |
if (Platform.isIOS) hideKeyboard(context); | |
}, | |
child: child, | |
), | |
), | |
home: MyHomePage(title: 'Flutter Demo Home Page'), | |
); | |
} | |
void hideKeyboard(BuildContext context) { | |
FocusScopeNode currentFocus = FocusScope.of(context); | |
if (!currentFocus.hasPrimaryFocus && currentFocus.focusedChild != null) { | |
FocusManager.instance.primaryFocus.unfocus(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment