5

I want to Customise DropDownButton, so that it does not render the content of DropdownItem. Instead it should render my Custom layout/widget before and after selecting an item from DropDown. In simple Words, I want to customise my DropDownButton.

Thanks,

3
  • Checking how Dropdown is implemented, I think you will need to edit in the source implementation in order to do that as it checks if value=null or else value takes the item value and not a subset of it. Commented Feb 28, 2018 at 9:00
  • Can you be a little more clear about what you are trying to achive? Commented Feb 28, 2018 at 10:47
  • @HemanthRaj making dropdown value customization rather than reflecting the choice (choosing A will make dropdownButton value = A, what about if I choose A, the dropdownButton value = B instead) Commented Feb 28, 2018 at 11:26

1 Answer 1

6

How to render DropdownButton items differently when it is dropped down?

I found a solution through DropdownMenuItem. Its build() is executed separately for closed and dropped down state. You can use the context to find out if it is closed or dropped down state. e.g you can check for an ancestor stateful widget.

I use something like this dummy code fragment:

DropdownButton<String>(
    value: selectedItem.id,
    items: items.map((item) {
        return DropdownMenuItem<String>(
            value: item.id,
            child: Builder(builder: (BuildContext context) {
                final bool isDropDown = context.ancestorStateOfType(TypeMatcher<PageState>()) == null;

                if (isDropDown) {
                    return Text(item.name);
                } else {
                    return Text(item.name, style: TextStyle(color: Colors.red));
                }
            },)
        );
    }).toList(),
);

Where items is a list of id-name instances, and PageState is the state of my own stateful widget.

Sign up to request clarification or add additional context in comments.

1 Comment

so this works. the issue I'm having is getting the dropdown width to expand where the dropdown items ares larger than the button...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.