Last active
March 15, 2021 10:28
-
-
Save r100-stack/3a1bc66570f31e0bd381890121c68962 to your computer and use it in GitHub Desktop.
Hide keyboard in TextFormField when clicked outside
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'; | |
void main() { | |
runApp(MyApp()); | |
} | |
class MyApp extends StatelessWidget { | |
// This widget is the root of your application. | |
@override | |
Widget build(BuildContext context) { | |
return MaterialApp( | |
title: 'Flutter Demo', | |
debugShowCheckedModeBanner: false, | |
theme: ThemeData( | |
primarySwatch: Colors.red, | |
), | |
home: MyHomePage(), | |
); | |
} | |
} | |
class MyHomePage extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar( | |
title: Text('Hide keyboard in TextFormField when touched outside'), | |
), | |
body: GestureDetector( | |
onTap: () { | |
print('Clicked outside'); | |
FocusScope.of(context).unfocus(); | |
}, | |
child: Container( | |
color: Colors.white, | |
child: Form( | |
child: Column( | |
children: [ | |
TextFormField( | |
onTap: () => print('Clicked TextFormField'), | |
), | |
], | |
), | |
), | |
), | |
), | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment