Mark
Mark

Reputation: 3849

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!

Upvotes: 2

Views: 1433

Answers (2)

West
West

Reputation: 2570

You can disable by setting direction like this,

direction: DismissDirection.none

Upvotes: 5

caiopo
caiopo

Reputation: 499

You can use the confirmDismiss parameter to veto the dismissal.

Example:

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

Upvotes: 1

Related Questions