【发布时间】:2018-03-26 15:00:03
【问题描述】:
我试图在 Flutter 中实现以下目标,但我失败了
<style>
#square {
display: block;
width: 300px;
height: 300px;
overflow: hidden;
}
#square img {
position: absolute;
}
</style>
<body>
<div id="square">
<img src="whatever.jpg" style="left:-5%;top:-10%;width:130%" />
</div>
</body>
这个想法是“平移/缩放”图像,使其不依赖于容器的尺寸(这就是我放置百分比样式的原因)。
我试图通过 CustomPainter 来实现这一点,但我无法确定要传递给 paintImage() 方法的绘制矩形。画布的左上角似乎与要绘制的容器的左上角不对应。
这里是部分代码:
class _ImagePainter extends CustomPainter {
const _ImagePainter({this.image, this.leftPercent, this.topPercent, this.widthPercent});
final ui.Image image;
final double leftPercent;
final double topPercent;
final double widthPercent;
//
// How to compute the theOffset ???
//
Offset offset;
@override
void paint(Canvas canvas, Size size) {
paintImage(canvas: canvas, rect: offset & (size * widthPercent), image: image, fit:BoxFit.contain, alignment: Alignment.topLeft);
}
@override
bool shouldRepaint(_ZoomableImagePainter old) {
return old.image != image || old.leftPercent != leftPercent || old.topPercent != topPercent || old.widthPercent != widthPercent;
}
}
我现在已经挣扎了好几个小时......
谁能帮帮我?
非常感谢
【问题讨论】:
标签: flutter