【发布时间】:2021-12-11 22:08:41
【问题描述】:
当我扫描条形码时,数字通过设备发送为String(我猜)。
如果我有一个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 来检索用户输入,因为互联网上没有如何使用这些低级类的示例。
【问题讨论】:
标签: android flutter io barcode-scanner textinput