Reputation: 101
I have an image Carousel with images i get from an API. Nonetheless I want to switch the images with swiping left and right. Current code works, but having an issue when I want to swipe left (back) it takes two swipes to get back on a previous image... So I'm guessing I have to change the distance in onPanUpdate?
return GestureDetector(
onPanEnd: (DragEndDetails details) {
initial = 0.0;
if (initial > distance) {
_nextImage();
print("next");
}
},
onPanUpdate: (DragUpdateDetails details) {
distance = details.globalPosition.dx - initial;
},
onPanStart: (DragStartDetails details) {
initial = details.globalPosition.dx;
if (initial <= distance) {
_previousImage();
print("previous");
}
},
Upvotes: 0
Views: 5297
Reputation: 54365
You can reference package https://pub.dev/packages/swipedetector or use it directly.
it use onHorizontalDrag to detect and allow Velocity and Displacement
code snippet of swipedetector
onHorizontalDragStart: (dragDetails) {
startHorizontalDragDetails = dragDetails;
},
onHorizontalDragUpdate: (dragDetails) {
updateHorizontalDragDetails = dragDetails;
},
onHorizontalDragEnd: (endDetails) {
double dx = updateHorizontalDragDetails.globalPosition.dx -
startHorizontalDragDetails.globalPosition.dx;
double dy = updateHorizontalDragDetails.globalPosition.dy -
startHorizontalDragDetails.globalPosition.dy;
double velocity = endDetails.primaryVelocity;
if (dx < 0) dx = -dx;
if (dy < 0) dy = -dy;
double positiveVelocity = velocity < 0 ? -velocity : velocity;
full swipe detector code
library swipedetector;
import 'package:flutter/material.dart';
class SwipeConfiguration {
//Vertical swipe configuration options
double verticalSwipeMaxWidthThreshold = 50.0;
double verticalSwipeMinDisplacement = 100.0;
double verticalSwipeMinVelocity = 300.0;
//Horizontal swipe configuration options
double horizontalSwipeMaxHeightThreshold = 50.0;
double horizontalSwipeMinDisplacement = 100.0;
double horizontalSwipeMinVelocity = 300.0;
SwipeConfiguration({
double verticalSwipeMaxWidthThreshold,
double verticalSwipeMinDisplacement,
double verticalSwipeMinVelocity,
double horizontalSwipeMaxHeightThreshold,
double horizontalSwipeMinDisplacement,
double horizontalSwipeMinVelocity,
}) {
if (verticalSwipeMaxWidthThreshold != null) {
this.verticalSwipeMaxWidthThreshold = verticalSwipeMaxWidthThreshold;
}
if (verticalSwipeMinDisplacement != null) {
this.verticalSwipeMinDisplacement = verticalSwipeMinDisplacement;
}
if (verticalSwipeMinVelocity != null) {
this.verticalSwipeMinVelocity = verticalSwipeMinVelocity;
}
if (horizontalSwipeMaxHeightThreshold != null) {
this.horizontalSwipeMaxHeightThreshold = horizontalSwipeMaxHeightThreshold;
}
if (horizontalSwipeMinDisplacement != null) {
this.horizontalSwipeMinDisplacement = horizontalSwipeMinDisplacement;
}
if (horizontalSwipeMinVelocity != null) {
this.horizontalSwipeMinVelocity = horizontalSwipeMinVelocity;
}
}
}
class SwipeDetector extends StatelessWidget {
final Widget child;
final Function() onSwipeUp;
final Function() onSwipeDown;
final Function() onSwipeLeft;
final Function() onSwipeRight;
final SwipeConfiguration swipeConfiguration;
SwipeDetector(
{@required this.child,
this.onSwipeUp,
this.onSwipeDown,
this.onSwipeLeft,
this.onSwipeRight,
SwipeConfiguration swipeConfiguration})
: this.swipeConfiguration = swipeConfiguration == null
? SwipeConfiguration()
: swipeConfiguration;
@override
Widget build(BuildContext context) {
//Vertical drag details
DragStartDetails startVerticalDragDetails;
DragUpdateDetails updateVerticalDragDetails;
//Horizontal drag details
DragStartDetails startHorizontalDragDetails;
DragUpdateDetails updateHorizontalDragDetails;
return GestureDetector(
child: child,
onVerticalDragStart: (dragDetails) {
startVerticalDragDetails = dragDetails;
},
onVerticalDragUpdate: (dragDetails) {
updateVerticalDragDetails = dragDetails;
},
onVerticalDragEnd: (endDetails) {
double dx = updateVerticalDragDetails.globalPosition.dx -
startVerticalDragDetails.globalPosition.dx;
double dy = updateVerticalDragDetails.globalPosition.dy -
startVerticalDragDetails.globalPosition.dy;
double velocity = endDetails.primaryVelocity;
//Convert values to be positive
if (dx < 0) dx = -dx;
if (dy < 0) dy = -dy;
double positiveVelocity = velocity < 0 ? -velocity : velocity;
if (dx > swipeConfiguration.verticalSwipeMaxWidthThreshold) return;
if (dy < swipeConfiguration.verticalSwipeMinDisplacement) return;
if (positiveVelocity < swipeConfiguration.verticalSwipeMinVelocity)
return;
if (velocity < 0) {
//Swipe Up
if (onSwipeUp != null) {
onSwipeUp();
}
} else {
//Swipe Down
if (onSwipeDown != null) {
onSwipeDown();
}
}
},
onHorizontalDragStart: (dragDetails) {
startHorizontalDragDetails = dragDetails;
},
onHorizontalDragUpdate: (dragDetails) {
updateHorizontalDragDetails = dragDetails;
},
onHorizontalDragEnd: (endDetails) {
double dx = updateHorizontalDragDetails.globalPosition.dx -
startHorizontalDragDetails.globalPosition.dx;
double dy = updateHorizontalDragDetails.globalPosition.dy -
startHorizontalDragDetails.globalPosition.dy;
double velocity = endDetails.primaryVelocity;
if (dx < 0) dx = -dx;
if (dy < 0) dy = -dy;
double positiveVelocity = velocity < 0 ? -velocity : velocity;
print("$dx $dy $velocity $positiveVelocity");
if (dx < swipeConfiguration.horizontalSwipeMinDisplacement) return;
if (dy > swipeConfiguration.horizontalSwipeMaxHeightThreshold) return;
if (positiveVelocity < swipeConfiguration.horizontalSwipeMinVelocity)
return;
if (velocity < 0) {
//Swipe Up
if (onSwipeLeft != null) {
onSwipeLeft();
}
} else {
//Swipe Down
if (onSwipeRight != null) {
onSwipeRight();
}
}
},
);
}
}
full example code
import 'package:flutter/material.dart';
import 'package:swipedetector/swipedetector.dart';
void main() => runApp(new MyApp());
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return new MaterialApp(
title: 'Flutter Demo',
theme: new ThemeData(
primarySwatch: Colors.blue,
),
home: new MyHomePage(),
debugShowCheckedModeBanner: false,
);
}
}
class MyHomePage extends StatefulWidget {
@override
_MyHomePageState createState() => new _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
String _swipeDirection = "";
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(),
body: Center(
child: Container(
child: Row(
children: <Widget>[
Expanded(
child: SwipeDetector(
child: Card(
child: Container(
padding: EdgeInsets.only(
top: 80.0,
bottom: 80.0,
left: 16.0,
right: 16.0,
),
child: Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Text(
'Swipe Me!',
style: TextStyle(
fontSize: 40.0,
),
),
Text(
'$_swipeDirection',
style: TextStyle(),
),
],
),
),
),
onSwipeUp: () {
setState(() {
_swipeDirection = "Swipe Up";
});
},
onSwipeDown: () {
setState(() {
_swipeDirection = "Swipe Down";
});
},
onSwipeLeft: () {
setState(() {
_swipeDirection = "Swipe Left";
});
},
onSwipeRight: () {
setState(() {
_swipeDirection = "Swipe Right";
});
},
swipeConfiguration: SwipeConfiguration(
verticalSwipeMinVelocity: 100.0,
verticalSwipeMinDisplacement: 50.0,
verticalSwipeMaxWidthThreshold:100.0,
horizontalSwipeMaxHeightThreshold: 50.0,
horizontalSwipeMinDisplacement:50.0,
horizontalSwipeMinVelocity: 100.0),
),
)
],
),
),
),
);
}
}
Upvotes: 3