Detecting swipe gestures is essential for creating fluid and intuitive mobile experiences. In Flutter, there are several ways to capture these gestures, but one of the fastest and simplest is through the SwipeDetector package. In this guide, you'll learn everything you need to use it professionally, optimize its performance, and compare it with native alternatives like GestureDetector.
We start from the fact that we previously saw how to use the FractionallySizedBox to create flexible containers with percentage sizes.
✅ What is SwipeDetector in Flutter?
It's designed solely to detect screen swipes—such as left, right, up, and down—and is very simple to implement when you add the swipedetector package.
Swipe Detector is a Flutter package specifically designed to detect swipes in four directions:
- Swipe Right
- Swipe Left
- Swipe Up
- Swipe Down
Its main advantage is that it simplifies gesture detection by eliminating the need to handle displacement, velocity, or drag event calculations. You simply wrap a widget and assign the desired actions.
It is especially useful for:
- Gesture navigation
- Image galleries
- Carousels
- Side panels
- Games in Flutter
Quick gestures without complex animations
Implementing the Swipe Detector
Step 1: Add Dependencies
Add the package in your pubspec.yaml file.
dependencies: swipedetector: #https://pub.dev/packages/swipedetectorPaso 2: Import and Use
import 'package:swipedetector/swipedetector.dart';
SwipeDetector(
child: Container(
width: 200,
height: 200,
color: Colors.blue,
),
onSwipeRight: () {
setState(() {
print("Swiped right");
});
},
),Simply declare SwipeDetector in the body or as a widget. That's it, you can use onSwipeRight, onSwipeLeft, onSwipeUp, onSwipeDown, or all at the same time to detect the desired swipe.
SwipeDetector(
child: Container(
width: 200,
height: 200,
color: Colors.blue,
child: Center(child: Text(_swipeDirection)),
),
onSwipeUp: () {
setState(() {
_swipeDirection = "Swipe Up";
});
},
onSwipeDown: () {
setState(() {
_swipeDirection = "Swipe Down";
});
},
onSwipeLeft: () {
setState(() {
_swipeDirection = "Swipe Left";
});
},
onSwipeRight: () {
setState(() {
_swipeDirection = "Swipe Right";
});
},
),So simple! :) You can also configure it to detect a swipe in only a part of the screen, using:
swipeConfiguration: SwipeConfiguration(
verticalSwipeMinVelocity: 100.0,
verticalSwipeMinDisplacement: 50.0,
verticalSwipeMaxWidthThreshold:100.0,
horizontalSwipeMaxHeightThreshold: 50.0,
horizontalSwipeMinDisplacement:50.0,
horizontalSwipeMinVelocity: 200.0),
),You just need to wrap any widget with SwipeDetector and assign the available callbacks:
- onSwipeLeft
- onSwipeRight
- onSwipeUp
- onSwipeDown
Complete Example: Detecting All Swipe Types (Directions) in Flutter
This code displays the detected swipe type on the screen, ideal for debugging or testing.
String _swipeDirection = "";
SwipeDetector(
child: Container(
width: 200,
height: 200,
color: Colors.blue,
child: Center(child: Text(_swipeDirection)),
),
onSwipeUp: () {
setState(() => _swipeDirection = "Swipe Up");
},
onSwipeDown: () {
setState(() => _swipeDirection = "Swipe Down");
},
onSwipeLeft: () {
setState(() => _swipeDirection = "Swipe Left");
},
onSwipeRight: () {
setState(() => _swipeDirection = "Swipe Right");
},
);This example is optimized for SEO using the terms:
detect swipe Flutter, Flutter swipe gestures, Flutter swipedetector example.⚙️ Configure Swipe Sensitivity (SwipeConfiguration)
One of the advanced features of the package is the customization of gesture sensitivity.
swipeConfiguration: SwipeConfiguration(
verticalSwipeMinVelocity: 100.0,
verticalSwipeMinDisplacement: 50.0,
verticalSwipeMaxWidthThreshold: 100.0,
horizontalSwipeMaxHeightThreshold: 50.0,
horizontalSwipeMinDisplacement: 50.0,
horizontalSwipeMinVelocity: 200.0,
),What is each parameter for?
- Parameter Function
- verticalSwipeMinVelocity Minimum velocity to validate vertical swipe
- verticalSwipeMinDisplacement Minimum distance to confirm the gesture
- verticalSwipeMaxWidthThreshold Maximum width where the vertical event will be detected
- horizontalSwipeMinVelocity Minimum velocity for horizontal swipe
- horizontalSwipeMinDisplacement Minimum horizontal displacement needed
This allows you to:
- Detect swipes only in specific areas
- Avoid false positives
- Configure customized touch areas
Swipe Detector vs GestureDetector (SEO Comparison)
Google often ranks comparisons, so we've included an optimized table:
Feature Swipe Detector GestureDetector
Ease of Use ⭐⭐⭐⭐⭐ ⭐⭐⭐
Native Flutter Support ❌ ✔
Customization Medium High
Complexity Low Medium/High
Ideal For Prototypes, simple apps Advanced apps, animations
Automatic Swipe Detection ✔ ❌ (requires coding)
Package-Free Alternative: Detecting Swipes with GestureDetector
For total control without external dependencies:
GestureDetector(
onHorizontalDragEnd: (details) {
if (details.primaryVelocity! > 0) {
print("Swipe Right");
} else {
print("Swipe Left");
}
},
child: Container(
width: 200,
height: 200,
color: Colors.orange,
),
);This section helps capture secondary keywords like Flutter horizontal drag, Flutter detect swipe without package.
❓ Frequently Asked Questions (SEO-Focused FAQ)
- Is Swipe Detector better than GestureDetector in Flutter?
- For simple cases, yes. For advanced interactions, complex animations, or overlapping gestures, GestureDetector is superior.
- Can I detect swipes within a specific area?
- Yes. Use swipeConfiguration to limit the active zone.
- Does Swipe Detector work in Flutter Web?
- Yes, but sensitivity varies by device. For web, GestureDetector is often recommended.
- Does Swipe Detector work on iOS and Android?
- It works perfectly on both platforms.
- What is the best way to detect swipe in Flutter without a package?
- Use onHorizontalDragEnd, onVerticalDragEnd, or GestureRecognizer.
Conclusion: Is it Worth Using Swipe Detector in Flutter?
Yes, if you are looking for an immediate, simple, and functional solution to detect swipes in your Flutter app. Its implementation speed and ease make it an ideal tool for:
- Prototypes
- Educational apps
- Simple gesture-based interfaces
- Widgets with basic interaction
- For advanced projects, you can always make the jump to GestureDetector or RawGestureDetector.
Next widget, learn how to make a scrollable container in Flutter.
I agree to receive announcements of interest about this Blog.
Let's explore a package that allows us to register the Swipe effect in our Flutter applications.