【问题标题】:Flutter show image only if it exists in array颤振仅在数组中存在时才显示图像
【发布时间】:2021-06-17 00:27:51
【问题描述】:

在我的widget 里面我有一些images。这些images 有一个position。我想检查是否存在具有特定位置的图像,显​​示它。否则我的GestureDetectorchild 应该什么都不是。

这是我尝试过的:

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,
      ),

但是如果没有 imageImagePosition.ONE ,我会得到一个 Bad State: No elemnt- 错误。

我在这里错过了什么?

如果您需要更多详细信息,请告诉我!

【问题讨论】:

  • 您到底想做什么?您要检查图像位置或图像可空性吗?
  • @JiteshMohite 有点但不完全
  • @Kudos 如果图像存在,则应该有一个具有该图像的孩子。否则应该没有孩子。我更新了我的问题

标签: image flutter dart widget


【解决方案1】:

尝试用: SizedBox.shrink() 更新最后的: null。我认为问题在于您将null 作为child 提供给GestureDetector,以防没有图像

【讨论】:

  • 你能分享堆栈跟踪吗?
【解决方案2】:

我实际上并不是因为没有您的错误的所有堆栈跟踪,而是尝试将 null 替换为空 Container() 如果仍然无法正常工作,请提供您所有的堆栈跟踪以获得进一步的帮助

【讨论】:

    猜你喜欢
    • 2016-08-24
    • 2020-11-15
    • 2022-01-08
    • 1970-01-01
    • 2019-08-30
    • 2019-08-01
    • 1970-01-01
    相关资源
    最近更新 更多