Skip to content

Commit b3a23a7

Browse files
committed
🔧 Deny trackpad pointer devices to prevent erratic drag events when interacting with TransformableBox.
1 parent b07b497 commit b3a23a7

File tree

1 file changed

+35
-1
lines changed

1 file changed

+35
-1
lines changed

packages/flutter_box_transform/lib/src/transformable_box.dart

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import 'dart:ui';
2+
13
import 'package:box_transform/box_transform.dart';
24
import 'package:flutter/material.dart';
35

@@ -287,6 +289,8 @@ class TransformableBox extends StatefulWidget {
287289
class _TransformableBoxState extends State<TransformableBox> {
288290
late TransformableBoxController controller;
289291

292+
bool isLegalGesture = false;
293+
290294
@override
291295
void initState() {
292296
super.initState();
@@ -396,12 +400,23 @@ class _TransformableBoxState extends State<TransformableBox> {
396400

397401
/// Called when the handle drag starts.
398402
void onHandlePanStart(DragStartDetails event, HandlePosition handle) {
403+
// Two fingers were used to start the drag. This produces issues with
404+
// the box drag event. Therefore, we ignore it.
405+
if (event.kind == PointerDeviceKind.trackpad) {
406+
isLegalGesture = false;
407+
return;
408+
} else {
409+
isLegalGesture = true;
410+
}
411+
399412
controller.onResizeStart(event.localPosition);
400413
widget.onResizeStart?.call(handle, event);
401414
}
402415

403416
/// Called when the handle drag updates.
404417
void onHandlePanUpdate(DragUpdateDetails event, HandlePosition handle) {
418+
if (!isLegalGesture) return;
419+
405420
final UIResizeResult result = controller.onResizeUpdate(
406421
event.localPosition,
407422
handle,
@@ -431,6 +446,8 @@ class _TransformableBoxState extends State<TransformableBox> {
431446

432447
/// Called when the handle drag ends.
433448
void onHandlePanEnd(DragEndDetails event, HandlePosition handle) {
449+
if (!isLegalGesture) return;
450+
434451
controller.onResizeEnd();
435452
widget.onResizeEnd?.call(handle, event);
436453
widget.onMinWidthReached?.call(false);
@@ -443,6 +460,8 @@ class _TransformableBoxState extends State<TransformableBox> {
443460
}
444461

445462
void onHandlePanCancel(HandlePosition handle) {
463+
if (!isLegalGesture) return;
464+
446465
controller.onResizeEnd();
447466
widget.onResizeCancel?.call(handle);
448467
widget.onMinWidthReached?.call(false);
@@ -456,12 +475,23 @@ class _TransformableBoxState extends State<TransformableBox> {
456475

457476
/// Called when the box drag event starts.
458477
void onDragPanStart(DragStartDetails event) {
478+
// Two fingers were used to start the drag. This produces issues with
479+
// the box drag event. Therefore, we ignore it.
480+
if (event.kind == PointerDeviceKind.trackpad) {
481+
isLegalGesture = false;
482+
return;
483+
} else {
484+
isLegalGesture = true;
485+
}
486+
459487
controller.onDragStart(event.localPosition);
460488
widget.onDragStart?.call(event);
461489
}
462490

463491
/// Called when the box drag event updates.
464492
void onDragPanUpdate(DragUpdateDetails event) {
493+
if (!isLegalGesture) return;
494+
465495
final UIMoveResult result = controller.onDragUpdate(
466496
event.localPosition,
467497
);
@@ -472,11 +502,15 @@ class _TransformableBoxState extends State<TransformableBox> {
472502

473503
/// Called when the box drag event ends.
474504
void onDragPanEnd(DragEndDetails event) {
505+
if (!isLegalGesture) return;
506+
475507
controller.onDragEnd();
476508
widget.onDragEnd?.call(event);
477509
}
478510

479511
void onDragPanCancel() {
512+
if (!isLegalGesture) return;
513+
480514
controller.onDragEnd();
481515
widget.onDragCancel?.call();
482516
}
@@ -494,7 +528,7 @@ class _TransformableBoxState extends State<TransformableBox> {
494528

495529
if (widget.draggable) {
496530
content = GestureDetector(
497-
behavior: HitTestBehavior.opaque,
531+
behavior: HitTestBehavior.translucent,
498532
onPanStart: onDragPanStart,
499533
onPanUpdate: onDragPanUpdate,
500534
onPanEnd: onDragPanEnd,

0 commit comments

Comments
 (0)