Created
August 27, 2023 20:24
-
-
Save Devlonoah/f2a57a5e11ef6d39ec41597deb227293 to your computer and use it in GitHub Desktop.
Programmatically open dropdown
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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