flutter 缩放和可拖动的独立图形

eiee3dmh  于 2023-01-14  发布在  Flutter
关注(0)|答案(1)|浏览(132)

我正在尝试实现,使两个圆圈同时缩放,但也允许他们被单独拖动。
我已经实现了一些添加圆圈和缩放功能,但拖动功能是我的问题。
我是一个新的使用flutter。
下面是我的代码:
https://github.com/SantyTaco/flutter_app_zoom_example
代码的主要部分如下:

void _handleScaleStart(ScaleStartDetails details) {
setState(() {
  _startingFocalPoint = details.focalPoint;
  _previousOffset = _offset;
  _previousZoom = _zoom;
});
}


void _handleScaleUpdate(ScaleUpdateDetails details) {
    setState(() {
      _zoom = _previousZoom * details.scale;

      // Ensure that item under the focal point stays in the same place despite zooming
      final Offset normalizedOffset = (_startingFocalPoint - _previousOffset) / _previousZoom;
      _offset = details.focalPoint - normalizedOffset * _zoom;
    });
  }



@override
  Widget build(BuildContext context) {
    return Stack(
        fit: StackFit.expand,
        children: <Widget>[
          GestureDetector(
              onScaleStart: _scaleEnabled ? _handleScaleStart : null,
              onScaleUpdate: _scaleEnabled ? _handleScaleUpdate : null,
              child: CustomPaint(
                  painter: _GesturePainter(
                      zoom: _zoom,
                      offset: _offset,
                      scaleEnabled: _scaleEnabled,
                  ),
                child: CustomPaint(
                    painter: GesturePainter2(
                      zoom: _zoom,
                      offset: _offset,
                      scaleEnabled: _scaleEnabled,
                    ),
              )
            ),
          )
        ]
    );
  }
}

有人能帮我解决吗?

czq61nw1

czq61nw11#

请尝试改用localFocalPoint。

void _handleScaleStart(ScaleStartDetails details) {
setState(() {
  _startingFocalPoint = details.localFocalPoint;
  _previousOffset = _offset;
  _previousZoom = _zoom;
});
}


void _handleScaleUpdate(ScaleUpdateDetails details) {
    setState(() {
      _zoom = _previousZoom * details.scale;

      // Ensure that item under the focal point stays in the same place despite zooming
      final Offset normalizedOffset = (_startingFocalPoint - _previousOffset) / _previousZoom;
      _offset = details.localFocalPoint - normalizedOffset * _zoom;
    });
  }

希望这个有用。

相关问题