Flutter : 使用 PopUpMenuButtonState 关闭 PopUpMenu

问题描述 投票:0回答:1

我想在单击打开菜单后关闭菜单:

所以我想利用PopupMenuButton的状态来关闭它。

我试过了:

_popUpKey.currentState?.dispose();
  final _popUpKey = GlobalKey();

                      PopupMenuButton<int>(
                        key: _popUpKey,
                        onSelected: (value) {
                          // Handle the selected value
                        },
                        itemBuilder: (context) => [
                          PopupMenuItem(
                            value: 1,
                            child: Text('Option 1'),
                          ),
                          PopupMenuItem(
                            value: 2,
                            child: Text('Option 2'),
                          ),
                        ],
flutter navigation popupmenu
1个回答
0
投票

如果你在onSelected里面使用SetState对你有帮助吗?

      onSelected: (SampleItem item) {
        setState(() {
          selectedItem = item;
        });
      },

需要 StateFull。

更多这里: https://api.flutter.dev/flutter/material/PopupMenuButton-class.html

© www.soinside.com 2019 - 2024. All rights reserved.