Rami Dhouib
Rami Dhouib

Reputation: 11

Flutter : Close PopUpMenu using PopUpMenuButtonState

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

Upvotes: 0

Views: 17

Answers (1)

Alexandre B.
Alexandre B.

Reputation: 371

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

Upvotes: 0

Related Questions