Skip to content

Instantly share code, notes, and snippets.

@Devlonoah
Created August 27, 2023 20:24
Show Gist options
  • Save Devlonoah/f2a57a5e11ef6d39ec41597deb227293 to your computer and use it in GitHub Desktop.
Save Devlonoah/f2a57a5e11ef6d39ec41597deb227293 to your computer and use it in GitHub Desktop.
Programmatically open dropdown
var items = ["Howdy", "Hello", "Hi", "Salud"];
void _showDropdownMenu(BuildContext context) {
final RenderBox button = context.findRenderObject() as RenderBox;
final RenderBox overlay =
Overlay.of(context)!.context.findRenderObject() as RenderBox;
final RelativeRect position = RelativeRect.fromRect(
Rect.fromPoints(
button.localToGlobal(Offset.zero, ancestor: overlay),
button.localToGlobal(button.size.bottomLeft(Offset.zero),
ancestor: overlay),
),
Offset.zero & overlay.size,
);
showMenu<String>(
context: context,
position: position,
items: items.map((String item) {
return PopupMenuItem<String>(
value: item,
child: Text(item),
);
}).toList(),
).then((String? newValue) {
if (newValue != null) {
// setState(() {
// selectedItem = newValue;
// });
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment