Skip to content

Instantly share code, notes, and snippets.

@r100-stack
Last active March 15, 2021 10:28
Show Gist options
  • Save r100-stack/3a1bc66570f31e0bd381890121c68962 to your computer and use it in GitHub Desktop.
Save r100-stack/3a1bc66570f31e0bd381890121c68962 to your computer and use it in GitHub Desktop.
Hide keyboard in TextFormField when clicked outside
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