如何在Flutter中自定义下拉菜单按钮?

时间:2020-07-24 18:03:50

标签: flutter dropdown

如何在Flutter中归档此设计?

我需要自定义下拉按钮,如以下屏幕截图所示:

enter image description here

如何为所选项目设置样式,为未选中项目设置样式,而又不更改下拉菜单上方标签的样式?

如果有人可以帮助,那就太好了

1 个答案:

答案 0 :(得分:0)

尝试一下:

            PopupMenuButton<MenuItems>(
              child: Row(
                children: [
                  Text('Show Menu'),
                  Icon(
                    Icons.expand_more,
                    color: Colors.grey,
                  ),
                ],
              ),
              onSelected: (MenuItems result) {
                setState(() {
                  selection = result;
                });
              },
              itemBuilder: (BuildContext context) =>
                  <PopupMenuEntry<MenuItems>>[
                PopupMenuItem<MenuItems>(
                  value: MenuItems.one,
                  child: Row(
                    children: [
                      MenuItems.one == selectedItem
                          ? Row(
                              children: [
                                Container(
                                  width: 3,
                                  height: 16,
                                  color: Colors.red,
                                ),
                                SizedBox(
                                  width: 8,
                                ),
                              ],
                            )
                          : Container(),
                      Text(
                        'One',
                        style: TextStyle(
                          color: (MenuItems.one == selectedItem)
                              ? Colors.red
                              : null,
                        ),
                      ),
                    ],
                  ),
                ),
                PopupMenuItem<MenuItems>(
                  value: MenuItems.two,
                  child: Row(
                    children: [
                      MenuItems.two == selectedItem
                          ? Row(
                              children: [
                                Container(
                                  width: 3,
                                  height: 16,
                                  color: Colors.red,
                                ),
                                SizedBox(
                                  width: 8,
                                ),
                              ],
                            )
                          : Container(),
                      Text(
                        'Two',
                        style: TextStyle(
                          color: (MenuItems.two == selectedItem)
                              ? Colors.red
                              : null,
                        ),
                      ),
                    ],
                  ),
                ),
                PopupMenuItem<MenuItems>(
                  value: MenuItems.three,
                  child: Row(
                    children: [
                      MenuItems.three == selectedItem
                          ? Row(
                              children: [
                                Container(
                                  width: 3,
                                  height: 16,
                                  color: Colors.red,
                                ),
                                SizedBox(
                                  width: 8,
                                ),
                              ],
                            )
                          : Container(),
                      Text(
                        'Three',
                        style: TextStyle(
                          color: (MenuItems.three == selectedItem)
                              ? Colors.red
                              : null,
                        ),
                      ),
                    ],
                  ),
                ),
              ],
            ),
          ],
        ),