当我扫描条形码时,数字通过设备以
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(focusNode: AlwaysDisabledFocusNode(),) class AlwaysDisabledFocusNode extends FocusNode { @override bool get hasFocus => false; }
- ashutosh