How to open a PopupMenuButton?

后端 未结 6 1733
暗喜
暗喜 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:18

    Screenshot:


    Full code:

    class MyPage extends StatelessWidget {
      final GlobalKey> _key = GlobalKey();
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            actions: [
              PopupMenuButton(
                key: _key,
                itemBuilder: (context) {
                  return >[
                    PopupMenuItem(child: Text('0'), value: 0),
                    PopupMenuItem(child: Text('1'), value: 1),
                  ];
                },
              ),
            ],
          ),
          body: RaisedButton(
            onPressed: () => _key.currentState.showButtonMenu(),
            child: Text('Open/Close menu'),
          ),
        );
      }
    }
    

提交回复
热议问题