opt slide

Signed-off-by: dom <githubaccount56556@proton.me>
This commit is contained in:
dom
2026-01-24 09:11:28 +08:00
parent 30ee413852
commit 310f497c30

View File

@@ -1,7 +1,7 @@
import 'dart:math' show max; import 'dart:math' show max;
import 'package:PiliPlus/utils/storage_pref.dart'; import 'package:PiliPlus/utils/storage_pref.dart';
import 'package:flutter/gestures.dart' show PositionedGestureDetails; import 'package:flutter/gestures.dart' show HorizontalDragGestureRecognizer;
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:get/get.dart'; import 'package:get/get.dart';
@@ -12,12 +12,12 @@ abstract class CommonSlidePage extends StatefulWidget {
} }
mixin CommonSlideMixin<T extends CommonSlidePage> on State<T>, TickerProvider { mixin CommonSlideMixin<T extends CommonSlidePage> on State<T>, TickerProvider {
Offset? downPos; double? _downDx;
bool? isSliding; late double _maxWidth;
late double maxWidth;
late bool _isRTL = false; late bool _isRTL = false;
late final bool enableSlide; late final bool enableSlide;
AnimationController? _animController; late final AnimationController _animController;
SlideDragGestureRecognizer? _slideDragGestureRecognizer;
static bool slideDismissReplyPage = Pref.slideDismissReplyPage; static bool slideDismissReplyPage = Pref.slideDismissReplyPage;
@@ -30,99 +30,99 @@ mixin CommonSlideMixin<T extends CommonSlidePage> on State<T>, TickerProvider {
vsync: this, vsync: this,
reverseDuration: const Duration(milliseconds: 500), reverseDuration: const Duration(milliseconds: 500),
); );
_slideDragGestureRecognizer =
SlideDragGestureRecognizer(
isDxAllowed: (double dx) {
const offset = 30;
final isLTR = dx <= offset;
final isRTL = dx >= _maxWidth - offset;
if (isLTR || isRTL) {
_isRTL = isRTL;
_downDx = dx;
return true;
}
return false;
},
)
..onUpdate = _onDragUpdate
..onEnd = _onDragEnd
..onCancel = _onDragEnd;
} }
} }
@override @override
void dispose() { void dispose() {
_animController?.dispose(); if (enableSlide) {
_animController.dispose();
_slideDragGestureRecognizer?.dispose();
_slideDragGestureRecognizer = null;
}
super.dispose(); super.dispose();
} }
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
final theme = Theme.of(context); final theme = Theme.of(context);
return enableSlide if (enableSlide) {
? LayoutBuilder( return LayoutBuilder(
builder: (context, constraints) { builder: (context, constraints) {
maxWidth = constraints.maxWidth; _maxWidth = constraints.maxWidth;
return AnimatedBuilder( return AnimatedBuilder(
animation: _animController!, animation: _animController,
builder: (context, child) { builder: (context, child) {
return Align( return Align(
alignment: AlignmentDirectional.topStart, alignment: AlignmentDirectional.topStart,
heightFactor: 1 - _animController!.value, heightFactor: 1 - _animController.value,
child: child, child: child,
);
},
child: buildPage(theme),
); );
}, },
) child: buildPage(theme),
: buildPage(theme); );
},
);
}
return buildPage(theme);
} }
Widget buildPage(ThemeData theme); Widget buildPage(ThemeData theme);
Widget buildList(ThemeData theme) => throw UnimplementedError(); Widget buildList(ThemeData theme) => throw UnimplementedError();
void onDismiss([_]) { void _onDragEnd([_]) {
if (isSliding == true) { final dx = _downDx!;
final dx = downPos!.dx; if (_animController.value * _maxWidth + (_isRTL ? (_maxWidth - dx) : dx) >=
if (_animController!.value * maxWidth + (_isRTL ? (maxWidth - dx) : dx) >= 100) {
100) { Get.back();
Get.back();
} else {
_animController!.reverse();
}
}
downPos = null;
isSliding = null;
}
void onPan(PositionedGestureDetails details) {
final localPosition = details.localPosition;
if (isSliding == false) {
return;
} else if (isSliding == null) {
if (downPos != null) {
Offset cumulativeDelta = localPosition - downPos!;
if (cumulativeDelta.dx.abs() >= cumulativeDelta.dy.abs()) {
downPos = localPosition;
isSliding = true;
} else {
isSliding = false;
}
} else {
isSliding = false;
}
} else if (isSliding == true) {
final from = downPos!.dx;
final to = details.localPosition.dx;
_animController!.value =
max(0, _isRTL ? from - to : to - from) / maxWidth;
}
}
void onPanDown(DragDownDetails details) {
final dx = details.localPosition.dx;
const offset = 30;
final isLTR = dx <= offset;
final isRTL = dx >= maxWidth - offset;
if (isLTR || isRTL) {
_isRTL = isRTL;
downPos = details.localPosition;
} else { } else {
isSliding = false; _animController.reverse();
} }
_downDx = null;
} }
Widget slideList(ThemeData theme) => GestureDetector( void _onDragUpdate(DragUpdateDetails details) {
onPanDown: onPanDown, final from = _downDx!;
onPanStart: onPan, final to = details.localPosition.dx;
onPanUpdate: onPan, _animController.value = max(0, _isRTL ? from - to : to - from) / _maxWidth;
onPanCancel: onDismiss, }
onPanEnd: onDismiss,
Widget slideList(ThemeData theme) => Listener(
onPointerDown: (event) => _slideDragGestureRecognizer?.addPointer(event),
child: buildList(theme), child: buildList(theme),
); );
} }
class SlideDragGestureRecognizer extends HorizontalDragGestureRecognizer {
SlideDragGestureRecognizer({
super.debugOwner,
super.supportedDevices,
super.allowedButtonsFilter,
required this.isDxAllowed,
});
final bool Function(double dx) isDxAllowed;
@override
bool isPointerAllowed(PointerEvent event) {
return isDxAllowed(event.localPosition.dx) && super.isPointerAllowed(event);
}
}