首页 > 解决方案 > 颤振不同的轴单元

问题描述

如何处理使用多种单位类型(空间百分比、像素数等)来调整小部件的大小?一个等效的 CSS 示例是div{width: 70%; height: 50px}

标签: flutter

解决方案


可以做这样的事情:

        Container(
          width: MediaQuery.of(context).size.width * 0.50, // this would be 50% of screen width
          height: 50,
        )

另一种选择是:

        LayoutBuilder(
            builder: (BuildContext context, BoxConstraints constraints) {
                return Container(
                  width: constraints.maxWidth * 0.50, // 50% of parent size
                  height: 50,
                );
            }
        )

更多信息:按屏幕宽度/高度百分比调整元素大小


推荐阅读