我的弹出菜单中有一个复选框项目列表(PopupMenuItem(,它由popupMenuButton触发。我希望用户能够选择多个复选框,但一旦选择了一个项目,就会关闭窗口。
有什么办法可以防止这种情况发生吗?我需要它保持打开状态,或者立即强制它再次打开。
(我尝试创建自己的PopupItem类来覆盖"handleTap((";,但是我需要更新父菜单视图的状态,我不能从其他类调用它。所以我又把它去掉了。(
class TopicsNotificationMenu extends StatefulWidget {
List<Topic> topics = [];
TopicsNotificationMenu(this.topics);
@override
_TopicsNotificationMenuState createState() =>
_TopicsNotificationMenuState();
}
class _TopicsNotificationMenuState extends State<TopicsNotificationMenu> {
_TopicsNotificationMenuState();
_updateTopics(_tp){
setState(() {
if(_tp.value == true){
_tp.value = false;
}else{
_tp.value = true;
_registerTopic(_tp.name);
}
});
}
@override
Widget build(BuildContext context) {
return PopupMenuButton(
onSelected: (value) {
_updateTopics(value);
},
itemBuilder: (BuildContext context) {
return widget.topics.map((var tp) {
var _icon = (tp.value == true) ? Icons.check_box : Icons.check_box_outline_blank;
return PopupMenuItem(
value: tp,
child: ListTile(
leading: Icon(_icon),
title: Text(tp.name),
),
);
}).toList();
});
}
我不得不为此创建自己的小部件。总之,我希望在屏幕右上角有一个浮动列表,其中包含复选框项目的列表。当我按下项目时,它们会被选中/未选中,但窗口一直打开,直到我点击关闭它。我使用了以下小部件树:
An OverlayEntry widget so that I could place it anywhere floating above the app
-> added the SafeArea widget so that my padding would include the notification bar at the top of the phone
-> a Gesture Detector, so that on tapping off it I could close it
-> a column with CrossAxisAlignment.end so that it was placed in the top-right
-> a container widget with some padding
-> a Material for elevation shading and to contain a list
-> The Listview
-> The List Tiles with icon and Text for each item. The icon was either the ticked or unticked graphic, depending on it's value (which is stored as a boolean in an array)
在ListTile的Tap上,它更新了小部件的状态并再次显示它。没有视觉上的闪烁,它是瞬间的。点击手势检测器,它只需删除小工具。
每次用户选择后,您都可以尝试重新打开它。我在这里举了一个例子
或者,我建议创建具有所需行为的自己的小部件。