Вы можете ссылаться на пакет https://pub.dev/packages/swipedetector или использовать его напрямую.
он использует onHorizontalDrag для обнаружения и разрешения скорости и смещения
фрагмент кода 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;
полный код детектора салфетки
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();
}
}
},
);
}
}
полный пример кода
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),
),
)
],
),
),
),
);
}
}
person
chunhunghan
schedule
30.09.2019