flutter - 如何使容器的宽度与其高度匹配
问题描述
我有一个Container
基于其父级的高度,它Expanded
的 flex 为 2。我需要高度和宽度相同。我不想创建固定宽度,例如width: 100.0
. 是否可以使宽度与Container
内部的高度匹配Expanded
?
解决方案
为此,您必须使用LayoutBuilder
,这是最少的代码:
Column(
children: <Widget>[
Expanded(
flex: 2,
child: LayoutBuilder(
builder: (_, constraints) {
return Container(
color: Colors.blue,
width: constraints.maxHeight, // width is made equal to height
);
},
),
),
Expanded(
flex: 9,
child: Container(color: Colors.orange),
),
],
)
编辑:正如@pskink 和其他提到的你也可以使用AspectRatio
AspectRatio(
aspectRatio: 1,
child: Container(color: Colors.blue),
)
推荐阅读
- c# - LookupAccountSid() 在 Server 2016 上引发 System.AccessViolationException
- javascript - 是否可以使用 Web Auido API 在不扭曲声音的情况下向 MediaElementAudioSourceNode 添加效果?
- python - Jinja 2,列不适合
- python - 获取错误“utf-8”编解码器无法解码位置 15456 中的字节 0xa0:尝试以阿拉伯语读取 csv 文件时起始字节无效
- azure-ad-b2c - 是否可以从 AAD B2C 为 SAML IdP 返回 JWT 令牌?
- c - 在 Linux 上使用新模块编译时出现问题
- mongodb - MongoDB 子文档使用 NestJs 获取具有文档计数的不同文档
- json - 如何将变量添加到现有 JSON 数组的所有项目?
- php - 为什么这个php数组函数给我一个错误
- python - pandas.api.extensions.register_dataframe_accessor 如何在数据帧切片上保留实例属性