Browse Source

Improve selection

lucky1213 4 years ago
parent
commit
e1b342f999
1 changed files with 50 additions and 8 deletions
  1. 50
    8
      packages/zefyr/lib/src/widgets/selection.dart

+ 50
- 8
packages/zefyr/lib/src/widgets/selection.dart View File

449
         alignment: Alignment.topLeft,
449
         alignment: Alignment.topLeft,
450
         width: interactiveRect.width,
450
         width: interactiveRect.width,
451
         height: interactiveRect.height,
451
         height: interactiveRect.height,
452
-        child: GestureDetector(
452
+        child: RawGestureDetector(
453
           behavior: HitTestBehavior.translucent,
453
           behavior: HitTestBehavior.translucent,
454
-          dragStartBehavior: DragStartBehavior.start,
455
-          onPanStart: _handleDragStart,
456
-          onPanUpdate: _handleDragUpdate,
454
+          gestures: <Type, GestureRecognizerFactory>{
455
+            CustomPanGestureRecognizer: GestureRecognizerFactoryWithHandlers<
456
+                CustomPanGestureRecognizer>(
457
+              () => CustomPanGestureRecognizer(
458
+                  onPanDown: _handleDragStart, onPanUpdate: _handleDragUpdate),
459
+              (CustomPanGestureRecognizer instance) {},
460
+            ),
461
+          },
457
           child: Padding(
462
           child: Padding(
458
             padding: EdgeInsets.only(
463
             padding: EdgeInsets.only(
459
               left: padding.left,
464
               left: padding.left,
503
     }
508
     }
504
   }
509
   }
505
 
510
 
506
-  void _handleDragStart(DragStartDetails details) {
507
-    _dragPosition = details.globalPosition;
511
+  bool _handleDragStart(Offset details) {
512
+    _dragPosition = Offset(details.dx, details.dy);
513
+    return true;
508
   }
514
   }
509
 
515
 
510
-  void _handleDragUpdate(DragUpdateDetails details) {
511
-    _dragPosition += details.delta;
516
+  void _handleDragUpdate(Offset details) {
517
+    _dragPosition = Offset(details.dx, details.dy);
512
     final globalPoint = _dragPosition;
518
     final globalPoint = _dragPosition;
513
     final paragraph = _scope.renderContext.boxForGlobalPoint(globalPoint);
519
     final paragraph = _scope.renderContext.boxForGlobalPoint(globalPoint);
514
     if (paragraph == null) {
520
     if (paragraph == null) {
606
     );
612
     );
607
   }
613
   }
608
 }
614
 }
615
+
616
+class CustomPanGestureRecognizer extends OneSequenceGestureRecognizer {
617
+  final Function onPanDown;
618
+  final Function onPanUpdate;
619
+
620
+  CustomPanGestureRecognizer({
621
+    @required this.onPanDown,
622
+    @required this.onPanUpdate,
623
+  });
624
+
625
+  @override
626
+  void addPointer(PointerEvent event) {
627
+    if (onPanDown(event.position)) {
628
+      startTrackingPointer(event.pointer);
629
+      resolve(GestureDisposition.accepted);
630
+    } else {
631
+      stopTrackingPointer(event.pointer);
632
+    }
633
+  }
634
+
635
+  @override
636
+  void handleEvent(PointerEvent event) {
637
+    if (event is PointerMoveEvent) {
638
+      onPanUpdate(event.position);
639
+    }
640
+    if (event is PointerUpEvent) {
641
+      stopTrackingPointer(event.pointer);
642
+    }
643
+  }
644
+
645
+  @override
646
+  String get debugDescription => 'customPan';
647
+
648
+  @override
649
+  void didStopTrackingLastPointer(int pointer) {}
650
+}