[英]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:所以我所做的是:
PopupMenuItem
复制PopupMenuItem
的整个 classTextFieldPopupMenuItem
) (例如: TextFieldPopupMenuItem
)handleTap
method to:将handleTap
方法更改为: @protected
void handleTap() {}
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.