admin管理员组

文章数量:1026114

I want to close the menu after opening it with a click :

So I want to use the state of the PopupMenuButton to close it.

I tried :

_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'),
                          ),
                        ],

I want to close the menu after opening it with a click :

So I want to use the state of the PopupMenuButton to close it.

I tried :

_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'),
                          ),
                        ],
Share Improve this question asked Nov 18, 2024 at 12:44 Rami DhouibRami Dhouib 114 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

If you use SetState inside the onSelected help you?

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

Need to be StateFull.

More here: https://api.flutter.dev/flutter/material/PopupMenuButton-class.html

I want to close the menu after opening it with a click :

So I want to use the state of the PopupMenuButton to close it.

I tried :

_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'),
                          ),
                        ],

I want to close the menu after opening it with a click :

So I want to use the state of the PopupMenuButton to close it.

I tried :

_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'),
                          ),
                        ],
Share Improve this question asked Nov 18, 2024 at 12:44 Rami DhouibRami Dhouib 114 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

If you use SetState inside the onSelected help you?

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

Need to be StateFull.

More here: https://api.flutter.dev/flutter/material/PopupMenuButton-class.html

本文标签: navigationFlutterClose PopUpMenu using PopUpMenuButtonStateStack Overflow