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
1 Answer
Reset to default 0If 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