Skip to content

Instantly share code, notes, and snippets.

@Devlonoah
Created August 27, 2023 20:24
Show Gist options
  • Select an option

  • Save Devlonoah/f2a57a5e11ef6d39ec41597deb227293 to your computer and use it in GitHub Desktop.

Select an option

Save Devlonoah/f2a57a5e11ef6d39ec41597deb227293 to your computer and use it in GitHub Desktop.

Revisions

  1. Devlonoah created this gist Aug 27, 2023.
    32 changes: 32 additions & 0 deletions show_dropdown.dart
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,32 @@

    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;
    // });
    }
    });
    }