我正在使用flutter PopUpMenuButton。所有我想要的是当我选择菜单上的任何项目时,弹出式菜单不应该被驳回,而是让我从弹出式菜单中选择多个值.文档中说你可以覆盖handleTap属性,但我不清楚如何做到这一点?
///The [handleTap] method can be overridden to adjust exactly what happens when
/// the item is tapped. By default, it uses [Navigator.pop] to return the
/// [PopupMenuItem.value] from the menu route.
void handleTap() {
Navigator.pop<T>(context, widget.value);
}
class PopupItem extends PopupMenuItem {
const PopupItem({
Key key,
Widget child,
}) : super(key: key, child: child);
@override
_PopupItemState createState() => _PopupItemState();
}
class _PopupItemState extends PopupMenuItemState {
@override
void handleTap() {}
}
使用方法
PopupMenuButton(
itemBuilder: (_) {
return [
PopupItem(child: ...),
];
}
你可以像这样使用CheckedPopupMenuItem... 就像在下面提到的那样。官方 文件资料
PopupMenuButton<Commands>(
onSelected: (Commands result) {
switch (result) {
case Commands.heroAndScholar:
setState(() { _heroAndScholar = !_heroAndScholar; });
break;
case Commands.hurricaneCame:
// ...handle hurricane option
break;
// ...other items handled here
}
},
itemBuilder: (BuildContext context) => <PopupMenuEntry<Commands>>[
CheckedPopupMenuItem<Commands>(
checked: _heroAndScholar,
value: Commands.heroAndScholar,
child: const Text('Hero and scholar'),
),
const PopupMenuDivider(),
const PopupMenuItem<Commands>(
value: Commands.hurricaneCame,
child: ListTile(leading: Icon(null), title: Text('Bring hurricane')),
),
// ...other items listed here
],
)