如何在屏幕上听键盘颤振? [英] How to listen to keyboard on screen Flutter?

查看:65
本文介绍了如何在屏幕上听键盘颤振?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个移动应用程序,我想在键盘出现在屏幕上时(即当输入文本字段处于焦点时)删除小部件.

I am building a mobile app, I want to remove a widget when the keyboard appears on the screen, i.e when the input text field is on focus.

我尝试使用RawKeyboardListener,但这似乎不起作用,我的代码如下:

I have tried to use RawKeyboardListener but that doesn't seem to work, my code is as below:

     new Container(
         child: new RawKeyboardListener(focusNode: new FocusNode(),
         onKey: (input) => debugPrint("*****KEY PRESSED"),
         child: new TextField(
         controller: new TextEditingController(),
    )));

推荐答案

当文本字段成为焦点时,键盘将自动出现.因此,您可以将一个列表器添加到focusnode上,以监听焦点变化并隐藏相应的小部件.

The keyboard will automatically appear when the text field is focused. So you can add a listner to the focusnode to listen the focus change and hide respective widget.

示例:

    void _listener(){
        if(_myNode.hasFocus){
          // keyboard appeared 
        }else{
          // keyboard dismissed
        }
    }

    FocusNode _myNode = new FocusNode()..addListener(_listner);

    TextField _myTextField = new TextField(
            focusNode: _mynNode,
            ...
            ...
        );

    new Container(
        child: _myTextField
    );

这篇关于如何在屏幕上听键盘颤振?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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