Flutter PopupMenuButton缺少波纹效果

时间:2019-10-20 16:14:09

标签: flutter flutter-layout flutter-widget

enter image description here

上图中的按钮是我正在尝试重现的按钮。轻按时,它应显示不同温度单位的列表。我从一个凸起的按钮开始,发现仅显示一个弹出列表并不容易。所以我将其更改为使用容器。除点击按钮时缺少涟漪效应外,它均有效。如果删除BoxDecoration颜色,它确实可以工作,但是我确实希望按钮的颜色为白色。有什么办法可以做到这一点?

                      Expanded(
                        flex: 2,
                        child: PopupMenuButton(
                          elevation: 3.0,
                          onSelected: (String value) {
                            setState(() {
                              _selection = value;
                            });
                          },
                          child: Container(
                            height: double.maxFinite,
                            width: double.maxFinite,
                            child: Center(
                              child: Text(
                                'C\n\nCelsius',
                                textAlign: TextAlign.center,
                              ),
                            ),
                            decoration: BoxDecoration(
                                color: Colors.white,
                                borderRadius: BorderRadius.circular(10),
                                border: Border.all(
                                    color: Colors.grey[kButtonBorderShade],
                                    width: 0.5)),
                          ),
                          itemBuilder: (BuildContext context) =>
                              <PopupMenuEntry<String>>[
                            const PopupMenuItem<String>(
                              value: 'Value1',
                              child: Text('Choose value 1'),
                            ),
                            const PopupMenuItem<String>(
                              value: 'Value2',
                              child: Text('Choose value 2'),
                            ),
                            const PopupMenuItem<String>(
                              value: 'Value3',
                              child: Text('Choose value 3'),
                            ),
                          ],
                        ),
                      ),

编辑:感谢您的以下回答。我给出的建议完全按照我想要的方式工作。这是我的最后一个按钮:

                      Expanded(
                        flex: 2,
                        child: ClipRRect(
                          borderRadius: BorderRadius.all(
                            Radius.circular(10),
                          ),
                          child: Material(
                            color: Colors.white,
                            child: PopupMenuButton(
                              onSelected: (String value) {
                                setState(() {
                                  _selection = value;
                                });
                              },
                              child: Container(
                                decoration: BoxDecoration(
                                    borderRadius: BorderRadius.circular(10),
                                    border: Border.all(
                                        color:
                                            Colors.grey[kButtonBorderShade],
                                        width: 0.5)),
                                child: Center(
                                  child: Text(
                                    'C\n\nCelsius',
                                    textAlign: TextAlign.center,
                                  ),
                                ),
                              ),
                              itemBuilder: (BuildContext context) =>
                                  <PopupMenuEntry<String>>[
                                const PopupMenuItem<String>(
                                  value: 'Value1',
                                  child: Text('Choose value 1'),
                                ),
                                const PopupMenuItem<String>(
                                  value: 'Value2',
                                  child: Text('Choose value 2'),
                                ),
                                const PopupMenuItem<String>(
                                  value: 'Value3',
                                  child: Text('Choose value 3'),
                                ),
                              ],
                            ),
                          ),
                        ),
                      ),

1 个答案:

答案 0 :(得分:2)

您可以实现将PopupMenuButtonMaterial包裹起来,然后将color属性放在Material小部件中而不是BoxDecoration中,如下所示:< / p>

Material(
    color: Colors.white,
    child: PopupMenuButton(...