【发布时间】:2022-06-29 15:29:08
【问题描述】:
使用 KeyboardListener 从扫描仪获取条形码,我无法解决一个非常愚蠢的问题。 首先使用keyboardListener 进行扫描可以完美地工作。 获得事件,条码也是,没有虚拟键盘,完美..
但如果我在同一屏幕或程序的任何地方使用任何文本字段,之后, 任何来到keyboardListener 的事件都显示虚拟键盘,没有任何文本字段或屏幕中的其他内容。 它变成了一场噩梦..
我想避免在没有任何输入的情况下显示键盘..
不想关闭键盘,堆栈溢出的方法很多。
重现步骤:
1:使用物理键盘或HID输入序列码或使用KeyboardListener
2:点击文本框,输入任何内容和有效文本
3:normay,KeyboardListener 重新获得控制权并获得物理事件,并且每个键盘都显示...这就是问题..
youtube video to illustrate (52s)
奇怪的东西。如果您使用方形键设置应用程序背景并获得前景,问题就会消失。虚拟键盘不会在物理键盘或 HID 使用上再次显示...直到下一个文本字段使用..
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: const MyHomePage(title: 'KbdListener with TextField'),
);
}}
class MyHomePage extends StatefulWidget {
const MyHomePage({Key? key, required this.title}) : super(key: key);
final String title;
@override
State<MyHomePage> createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
List<FocusNode> ListFocusNode = [FocusNode(), FocusNode()];
DateTime whenlastchar = DateTime.now();
List<String> scanned4 = [];
String _receivedtext = "Scanned text here..";
final TextEditingController _myTextControler =
TextEditingController(text: "");
@override
void initState() {
ListFocusNode.first.requestFocus();
super.initState();
}
@override
void dispose() {
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
KeyboardListener(
key: const Key('KeyboardListener-files'),
focusNode: ListFocusNode.first,
autofocus: true,
onKeyEvent: (event) async {
var difference = DateTime.now().difference(whenlastchar);
whenlastchar = DateTime.now();
if (event.character != null) {
if (difference.inMilliseconds > 1000) {
scanned4.clear();
}
scanned4.add(event.character.toString());
if ((event.character == "\n") ||
(event.character == " ") ||
(event.character == 0x09)) {
String tempo =
scanned4.reduce((first, second) => first + second);
scanned4.clear();
tempo = tempo.trim();
// update
setState(() {
_receivedtext = tempo;
});
}
}
},
child: Column(
children: <Widget>[
Text(
_receivedtext,
style: Theme.of(context).textTheme.headlineSmall,
),
],
),
),
Expanded(
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
children: [
TextField(
controller: _myTextControler,
autofocus: false,
focusNode: ListFocusNode.last,
keyboardType: TextInputType.text,
style: const TextStyle(
fontSize: 20,
color: Colors.black,
fontWeight: FontWeight.w400,
),
textInputAction: TextInputAction.done,
onSubmitted: (value) {
print("textfield value: '$value'");
setState(() {
_receivedtext = value;
});
_myTextControler.clear();
FocusScope.of(context)
.requestFocus(ListFocusNode.first);
},
),
Row(children: [
TextButton(
child: const Text("KeyboardListener Focus"),
onPressed: () {
setState(() {
FocusScope.of(context)
.requestFocus(ListFocusNode.first);
});
},
),
]),
],
),
),
],
),
),
);
}
}
【问题讨论】:
-
你能添加一些代码吗?
-
添加了 src 代码来测试和查看问题..
-
有一个类似的应用程序,但是我无法使用 Raw Keyboard Listener 读取条形码,您能帮帮我吗?
标签: flutter keyboard textfield