Как не отклонить PopUpMenuButton после выбора элемента? - PullRequest
1 голос
/ 28 апреля 2020

Я использую флаттер 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);
  }

Ответы [ 2 ]

0 голосов
/ 28 апреля 2020

Вы можете использовать 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
      ],
    )
0 голосов
/ 28 апреля 2020
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: ...),
  ];
}
...