flutter 当在文本字段上按回车键时,是否可以覆盖默认行为?

fslejnso  于 2023-02-20  发布在  Flutter
关注(0)|答案(1)|浏览(137)

我尝试覆盖按enter时新行的默认行为。我能够捕获键事件,但它仍然默认为添加新行。想法是在enter键上执行函数。
下面是捕获enter键的代码

RawKeyboardListener(
        child: EditableText(
          style: const TextStyle(
            fontSize: 16,
          ),
          backgroundCursorColor: Colors.black,
          controller: widget.controller,
          cursorColor: Colors.black,
          focusNode: widget.focusNode,
          maxLines: null,
        ),
        focusNode: FocusNode(),
        onKey: (RawKeyEvent event) {
          if (event.isKeyPressed(LogicalKeyboardKey.enter)) //Enter Key ID from keyboard
          {
            print("Enter is pressed");
          }
        },
      ),
wnavrhmk

wnavrhmk1#

在不知道要完成什么的情况下,有一种方法可以让多行TextField在按Enter键时不创建新行,然后可以监听onEditingCompleteonSubmitted

TextField(
  keyboardType: TextInputType.text,
  maxLines: null,
  onEditingComplete: () => print('Enter was probably pressed'),
)

相关问题