2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

FlutterでTextFieldのフォーカスを正しく外す方法 Listener編

Last updated at Posted at 2021-02-02

GestureDetectorを使う方法だと、ボタンをタップした場合はフォーカスが外れない。
Listenerを使うと正しく外れました。

追記 2021/02/21
コメントよりご指摘いただきました。
長押しから貼り付けを選択した際にもフォーカスが外れて貼り付けされないようです。
良い方法があれば、教えてください!

#環境
flutter 1.25.0

#方法

Listener(
  onPointerDown: (_) {
    FocusScopeNode currentFocus = FocusScope.of(context);
    if (!currentFocus.hasPrimaryFocus && currentFocus.focusedChild != null) {
      currentFocus.focusedChild.unfocus();
    }
  },
  child: MaterialApp(...),
);

#参考
https://flutterigniter.com/dismiss-keyboard-form-lose-focus/

GestureDetectorを使う方法(古いバージョンだと問題ない模様)
https://qiita.com/tsukushibito/items/d7fde1d998a30292add7

2
1
4

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
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?