How do I open a popup menu from a second widget?
final button = new PopupMenuButton(
itemBuilder: (_) => >[
Use popup_menu package as a library
Add popup_menu: ^1.0.5
in your pubspec.yaml
dependencies. And import it:
import 'package:popup_menu/popup_menu.dart';
First, you should set the context at somewhere in you code. Like below:
PopupMenu.context = context;
Then create a showPopup
widget and pass the required params:
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');
}
Here is the open popup
@override
Widget build(BuildContext context) {
PopupMenu.context = context; // This is the set context
return Scaffold(
appBar: AppBar(
title: Text('Show Popup')),
body: Stack(
children: [
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,
),
),
);
},
),
],
),
);
}