首页 > 解决方案 > Flutter:设置按钮高度以填充容器

问题描述

我一直试图让我的按钮来填充我的容器。 在此处输入图像描述

但是从上图可以看出,左边的按钮(红色)显然没有填满整个容器(绿色)。我可以通过向 MaterialButton 添加高度来解决这个问题,但恕我直言,这不是最好的解决方案,因为设备的高度可能会有所不同。你能帮我解决这个问题吗?谢谢。

这是我的代码:

        Row(
          children: <Widget>[
            Expanded(
              child: Container(
                color: Colors.green,
                child: MaterialButton(
                  color: Colors.red,
                  child: Text("Button A"),
                  onPressed: () {},
                ),
              ),
            ),
            Expanded(
              child: MaterialButton(
                color: Colors.blue,
                child: Text("Button B"),
                onPressed: () {},
              ),
            ),
          ],
        ),

标签: flutter

解决方案


只需添加 -materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,MaterialButton

Row(
            children: <Widget>[
              Expanded(
                child: Container(
                  color: Colors.green,
                  child: MaterialButton(
                    materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,  // Add this
                    color: Colors.red,
                    child: Text("Button A"),
                    onPressed: () {},
                  ),
                ),
              ),
              Expanded(
                child: MaterialButton(
                  materialTapTargetSize: MaterialTapTargetSize.shrinkWrap, // Add this
                  color: Colors.blue,
                  child: Text("Button B"),
                  onPressed: () {},
                ),
              ),
            ],
          ),

推荐阅读