最佳答案
简单的方法无疑是避免辅助类。
从 Dart 2.2 开始,使用 set literals,我们可以直接将菜单项的映射放置在应用栏中
appBar: AppBar(
title: Text('Homepage'),
actions: <Widget>[
PopupMenuButton<String>(
onSelected: handleClick,
itemBuilder: (BuildContext context) {
return {'Logout', 'Settings'}.map((String choice) {
return PopupMenuItem<String>(
value: choice,
child: Text(choice),
);
}).toList();
},
),
],
),
并使用方法中的项目文本值处理单击
void handleClick(String value) {
switch (value) {
case 'Logout':
break;
case 'Settings':
break;
}
}
关于flutter - 在 Flutter 中添加 3 点弹出菜单 AppBar 的最简单方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58144948/