简体   繁体   English

当我在 Flutter Web 中的 TextField 中按空格键时,如何防止 PopupMenu 消失?

[英]How do I prevent the PopupMenu from disappearing when I press space bar within the TextField in it in Flutter Web?

Here's the snippet of the code in the PopupMenuButton (in Flutter Web):这是 PopupMenuButton 中的代码片段(在 Flutter Web 中):

PopupMenuButton(
   offset: Offset(100, 100),
   elevation: 5.0,
   child: ListTile(
   dense: true,
   leading: Icon(
      Icons.filter_vintage,
      size: 16,
     ),
   title: Text('Menu'),

   itemBuilder: (context) => [
       PopupMenuItem(child: Container(
           padding: EdgeInsets.all(20.0),
           child: TextField(
              autofocus: true,
              cursorColor: kLeadingOrangeColor,
              style: kFilterButtonTextStyle,      
              onChanged: (input) => searchNameString = input,
              onEditingComplete: onEditingCompleteCallBack,
              controller: searchController,
         )),
       ]),

Then here is the short recording.然后是简短的录音。 Notice the PopupMenu disappeared when I press请注意,当我按下时 PopupMenu 消失了

在此处输入图像描述

So the problem is that every time I pressed space bar, the PopupMenu disappear.所以问题是每次我按空格键时,PopupMenu 都会消失。 What I need is the PopupMenu persisted until I press or click somewhere else.我需要的是 PopupMenu 一直存在,直到我按下或单击其他地方。 Anyone knows where might be the problem?任何人都知道问题可能出在哪里?

Here is my flutter doctor :这是我的flutter doctor

[✓] Flutter (Channel master, 1.20.0-7.0.pre, on Mac OS X 10.15.5 19F101, locale en-US)
 
[✓] Android toolchain - develop for Android devices (Android SDK version 29.0.3)
[✓] Xcode - develop for iOS and macOS (Xcode 11.5)
[✓] Chrome - develop for the web
[✓] Android Studio (version 4.0)
[✓] VS Code (version 1.47.0)
[✓] Connected device (2 available)

• No issues found!

I have found the solution.我找到了解决方案。

The problem is with the handleTap method in the PopupMenuItem class.问题出在PopupMenuItem class 中的handleTap方法上。

 @protected
  void handleTap() {
    Navigator.pop<T>(context, widget.value);
  }

So what I did was:所以我所做的是:

  1. Copy the whole class of PopupMenuItem复制PopupMenuItem的整个 class
  2. Rename it something else.将其重命名为其他名称。 (eg: TextFieldPopupMenuItem ) (例如: TextFieldPopupMenuItem
  3. Change the handleTap method to:handleTap方法更改为:
 @protected
  void handleTap() {}
  1. Use this new class for the text fields.将这个新的 class 用于文本字段。

Then it works!然后它工作!

I think it is because of the abstraction of what we can put inside PopupMenuItem , it is hard to determine which input can be taken to dismiss it.我认为这是因为我们可以在PopupMenuItem中放入的内容的抽象,很难确定可以采用哪个输入来解除它。 However, since I'm using hard keyboard, I think it is not supposed to detect it as a 'tap', but maybe it is up to the Flutter team.但是,由于我使用的是硬键盘,我认为它不应该将其检测为“敲击”,但可能取决于 Flutter 团队。

Anyway, this work around works.无论如何,这项工作是可行的。

Hope it will help someone.希望它会帮助某人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Flutter &amp; Textfield :如何通过自动删除该空间来限制我的用户使用文本字段中的空间? - Flutter & Textfield : How do I restrict my user from using space in textfield by automatically removing that space? 如何使用 RawKeyboardListener 将焦点节点从下拉列表更改为文本字段,因为它两次听 flutter web 中的按键 - How to i change focus node from dropdown to textfield with RawKeyboardListener as it listen twice to key press in flutter web 在 Flutter 中,我想通过按下按钮获取当前日期并显示在文本字段中,我该怎么做? - In Flutter, I want to get current date with button press and show in a Textfield, How can I do this? 如何在 Flutter 中从 main() 中自动路由? - How do I automatically route when in Flutter from within main()? 如何防止键盘覆盖 TextField - How do I prevent keyboard from covering up TextField Flutter - 长按弹出菜单 - Flutter - PopupMenu on long press 如何将文本字段的值居中(颤动) - How do I center the value of the textfield (flutter) 如何在 Flutter 中为 TextField 添加渐变? - How do I add a gradient to a TextField in Flutter? Flutter:当键盘隐藏(后按)时防止调用 TextField 的 onChange - Flutter: Prevent to call onChange of TextField when Keyboard hide (Back Press) Flutter:如何防止cursor手柄消失? - Flutter: How to prevent the cursor handle from disappearing?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM