1 year ago

#351952

test-img

De_baha

Scroll and controller priority flutter

i have a CupertinoModalBottomSheet that is closing by scrolling down. And in this page i have image that i can pinch by two fingers. Is there any way to change priority to pinching when two fingers on screen. Because when i put two fingers on screen i can still close the page by scrolling down until i start pinching.

class PinchZoom extends StatefulWidget {
  final Widget child;

  PinchZoom({
    Key? key,
    required this.child,
  }) : super(key: key);

  @override
  State<PinchZoom> createState() => _PinchZoomState();
}

class _PinchZoomState extends State<PinchZoom>
    with SingleTickerProviderStateMixin {
  late TransformationController controller;
  late AnimationController animationController;
  Animation<Matrix4>? animation;
  Map<int, Offset> touchPositions = <int, Offset>{};

  final double minScale = 1;
  final double maxScale = 2;

  double scale = 1;
  OverlayEntry? entry;

  @override
  void initState() {
    super.initState();

    controller = TransformationController();
    animationController = AnimationController(
      vsync: this,
      duration: Duration(milliseconds: 200),
    )
      ..addListener(() => controller.value = animation!.value)
      ..addStatusListener((status) {
        if (status == AnimationStatus.completed) {
          removeOverlay();
        }
      });
  }

  @override
  void dispose() {
    controller.dispose();
    animationController.dispose();

    super.dispose();
  }

  @override
  Widget build(BuildContext context) =>
      BlocBuilder<DishInfoCubit, DishInfoState>(
        builder: (_, _state) {
          final dishCubit = BlocProvider.of<DishInfoCubit>(context);

          return Builder(
            builder: (context) => Listener(
              onPointerMove: (opm) {
                savePointerPosition(opm.pointer, opm.position);
                if (touchPositions.length > 1) {
                  dishCubit.setIsPinchInProgress(true);
                }
              },
              onPointerDown: (opm) {
                savePointerPosition(opm.pointer, opm.position);
                if (touchPositions.length > 1) {
                  dishCubit.setIsPinchInProgress(true);
                }
              },
              onPointerCancel: (opc) {
                clearPointerPosition(opc.pointer);
                if (touchPositions.length < 2) {
                  dishCubit.setIsPinchInProgress(false);
                }
              },
              onPointerUp: (opc) {
                clearPointerPosition(opc.pointer);
                if (touchPositions.length < 2) {
                  dishCubit.setIsPinchInProgress(false);
                }
              },
              child: InteractiveViewer(
                transformationController: controller,
                clipBehavior: Clip.none,
                panEnabled: false,
                minScale: minScale,
                maxScale: maxScale,
                onInteractionStart: (details) {
                  print("trying to start anim");
                  if (details.pointerCount < 2) return;
                  animationController.stop();
                },
                onInteractionEnd: (details) {
                  if (details.pointerCount != 1) return;

                  resetAnimation();
                  dishCubit.setIsPinchInProgress(false);
                },
                child: AspectRatio(
                  aspectRatio: 1,
                  child: widget.child,
                ),
              ),
            ),
          );
        },
      );

  void removeOverlay() {
    entry?.remove();
    entry = null;
  }

  void resetAnimation() {
    animation = Matrix4Tween(
      begin: controller.value,
      end: Matrix4.identity(),
    ).animate(
      CurvedAnimation(parent: animationController, curve: Curves.easeInOut),
    );

    animationController.forward(from: 0);
  }

  void savePointerPosition(int index, Offset position) {
    setState(() {
      touchPositions[index] = position;
    });
  }

  void clearPointerPosition(int index) {
    setState(() {
      touchPositions.remove(index);
    });
  }
}

flutter

scroll

scrollcontroller

0 Answers

Your Answer

Accepted video resources