Merge pull request 'Ignore swipe when it starts in system gesture insets' (#234) from EpicKiwi/swipe_to_action:system-gesture-insets into master

Reviewed-on: #234
Reviewed-by: Inex Code <inex@noreply.localhost>
This commit is contained in:
Inex Code 2025-01-02 14:44:18 +00:00
commit e567c39d20

View file

@ -204,6 +204,14 @@ class _SwipeableState extends State<Swipeable> 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;
// 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;
@ -237,8 +245,22 @@ class _SwipeableState extends State<Swipeable> with TickerProviderStateMixin, Au
}
void _handlePointerDown(PointerDownEvent event) {
final xPos = event.position.dx;
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!;
}
if(validTouch && _maxX != null){
validTouch = xPos < _maxX!;
}
setState(() {
_isTouch = widget.allowedPointerKinds.contains(event.kind);
_isTouch = validTouch;
});
}
@ -429,6 +451,21 @@ class _SwipeableState extends State<Swipeable> 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(_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;
});
});
}
}
Widget content = SlideTransition(
position: _moveAnimation,
child: widget.child,