キーボードを閉じるには?
主に以下の2つの方法があります。
今回は、1の方法を紹介いたします。
- キーボード外の画面タップでキーボードを閉じる
- キーボード上に閉じるボタンを配置する
キーボード外の画面タップでキーボードを閉じる
キーボードを閉じる処理は以下のコードです。
FocusScope.of(context).unfocus()
画面タップ時に上記の処理を発火させるには、GestureDetector
を使用して、以下のように記載いたします。
GestureDetector(
onTap: () => FocusScope.of(context).unfocus(),
child: HomePage(),
),
だいたいこんな感じです
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: '',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: GestureDetector(
onTap: () => FocusScope.of(context).unfocus(),
child: HomePage(),
),
);
}
}