我想在单击打开菜单后关闭菜单:
所以我想利用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'),
),
],
如果你在onSelected里面使用SetState对你有帮助吗?
onSelected: (SampleItem item) {
setState(() {
selectedItem = item;
});
},
需要 StateFull。
更多这里: https://api.flutter.dev/flutter/material/PopupMenuButton-class.html