How to open a PopupMenuButton?

后端 未结 6 1707
暗喜
暗喜 2021-01-07 17:34

How do I open a popup menu from a second widget?

final button = new PopupMenuButton(
    itemBuilder: (_) => >[
                 


        
6条回答
  •  走了就别回头了
    2021-01-07 18:04

    I think it would be better do it in this way, rather than showing a PopupMenuButton

    void _showPopupMenu() async {
      await showMenu(
        context: context,
        position: RelativeRect.fromLTRB(100, 100, 100, 100),
        items: [
          PopupMenuItem(
              child: const Text('Doge'), value: 'Doge'),
          PopupMenuItem(
              child: const Text('Lion'), value: 'Lion'),
        ],
        elevation: 8.0,
      );
    }
    

    There will be times when you would want to display _showPopupMenu at the location where you pressed on the button Use GestureDetector for that

    final tile = new ListTile(
      title: new Text('Doge or lion?'),
      trailing: GestureDetector(
        onTapDown: (TapDownDetails details) {
          _showPopupMenu(details.globalPosition);
        },
        child: Container(child: Text("Press Me")),
      ),
    );
    

    and then _showPopupMenu will be like

    _showPopupMenu(Offset offset) async {
        double left = offset.dx;
        double top = offset.dy;
        await showMenu(
        context: context,
        position: RelativeRect.fromLTRB(left, top, 0, 0),
        items: [
          ...,
        elevation: 8.0,
      );
    }
    

提交回复
热议问题