【发布时间】:2021-06-17 00:27:51
【问题描述】:
在我的widget 里面我有一些images。这些images 有一个position。我想检查是否存在具有特定位置的图像,显示它。否则我的GestureDetector 的child 应该什么都不是。
这是我尝试过的:
child: GestureDetector(
onTap: () => print("tapped 1"),
onScaleStart: (details) {
_startingFocalPoint.value = details.focalPoint;
_previousOffset.value = _offset.value;
_previousZoom.value = _zoom.value;
},
onScaleUpdate: (details) {
_zoom.value = _previousZoom.value * details.scale;
final Offset normalizedOffset =
(_startingFocalPoint.value - _previousOffset.value) /
_previousZoom.value;
_offset.value =
details.focalPoint - normalizedOffset * _zoom.value;
},
child: widget.images.where(
(image) => image.position == ImagePosition.ONE) !=
null
? ClipPath(
clipper: _Position1ClipperImage(),
child: Transform(
transform: Matrix4.identity()
..translate(_offset.value.dx, _offset.value.dy)
..scale(_zoom.value),
child: Image.asset(
widget.images
.firstWhere(
(image) => image.position == ImagePosition.ONE,
)
.path,
width: widget.width,
height: widget.width,
fit: BoxFit.fill),
),
)
: null,
),
但是如果没有 image 和 ImagePosition.ONE ,我会得到一个 Bad State: No elemnt- 错误。
我在这里错过了什么?
如果您需要更多详细信息,请告诉我!
【问题讨论】:
-
您到底想做什么?您要检查图像位置或图像可空性吗?
-
@JiteshMohite 有点但不完全
-
@Kudos 如果图像存在,则应该有一个具有该图像的孩子。否则应该没有孩子。我更新了我的问题