简体   繁体   English

Flutter InteractiveViewer class 如何在加载新图像时进行缩放

[英]Flutter InteractiveViewer class how to Zoom/scale out when loading a new Image

I love the InteractiveViewer class , it allows me the user to pinch out to zoom in on Image Widgets.我喜欢InteractiveViewer class ,它让我的用户可以放大图像小部件。

However, it's given me a bug.但是,它给了我一个错误。 If the user remains zoomed in on an image, then exits my DetailsScreen containing that image, then activates DetailScreen again with new details and image, then the new image is zoomed in by the same amount as the old one.如果用户仍然放大图像,然后退出包含该图像的 DetailsScreen,然后使用新的详细信息和图像再次激活 DetailScreen,则新图像将放大与旧图像相同的数量。 The InteractiveViewer's scale is not being reset. InteractiveViewer 的比例未重置。

I've tried to reset it by giving the InteractiveViewer a transformationController: imageZoomController, parameter in its constructor.我试图通过在其构造函数中为 InteractiveViewer 提供一个transformationController: imageZoomController,参数来重置它。 Then when I exit the DetailScreen I tried imageZoomController.dispose() , however this causes lots of crashes... hmmmph.然后,当我退出 DetailScreen 时,我尝试了imageZoomController.dispose() ,但这会导致很多崩溃......嗯。

It just needs to reset to being fully zoomed out, I need to retrigger InteractiveViewer's initial minScale: 1, parameter.它只需要重置为完全缩小,我需要重新触发 InteractiveViewer 的初始minScale: 1,参数。

I don't favour using InteractiveViewer's onInteractionEnd parameter, because I find it annoying when I've intentionally zoomed in on an image and it jumps away from it.我不赞成使用 InteractiveViewer 的onInteractionEnd参数,因为当我故意放大图像并且它跳离它时,我觉得它很烦人。

Help appreciated.帮助表示赞赏。

You can try this package: https://pub.dev/packages/photo_view你可以试试这个package: https://pub.dev/packages/photo_view

It also allows us to double click to animate to init scale without even touch the controller.它还允许我们双击动画以初始化缩放,甚至无需触摸 controller。

Usage:用法:

@override
Widget build(BuildContext context) {
  return Container(
    child: PhotoView(
      imageProvider: AssetImage("assets/large-image.jpg"),
    )
  );
}

Found a solution through experimentation, it appears to work in the emulator, not quite sure why, but problem solved I hope.通过实验找到了一个解决方案,它似乎在模拟器中工作,不太清楚为什么,但我希望问题解决了。

imageZoomController.value = Matrix4.identity();
Navigator.of(context).pop();

When I exit the DetailsScreen twiddling the imageZoomController.value seems to reset the InteractiveViewer to a zoom of 1.当我退出 DetailsScreen 时,旋转 imageZoomController.value 似乎将 InteractiveViewer 重置为 1 的缩放。

Thanks for the suggestion Thea, if others have my problem & my solution doesn't work, then try Thea's plugin.感谢 Thea 的建议,如果其他人有我的问题并且我的解决方案不起作用,请尝试 Thea 的插件。

I'll provide more context, Widgets etc. in future, my bad.将来我会提供更多的上下文、小部件等,我的错。 Apologies @Nisanth道歉@Nisanth

viewController.value = Matrix4.identity(); viewController.value = Matrix4.identity();

GET YOU TO THE INITIAL ZOOM OF THE PHOTO带您进入照片的初始缩放

TransformationController viewController = TransformationController();

    var initialControllerValue;
    
    InteractiveViewer(
     
        transformationController: viewController,
        onInteractionStart: (details){

            initialControllerValue = viewController.value;
        },
    
        onInteractionEnd: (details){
   
            viewController.value = initialControllerValue;
        },
           

First, check if the image or widget is already zoomed in then take it to default zoom.首先,检查图像或小部件是否已经放大,然后将其设为默认缩放。

            final _transformationController = TransformationController();
            if (_transformationController.value != Matrix4.identity()) {
                  _transformationController.value = Matrix4.identity();
                }

Widget:
        InteractiveViewer(
              transformationController: _transformationController,
              child:Container(),
             ),

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

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