【问题标题】:flutter TextField use cause KeyboardListener alway show keyboard on event颤动的TextField使用原因键盘侦听器总是在事件中显示键盘
【发布时间】: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


【解决方案1】:

好的,所以,目前没有flutter软件解决方案。

这是一个谷歌键盘错误或颤振错误..或两者兼而有之。

似乎谷歌键盘不会从 textfield、TextControler、focusnode 或其他地方消失。或颤动不会破坏对谷歌键盘的回调。不知道。

但是,尝试其他应用程序键盘,它很神奇。它的工作......正常......流利......正如预期的那样。

老实说,也许这个替换键盘没有实现回调。

但他工作,应用程序工作,我的客户可以流畅地使用它..

无需更改我的代码或颤振代码的任何部分..

费用?只是对客户说。 '安装和使用这个键盘..'

【讨论】:

    【解决方案2】:

    您尝试过使用https://pub.dev/packages/flutter_barcode_listener

    这个库允许条形码扫描并解决键盘监听器引起的许多问题

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-28
      • 1970-01-01
      • 1970-01-01
      • 2020-07-27
      • 1970-01-01
      相关资源
      最近更新 更多