From 3ae1548ba02c131dc512ddfc9226f9ebe5cd20ad Mon Sep 17 00:00:00 2001 From: EpicKiwi Date: Thu, 2 Jan 2025 00:18:26 +0100 Subject: [PATCH 1/3] Ignore swipes initiated from inside system gesture --- lib/src/swipeable.dart | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/lib/src/swipeable.dart b/lib/src/swipeable.dart index 021faec..e29ea06 100644 --- a/lib/src/swipeable.dart +++ b/lib/src/swipeable.dart @@ -204,6 +204,9 @@ class _SwipeableState extends State with TickerProviderStateMixin, Au bool _isTouch = true; + double? _minX; + double? _maxX; + @override bool get wantKeepAlive => _moveController.isAnimating == true; @@ -237,8 +240,20 @@ class _SwipeableState extends State with TickerProviderStateMixin, Au } void _handlePointerDown(PointerDownEvent event) { + final xPos = event.position.dx; + + var validTouch = widget.allowedPointerKinds.contains(event.kind); + + if(validTouch && _minX != null){ + validTouch = xPos > _minX!; + } + + if(validTouch && _maxX != null){ + validTouch = xPos < _maxX!; + } + setState(() { - _isTouch = widget.allowedPointerKinds.contains(event.kind); + _isTouch = validTouch; }); } @@ -429,6 +444,18 @@ class _SwipeableState extends State with TickerProviderStateMixin, Au } } + MediaQueryData? mediaQuery = MediaQuery.maybeOf(context); + if (mediaQuery != null) { + if(_minX == null || _maxX == null){ + WidgetsBinding.instance.addPostFrameCallback((_) { + setState(() { + _minX = mediaQuery.systemGestureInsets.left; + _maxX = mediaQuery.size.width - mediaQuery.systemGestureInsets.right; + }); + }); + } + } + Widget content = SlideTransition( position: _moveAnimation, child: widget.child, From 73cf87b2b59c179c0be2625bd5fcd4be6495227c Mon Sep 17 00:00:00 2001 From: EpicKiwi Date: Thu, 2 Jan 2025 10:16:25 +0100 Subject: [PATCH 2/3] Added some comments Signed-off-by: EpicKiwi --- lib/src/swipeable.dart | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/lib/src/swipeable.dart b/lib/src/swipeable.dart index e29ea06..1738f72 100644 --- a/lib/src/swipeable.dart +++ b/lib/src/swipeable.dart @@ -204,7 +204,9 @@ class _SwipeableState extends State with TickerProviderStateMixin, Au bool _isTouch = true; + // Minimum global X where swipe is allowed to start double? _minX; + // Maximum global X where swipe is allowed to start double? _maxX; @override @@ -244,6 +246,8 @@ class _SwipeableState extends State with TickerProviderStateMixin, Au var validTouch = widget.allowedPointerKinds.contains(event.kind); + // Check if touch was performed after minX and before maxX to avoid system + // gesture insets if(validTouch && _minX != null){ validTouch = xPos > _minX!; } @@ -444,6 +448,8 @@ class _SwipeableState extends State with TickerProviderStateMixin, Au } } + // Get system screen size and system gesture insets + // to avoid starting a swipe in this areas MediaQueryData? mediaQuery = MediaQuery.maybeOf(context); if (mediaQuery != null) { if(_minX == null || _maxX == null){ From b1f20c8b5c0230aa66344a747c47086fca446736 Mon Sep 17 00:00:00 2001 From: EpicKiwi Date: Thu, 2 Jan 2025 15:19:09 +0100 Subject: [PATCH 3/3] Update insets when device width changes --- lib/src/swipeable.dart | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/src/swipeable.dart b/lib/src/swipeable.dart index 1738f72..4b03528 100644 --- a/lib/src/swipeable.dart +++ b/lib/src/swipeable.dart @@ -208,6 +208,9 @@ class _SwipeableState extends State with TickerProviderStateMixin, Au double? _minX; // Maximum global X where swipe is allowed to start double? _maxX; + // Screen width reference that helped to compute _minX and _maxX + // used to check if insets should be updated or not + double? _widthReference; @override bool get wantKeepAlive => _moveController.isAnimating == true; @@ -452,9 +455,10 @@ class _SwipeableState extends State with TickerProviderStateMixin, Au // to avoid starting a swipe in this areas MediaQueryData? mediaQuery = MediaQuery.maybeOf(context); if (mediaQuery != null) { - if(_minX == null || _maxX == null){ + if(_widthReference == null || _widthReference != mediaQuery.size.width){ WidgetsBinding.instance.addPostFrameCallback((_) { setState(() { + _widthReference = mediaQuery.size.width; _minX = mediaQuery.systemGestureInsets.left; _maxX = mediaQuery.size.width - mediaQuery.systemGestureInsets.right; });