简体   繁体   English

如何制作颤振自定义下拉按钮?

[英]How to make flutter custom drop down button?

I want to Customise DropDownButton , so that it does not render the content of DropdownItem .我想自定义DropDownButton ,这样它就不会呈现DropdownItem的内容。 Instead it should render my Custom layout/widget before and after selecting an item from DropDown.相反,它应该在从 DropDown 中选择一个项目之前和之后呈现我的自定义布局/小部件。 In simple Words, I want to customise my DropDownButton .简单来说,我想自定义我的DropDownButton

Thanks,谢谢,

How to render DropdownButton items differently when it is dropped down?下拉时如何以不同方式呈现DropdownButton项目?

I found a solution through DropdownMenuItem .我通过DropdownMenuItem找到了解决方案。 Its build() is executed separately for closed and dropped down state.它的build()是针对关闭和下拉状态分别执行的。 You can use the context to find out if it is closed or dropped down state.您可以使用上下文来确定它是关闭状态还是下拉状态。 eg 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.其中items是一个id-name实例列表,而PageState是我自己的有状态小部件的状态。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM