如何检索文本输入没有文本字段在扑动



当我扫描条形码时,这些数字通过设备发送为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的类,它是系统文本输入控件的低级接口。

文档说

要开始与系统的文本输入控件交互,调用attach在系统的文本输入控件和TextInputClient之间建立一个TextInputConnection。

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

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

这就是我所做的,我想隐藏TextField,只听用户输入的文本。使用堆栈小部件将其他小部件置于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你可以输入文本。

最新更新