skip to Main Content

Both of the widgets look almost similar. So, what is the exact use case of them? Also, how do you set the width of the DropdownMenu, not the entries?

2

Answers


    • DropdownMenu is a stateless widget that represents the drop-down menu itself. It does not have any state of its own, and it depends on the DropdownButton widget to provide the list of items and the currently selected item.
    • DropdownButton is a stateful widget that represents the drop-down button itself. It has state for the list of items, the currently selected item, and the on-change callback.

    In other words, DropdownMenu is a "dumb" widget that just displays the drop-down menu, while DropdownButton is a "smart" widget that handles the logic of displaying the list of items, selecting an item, and calling the on-change callback.

    Login or Signup to reply.
  1. 1. DropdownButton:
    *DropdownButton is a Flutter widget that provides a clickable button with a dropdown arrow, and when clicked, it displays a list of items in a popup menu. It’s used when you want to provide a single selectable option from a list of items. When an item is selected from the dropdown menu, the button’s label changes to the selected item’s label.

    DropdownButton<String>(
      value: selectedValue,
      onChanged: (newValue) {
        setState(() {
          selectedValue = newValue;
        });
      },
      items: [
        DropdownMenuItem(
          value: 'Option 1',
          child: Text('Option 1'),
        ),
        DropdownMenuItem(
          value: 'Option 2',
          child: Text('Option 2'),
        ),
      ],
    )
    

    2.DropdownMenu:

    There isn’t a widget called DropdownMenu in the official Flutter library as of my knowledge cutoff date is September 2021. However, you might be referring to the PopupMenuButton widget. This widget creates a button that, when pressed, displays a popup menu with a list of items.

    PopupMenuButton is used when you want to show a list of menu items in response to a user action (like a right-click or a long press), rather than as a direct replacement for a selection widget like DropdownButton.

    PopupMenuButton<String>(
      onSelected: (selectedValue) {
        // Handle the selected value
      },
      itemBuilder: (BuildContext context) => [
        PopupMenuItem(
          value: 'Option 1',
          child: Text('Option 1'),
        ),
        PopupMenuItem(
          value: 'Option 2',
          child: Text('Option 2'),
        ),
      ],
    )
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search