简体   繁体   English

用 CarouselSlider 颤振 InteractiveViewer 很难捏缩放

[英]flutter InteractiveViewer with CarouselSlider very difficult to pinch zoom

I have a flutter app that show images with a CarouselSlider and InteractiveViewer so the user can pinch to zoom in and out.我有一个颤动的应用程序,它使用 CarouselSlider 和 InteractiveViewer 显示图像,因此用户可以通过捏合来放大和缩小。 the problem is that it's very difficult to do this, normally the pinch movement make the CarouselSlider to slide between photos.问题是这样做非常困难,通常捏合动作会使 CarouselSlider 在照片之间滑动。 One solution is to disable slide with neverscrollablescrollphysics() but I dont wanto to do that.一种解决方案是使用 neverscrollablescrollphysics() 禁用幻灯片,但我不想这样做。

here is part of the code:这是代码的一部分:

CarouselSlider(
          items: widget.photos.map((i) {
            return Container(
              margin: EdgeInsets.all(10),
              child: InteractiveViewer(
                scaleEnabled: true,
                minScale: 0.1,
                maxScale: 10.0,
                child: Image.network(i),
              ),
            );
          }).toList(),
          options: CarouselOptions(
            enableInfiniteScroll: false,
            height: MediaQuery.of(context).size.height,
            disableCenter: true,
            viewportFraction: 1.0,
            initialPage: widget.position,
            onPageChanged: (index, reason) {
              setState(() {
                widget.position = index;
              });
            },
          ),
        ),

this is a first quick fix (i will update it soon)这是第一个快速修复(我会尽快更新)

You can copy the CarouselSlider-Class and name it like MyCarouselSlider您可以复制 CarouselSlider-Class 并将其命名为 MyCarouselSlider

Then edit the getGestrureWrapper-Method like this然后像这样编辑getGestrureWrapper-Method

  Widget getGestureWrapper(Widget child) {
    Widget wrapper;
    if (widget.options.height != null) {
      wrapper = Container(height: widget.options.height, child: child);
    } else {
      wrapper = AspectRatio(aspectRatio: widget.options.aspectRatio, child: child);
    }
    return GestureDetector(
      child: NotificationListener(
        onNotification: (dynamic notification) {
          if (widget.options.onScrolled != null && notification is ScrollUpdateNotification) {
            widget.options.onScrolled(carouselState.pageController.page);
          }
          return false;
        },
        child: wrapper,
      ),
      behavior: HitTestBehavior.opaque,
      dragStartBehavior: DragStartBehavior.start,
    );

    // return RawGestureDetector(
    //   behavior: HitTestBehavior.translucent,
    //   gestures: {
    //     _MultipleGestureRecognizer: GestureRecognizerFactoryWithHandlers<_MultipleGestureRecognizer>(
    //         () => _MultipleGestureRecognizer(), (_MultipleGestureRecognizer instance) {
    //       instance.onStart = (_) {
    //         onPanDown();
    //       };
    //       instance.onDown = (_) {
    //         onPanDown();
    //       };
    //       instance.onEnd = (_) {
    //         onPanUp();
    //       };
    //       instance.onCancel = () {
    //         onPanUp();
    //       };
    //     }),
    //   },
    //   child: NotificationListener(
    //     onNotification: (dynamic notification) {
    //       if (widget.options.onScrolled != null && notification is ScrollUpdateNotification) {
    //         widget.options.onScrolled(carouselState.pageController.page);
    //       }
    //       return false;
    //     },
    //     child: wrapper,
    //   ),
    // );
  }

And use your newly created MyCarouselSlider-Class instead.并使用您新创建的 MyCarouselSlider-Class 代替。 Important: this is not a final solution.重要提示:这不是最终解决方案。

I'm unsure whats the problem with the RawGestureDetector.我不确定 RawGestureDetector 有什么问题。 If I find a better solution I will update this post.如果我找到更好的解决方案,我会更新这篇文章。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM