How to make flutter custom drop down button?

試著忘記壹切 提交于 2019-12-06 12:43:35

问题


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,


回答1:


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.



来源:https://stackoverflow.com/questions/49024142/how-to-make-flutter-custom-drop-down-button

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!