69 lines
1.3 KiB
Dart
69 lines
1.3 KiB
Dart
// Flutter imports:
|
|
import 'package:flutter/gestures.dart';
|
|
import 'package:flutter/material.dart';
|
|
|
|
///
|
|
/// Направления свайпов
|
|
///
|
|
enum SwipeDirection {
|
|
///
|
|
/// Свайп вниз
|
|
///
|
|
DOWN,
|
|
|
|
///
|
|
/// Свайп вверх
|
|
///
|
|
UP,
|
|
}
|
|
|
|
class SwipeGesture extends StatelessWidget {
|
|
///
|
|
/// Отслеживание свайпа по виджету
|
|
///
|
|
const SwipeGesture({
|
|
required this.swipe,
|
|
required this.onSwipe,
|
|
required this.child,
|
|
super.key,
|
|
});
|
|
|
|
/// Направление движения
|
|
final SwipeDirection swipe;
|
|
|
|
/// Обработка свайпа
|
|
final VoidCallback onSwipe;
|
|
|
|
/// Потомок
|
|
final Widget child;
|
|
|
|
void _onVerticalDragUpdate(DragUpdateDetails e) {
|
|
const int sensitivity = 3;
|
|
|
|
switch (swipe) {
|
|
case SwipeDirection.DOWN:
|
|
if (e.delta.dy > sensitivity) {
|
|
onSwipe();
|
|
}
|
|
|
|
break;
|
|
case SwipeDirection.UP:
|
|
if (e.delta.dy < sensitivity) {
|
|
onSwipe();
|
|
}
|
|
|
|
break;
|
|
}
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return GestureDetector(
|
|
dragStartBehavior: DragStartBehavior.start,
|
|
behavior: HitTestBehavior.opaque,
|
|
onVerticalDragUpdate: _onVerticalDragUpdate,
|
|
child: child,
|
|
);
|
|
}
|
|
}
|