Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When I use the PhotoViewGalleryPageOptions.customChild , the scaleState doesn't work #335

Closed
cherrybiu opened this issue Sep 2, 2020 · 14 comments
Labels
awaiting-further-comments question Further information is requested

Comments

@cherrybiu
Copy link

I don't know if I use the controller correctly. but it still didn't work when i have tried many times? could you please pointed out where my fault is?

final item = widget.galleryItems[index];
    return PhotoViewGalleryPageOptions.customChild(
      scaleStateController: scaleStateController,
      child: GestureDetector(
        behavior: HitTestBehavior.translucent,
        onTap: () {
          Navigator.of(context).pop();
        },
        onDoubleTap: () {
          scaleStateController.scaleState = PhotoViewScaleState.zoomedOut;
        },
        child: CustomImage(
          imageSize: ImageSize.original,
          width: MediaQuery.of(context).size.width,
          imageUrl: item,
        )
      ),
      initialScale: 0.5,
      minScale: PhotoViewComputedScale.contained * 1,
      maxScale: PhotoViewComputedScale.covered * 2,
    );
  }

what i want to deal is to zoom the pic by doubleTap

@cherrybiu cherrybiu added the bug Something isn't working label Sep 2, 2020
@renancaraujo renancaraujo removed the bug Something isn't working label Sep 2, 2020
@renancaraujo
Copy link
Member

There is a couple of problems with this usage.

  1. In this way, you are interfering directly with how the scale state cycle inside photo view is handling double tap. We have to ways to customize the double-tap gesture cycle, one of them is passing a custom scaleStateCycle.

  2. zoomedIn and zoomedOut are generic values for any state not controlled by the scale state cycle, one should never set it as one of these two values.

  3. If you intend to set the double-tap gesture to have totally different behavior than the one we have, you have to pass an empty function as scaleStateCycle on PhotoView's constructor and wrap PhotoView in a gesture detector, not use a child.

@renancaraujo renancaraujo added the question Further information is requested label Sep 2, 2020
@cherrybiu
Copy link
Author

cherrybiu commented Sep 3, 2020

There is a couple of problems with this usage.

  1. In this way, you are interfering directly with how the scale state cycle inside photo view is handling double tap. We have to ways to customize the double-tap gesture cycle, one of them is passing a custom scaleStateCycle.
  2. zoomedIn and zoomedOut are generic values for any state not controlled by the scale state cycle, one should never set it as one of these two values.
  3. If you intend to set the double-tap gesture to have totally different behavior than the one we have, you have to pass an empty function as scaleStateCycle on PhotoView's constructor and wrap PhotoView in a gesture detector, not use a child.

Having read your reply, i just want to have the double tap behavior like yours.PhotoViewGalleryPageOptions is normal, but once i added the customChild, set the new scaleStateCycle, the double tap is triggered but the image is still.

@renancaraujo
Copy link
Member

Yeah, the problem is how you did it. You don't need to add an internal GestureDetector to do that.

@cherrybiu
Copy link
Author

cherrybiu commented Sep 3, 2020

I removed my GestureDetector, the only trigged event is the default doubleTap event. it did worked,but the image remains still... And I wonder why i double tapped once, the nextScaleState executed three times.

@renancaraujo
Copy link
Member

Now I see something else.

Your initialScale is a absolute number (0.5) while minScale and maxScale are relative ones (PhotoViewComputedScale.contained and PhotoViewComputedScale.covered).

This is a problem since you cannot determine if half of the size of the image is bigger than half of the container smallest side or smaller than twice the size of the biggest size of the container).

also, I have to ask your code without the gesture detector.

As a resource, you can try to pass childSize to the page options as MediaQuery.of(context).size (I assume you are using the size of the screen as a guide).

@cherrybiu
Copy link
Author

  PhotoViewGalleryPageOptions _buildItem(BuildContext context, int index) {
    final item = widget.galleryItems[index];
    return PhotoViewGalleryPageOptions.customChild(
      scaleStateCycle: customScaleState,
      child: SupermonkeyImage(
        imageSize: ImageSize.original,
        width: MediaQuery.of(context).size.width,
        imageUrl: item,
      ),
      initialScale: PhotoViewComputedScale.contained * 1,
      minScale: PhotoViewComputedScale.contained * 1,
      maxScale: PhotoViewComputedScale.covered * 2,
      childSize: MediaQuery.of(context).size
    );
  }
PhotoViewScaleState customScaleState(PhotoViewScaleState actual) {
  print('actual--');
  print(actual);
  switch (actual) {
    case PhotoViewScaleState.initial:
      return PhotoViewScaleState.covering;
    case PhotoViewScaleState.covering:
      return PhotoViewScaleState.originalSize;
    case PhotoViewScaleState.originalSize:
      return PhotoViewScaleState.initial;
    default:
      return PhotoViewScaleState.initial;
  }
}

when i double tapped, the 'actual' has been printed three times.

@cherrybiu
Copy link
Author

Now I see something else.

Your initialScale is a absolute number (0.5) while minScale and maxScale are relative ones (PhotoViewComputedScale.contained and PhotoViewComputedScale.covered).

This is a problem since you cannot determine if half of the size of the image is bigger than half of the container smallest side or smaller than twice the size of the biggest size of the container).

also, I have to ask your code without the gesture detector.

As a resource, you can try to pass childSize to the page options as MediaQuery.of(context).size (I assume you are using the size of the screen as a guide).

I did a preview demo, and reset the value of initialScale, it works now, and the image's scale changed too~ I will check my code, and find out what my fault is. extremely grateful~~

@cherrybiu cherrybiu reopened this Sep 4, 2020
@cherrybiu
Copy link
Author

Unfortunately, I have an another question..if I set the initialValue is lower than container * 1, or bigger than it, it works now.But once I set it container * 1, it remains unchanged, cause doubleTap recovered it to originalSize. Then how can I zoom out when I first preview it :(

@renancaraujo
Copy link
Member

It is really hard for me to understand to me what you are trying to achieve here. What is "container * 1"?

@cherrybiu
Copy link
Author

Due to my limited English, I am sorry for what I described.Please let me re-describe my question:

Once I set the initialScale is lower than PhotoViewComputedScale.contained * 1.0 or bigger than it, the image will change bigger or smaller which means double tap works.

but once I set the initialScale to PhotoViewComputedScale.contained * 1.0. minScale is PhotoViewComputedScale.contained * 1.0 too, and the maxScale is PhotoViewComputedScale.covered * 2, When I double tapped the image, the nextScaleStateCycle was triggered, but the image didn't change. I guess it's because nextScaleStateCycle recovered the scale to originalSize.

I need to set the image fit the screen's width when I firstly previewed the image, it's necessary to set PhotoViewComputedScale.contained * 1.0 . So how can I change the image when I set the initialScale PhotoViewComputedScale.contained * 1.0 ?
Here is my code:

PhotoViewGalleryPageOptions _buildItem(BuildContext context, int index) {
 final item = widget.galleryItems[index];
 return PhotoViewGalleryPageOptions.customChild(
   scaleStateController: _controller,
   child: Container(
     alignment: Alignment.center,
     child: Scrollbar(
         child: SingleChildScrollView(
       scrollDirection: Axis.vertical,
       child: Container(
         child: CustomImage(
                     imageSize: ImageSize.original,
                     imageUrl: item,
                     fit: BoxFit.fitWidth,
                     alignment: Alignment.center,
                     placeholder: (context, url) {
                       return CustomImage(
                         width: MediaQuery.of(context).size.width,
                         imageUrl: item,
                         fit: BoxFit.fitWidth,
                         alignment: Alignment.center,
                       );
                     },
                   ),
     )),
   ),
   ),
   scaleStateCycle: nextScaleStateCycle,
   initialScale: PhotoViewComputedScale.contained * 1,
   minScale: PhotoViewComputedScale.contained * 1,
   maxScale: PhotoViewComputedScale.covered * 2,
//      childSize: MediaQuery.of(context).size
 );
}

PhotoViewScaleState nextScaleStateCycle(PhotoViewScaleState actual) {
 print('actual--');
 print(actual);
 switch (actual) {
   case PhotoViewScaleState.initial:
     return PhotoViewScaleState.covering;
   case PhotoViewScaleState.covering:
     return PhotoViewScaleState.zoomedOut;
   case PhotoViewScaleState.originalSize:
     return PhotoViewScaleState.initial;
   case PhotoViewScaleState.zoomedIn:
   case PhotoViewScaleState.zoomedOut:
     return PhotoViewScaleState.initial;
   default:
     return PhotoViewScaleState.initial;
 }
}

@CoderLengary
Copy link

这英文。。太绕了。我昨天下的源码,今天才刚刚看的。我猜是你的 nextScaleStateCycle 设置有问题。doubleTab 之后,进入到 covering 状态。你换成 zoomedIn 不知道可不可以 @cherrybiu

case PhotoViewScaleState.initial: return PhotoViewScaleState.zoomedIn;

@cherrybiu
Copy link
Author

cherrybiu commented Sep 28, 2020

这英文。。太绕了。我昨天下的源码,今天才刚刚看的。我猜是你的 nextScaleStateCycle 设置有问题。doubleTab 之后,进入到 covering 状态。你换成 zoomedIn 不知道可不可以 @cherrybiu

case PhotoViewScaleState.initial: return PhotoViewScaleState.zoomedIn;

这些状态都试了,不行的。 不过不用customChild,而直接用PhotoViewGalleryPageOptions,双击就是正常的,但是这个时候imageProvider类型太局限,无法拓展其他很多自定义功能

@renancaraujo
Copy link
Member

Since I dont speak chinese, is this issue still going on? We may change the name of things on #322 since initialScale is not the best one.

@no-response
Copy link

no-response bot commented Nov 22, 2020

Since we got no answer for a while, I am automatically closing this issue for the sake of organization. If there is any news on that, feel free to retake the discussion.

@no-response no-response bot closed this as completed Nov 22, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting-further-comments question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants