首页 > 解决方案 > 在 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)上给出了宽度和高度,所以堆栈的孩子应该能够从那里推断出父级的宽度和高度,对吧?

标签: flutter

解决方案


推荐阅读