flutter - 在 Flutter 中,如何以固定尺寸组合 Stack 小部件,然后将其缩放到设备的屏幕尺寸?
问题描述
例如,我想创建一个组成某种图像的堆栈。例如:
var composedImage = Container(
width: 700,
height: 400,
child: Stack(children: [
Container(width: 700, height: 400, color: Colors.gray),
Positioned(left: 300, top: 100, child: Container(width: 50, height: 200), color: Colors.red),
Positioned(left: 500, top: 200, child: Image.network(url), color: Colors.blue),
Positioned(left: 0, top: 40, child: Text(user.name)),
])
);
然后我想将其缩放到设备的大小:
var innerWidth = MediaQuery.of(this).size.shortestSize;
var scale = innerWidth / 700;
var result = Transform.scale(scale: scale, child: composedImage);
但是 Stack 里面的孩子给了我这个错误:
════════ Exception caught by widgets library ═══════════════════════════════════════════════════════
Incorrect use of ParentDataWidget.
════════════════════════════════════════════════════════════════════════════════════════════════════
为什么会这样?我很确定我已经在父级(700、400)上给出了宽度和高度,所以堆栈的孩子应该能够从那里推断出父级的宽度和高度,对吧?
解决方案
推荐阅读
- javascript - ajax 成功调用控制器方法 laravel 时数据库不更新
- angular - 在 Angular5 中使用来自 highcharts-editor 的 ModalEditor
- c# - 连接时如何放置取消按钮
- java - java - 从任意位置删除数组元素并移动剩余元素
- ibm-cloud - 如何与 SharePoint 上的自定义列表同步
- wpf - RadGridView GridViewSelectColumn 即使在禁用时仍然可选择
- html - 如何使用 Bootstrap4 和/或 flex 将内容居中并设置页脚?
- mysql - 在 Windows 服务器和 Ubuntu 上运行 Mysql 8.0 - 比较
- ios - Swift - 在数组中添加粒度(2 x 2、5 x 5 等)
- python - 在 Raspberry Pi Zero 上安装 chromium 或 Firefox webdriver 的可靠方法