简体   繁体   中英

How to detect keyboard click in flutter?

In the picture below, I want to delete the tag when I press the backspace key.

Picture

but I don`t detect backspace event.

this is my code.

  Widget build(BuildContext context) {
    return RawKeyboardListener(
      focusNode: FocusNode(),
      onKey: (RawKeyEvent event) {
        print(event);
        if(event.isKeyPressed(LogicalKeyboardKey.backspace)) {
          print("press backspace");
          if(_tagsStringContent.length > 0) {
            if (_tagsStringContent.length == 1 && _showPrefixIcon == true) {
              widget.onDelete(_tagsStringContent[_tagsStringContent.length-1]);
              setState(() {
                _tagsStringContent.removeLast();
                _showPrefixIcon = false;
              });
            } else {
              widget.onDelete(_tagsStringContent[_tagsStringContent.length-1]);
              setState(() {
                _tagsStringContent.removeLast();
              });
            }
          }
          }
      },
      autofocus: true,
      child: TextField( (...skip)
      )
    )
  ) 

RawKeyEvent only works in computer backspace.

how to detect backspace key in keyboard?

尝试检查这个颤振包https://pub.dev/packages/keyboard_actions

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM