简体   繁体   中英

Is it possible to disable a dismissible in Flutter?

I'm building an imageviewer which is built on an InteractiveViewer . The InteractiveViewer is wrapped in a Dismissible that will dismiss the widget when the image is swiped up.

        Listener(

          onPointerMove: (PointerMoveEvent event) {

            if (_transformationController.value.getMaxScaleOnAxis() > 1.0) {       
                     _canBeDismissed = false; }
            else { _canBeDismissed = true;  }

          },
          child: AnimatedOpacity(
            opacity:_opacity,
            duration: const Duration(milliseconds: 200),
            child: Dismissible(
              //direction: DismissDirection.vertical,
              direction: (_canBeDismissed == true) ? DismissDirection.vertical : DismissDirection.vertical,
              key: daKey,
              onDismissed: (_) => Navigator.of(context).pop(),
              child: GestureDetector(
                onDoubleTapDown: _handleDoubleTapDown,
                onDoubleTap: _handleDoubleTap,
                child: InteractiveViewer(
                  transformationController: _transformationController,
                  child:
                  Container(
                    alignment: Alignment.center,
                    decoration: BoxDecoration(
                      image:
                      DecorationImage(
                        image:
                        CachedNetworkImageProvider(image),
                        fit: BoxFit.contain,
                      ),
                    ),
                  ),
                ),
              ),
            ),
          ),
        )
    );

However, when the image is zoomed in and a user tries to pan the image, it will dismiss the image rather then let the user pan the image.

So I was thinking of simply disabling the Dismissible while an image is zoomed. I can detect when an image is zoomed by using a Listener , and setting my variable _canBeDismissed to true or false-- that is not a problem.

The problem is how do I programmatically disable the dismissible function?

I tried to mess with the dismiss direction and have it set to null as follows:

direction: (_canBeDismissed == true) ? DismissDirection.vertical : null,

But I guess you aren't allowed to set it to null so that didn't work. I was hoping there is some kind of enabled flag that would solve my problems. Any ideas? Or alternative ways of accomplishing my ultimate goal which is to have a image widget that a user can zoom and pan but also dismiss by swiping up, similar to twitter. (I've already tried some of the big repositories on pub.dev but they all seem buggy or are impossible to implement)

Thanks!

You can use the confirmDismiss parameter to veto the dismissal.

Example:

Dismissible(
  confirmDismiss: (direction) async => _canBeDismissed,
  child: // ...
),

您可以通过像这样设置方向来禁用,

direction: DismissDirection.none

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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