当我扫描条形码时,数字作为 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/