android - 如何在 Flutter 中检索没有文本字段的文本输入

标签 android flutter io barcode-scanner textinput

当我扫描条形码时,数字作为 String(我猜)通过设备发送。 如果我有一个 textfield,并且它被选中/聚焦,扫描会自动填充数字字符串。 我不想把它放在 textField 上,我想在一个类上捕捉它,处理它以便我可以更新我的购物车。

我尝试使用 KeyboardListener,但没有用。

@override
  Widget build(BuildContext context) {
    return KeyboardListener(
        focusNode: FocusNode(),
        onKeyEvent: (event) {
          text = event.character.toString();
        },
        child: Scaffold(
            appBar: AppBar(
              title: const Text('Retrieve Text Input'),
            ),
            body: Center(
              child: Text(text),
            )));
  }
}

我还看到存在一个名为 TextInput 的类,它是系统文本输入控件的低级接口(interface)。

文档说

To start interacting with the system's text input control, call attach to establish a TextInputConnection between the system's text input control and a TextInputClient.

EditableTextState client = EditableTextState(); 
TextInputConfiguration configuration = TextInputConfiguration();

final TextInputConnection _textInputConnection =
        TextInput.attach(client, configuration);

但我不明白如何使用此 TextInputConnection 来检索用户输入,因为互联网上没有如何使用这些低级类的示例。

最佳答案

我就是这样做的,我想隐藏 TextField 并只听用户输入的文本。 使用 Stack 小部件将其他小部件放在 TextField 上,以便它隐藏在其他小部件下。 在使用 TextField 的焦点点击上部小部件时,将焦点放在 TextField 上。 使用 FocusNode getFocus 函数。

您可以尝试禁用用户交互并在 TextField 中应用一些样式。

 style: TextStyle(color: Colors.transparent),
 autofocus: true,
 cursorColor: Colors.transparent,
 showCursor: false,
 autocorrect: false,
 enableSuggestions: false,
 maxLines: 1,
 enableInteractiveSelection: false,
 decoration: InputDecoration(border: InputBorder.none),
                              

使用 TextEditController,您可以获得输入的文本。

关于android - 如何在 Flutter 中检索没有文本字段的文本输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69724728/

相关文章:

android - 如何在Android中实现NavigationView的可扩展菜单

android - 插入脚本返回成功,但没有任何内容插入到数据库中

ios - "An error occurred when accessing the keychain"

flutter - 如何在 flutter 测试中打开抽屉

java - 在 Java 中连接两个大文件(超过 1.5GB)的最有效(最快)的方法是什么?

java - 将文件加载到内存中(Java)?

c - 定位转义序列并将其转换为其他值

java - 尝试访问数组java中的元素时表达式预期错误

flutter - 无法将类型为 'String' 的值分配给 flutter Getx 中类型为 'RxString' 的变量

android - 测试权限和 Android 应用 list 文件