单击文本字段外部/屏幕上的任何位置后,如何在颤动时隐藏软输入键盘? [英] How to hide soft input keyboard on flutter after clicking outside TextField/anywhere on screen?

查看:20
本文介绍了单击文本字段外部/屏幕上的任何位置后,如何在颤动时隐藏软输入键盘?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我知道使用此代码隐藏软键盘的方法,通过任何小部件的onTap方法.

Currently, I know the method of hiding the soft keyboard using this code, by onTap methods of any widget.

FocusScope.of(context).requestFocus(new FocusNode());

但我想通过单击 TextField 外部或屏幕上的任何位置来隐藏软键盘.flutter 中是否有任何方法可以做到这一点?

But I want to hide the soft keyboard by clicking outside of TextField or anywhere on the screen. Is there any method in flutter to do this?

推荐答案

你做错了,试试这个简单的隐藏软键盘的方法.你只需要在 GestureDetector 方法和 onTap 方法中写下这段代码.

You are doing it in the wrong way, just try this simple method to hide the soft keyboard. you just need to wrap your whole screen in the GestureDetector method and onTap method write this code.

        FocusScope.of(context).requestFocus(new FocusNode());

这里是完整的例子:

new Scaffold(

body: new GestureDetector(
  onTap: () {
   
    FocusScope.of(context).requestFocus(new FocusNode());
  },
child: new Container(
   //rest of your code write here
    )
 )

更新(2021 年 5 月)

return GestureDetector(
      onTap: () => FocusManager.instance.primaryFocus?.unfocus(),
      child: Scaffold(
        appBar: AppBar(
          title: Text('Login'),
        ),
        body: Body(),
      ),
    );

即使您触摸 AppBar 这也会起作用,newDart 2 中可选.FocusManager.instance.primaryFocus 将返回当前在小部件树中具有主要焦点的节点.

This will work even when you touch the AppBar, new is optional in Dart 2. FocusManager.instance.primaryFocus will return the node that currently has the primary focus in the widget tree.

Dart 中具有空安全性的条件访问

这篇关于单击文本字段外部/屏幕上的任何位置后,如何在颤动时隐藏软输入键盘?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆