14
11

More than 3 years have passed since last update.

【Flutter】キーボード外タップで、キーボードを閉じる

Posted at

キーボードを閉じるには?

主に以下の2つの方法があります。
今回は、1の方法を紹介いたします。

  1. キーボード外の画面タップでキーボードを閉じる
  2. キーボード上に閉じるボタンを配置する

キーボード外の画面タップでキーボードを閉じる

キーボードを閉じる処理は以下のコードです。

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(),
        ),
    );
  }
}
14
11
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
14
11