使用popup_menu包作为库
添加popup_menu: ^1.0.5
您的pubspec.yaml
依赖项。并导入它:
import 'package:popup_menu/popup_menu.dart';
首先,您应该在代码中的某处设置上下文。如下所示:
PopupMenu.context = context;
然后创建一个showPopup
小部件并传递所需的参数:
void showPopup(Offset offset) {
PopupMenu menu = PopupMenu(
// backgroundColor: Colors.teal,
// lineColor: Colors.tealAccent,
maxColumn: 3,
items: [
MenuItem(title: 'Copy', image: Image.asset('assets/copy.png')),
MenuItem(title: 'Mail', image: Icon(Icons.mail, color: Colors.white)),
MenuItem(title: 'Power',image: Icon(Icons.power, color: Colors.white,)),
],
onClickMenu: onClickMenu,
stateChanged: stateChanged,
onDismiss: onDismiss);
menu.show(rect: Rect.fromPoints(offset, offset));
}
void stateChanged(bool isShow) {
print('menu is ${isShow ? 'showing' : 'closed'}');
}
void onClickMenu(MenuItemProvider item) {
print('Click menu -> ${item.menuTitle}');
}
void onDismiss() {
print('Menu is dismiss');
}
这是打开的弹出窗口
@override
Widget build(BuildContext context) {
PopupMenu.context = context; // This is the set context
return Scaffold(
appBar: AppBar(
title: Text('Show Popup')),
body: Stack(
children: <Widget>[
ListView.builder(
itemCount: list.length,
itemBuilder: (context, index) {
return MaterialButton(
child: GestureDetector(
onTapUp: (TapUpDetails details) {
showPopup(details.globalPosition);
},
child: ListTile(
leading: IconButton(
icon: Icon(Icons.restaurant_menu),
),
title: Text("Select your categories"),
subtitle: Text("Sub Title"),
// onTap: showPopup,
),
),
);
},
),
],
),
);
}