Files
PiliPlus/lib/pages/common/slide/common_slide_page.dart
dom 310f497c30 opt slide
Signed-off-by: dom <githubaccount56556@proton.me>
2026-01-24 15:20:01 +08:00

129 lines
3.5 KiB
Dart

import 'dart:math' show max;
import 'package:PiliPlus/utils/storage_pref.dart';
import 'package:flutter/gestures.dart' show HorizontalDragGestureRecognizer;
import 'package:flutter/material.dart';
import 'package:get/get.dart';
abstract class CommonSlidePage extends StatefulWidget {
const CommonSlidePage({super.key, this.enableSlide = true});
final bool enableSlide;
}
mixin CommonSlideMixin<T extends CommonSlidePage> on State<T>, TickerProvider {
double? _downDx;
late double _maxWidth;
late bool _isRTL = false;
late final bool enableSlide;
late final AnimationController _animController;
SlideDragGestureRecognizer? _slideDragGestureRecognizer;
static bool slideDismissReplyPage = Pref.slideDismissReplyPage;
@override
void initState() {
super.initState();
enableSlide = widget.enableSlide && slideDismissReplyPage;
if (enableSlide) {
_animController = AnimationController(
vsync: this,
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
void dispose() {
if (enableSlide) {
_animController.dispose();
_slideDragGestureRecognizer?.dispose();
_slideDragGestureRecognizer = null;
}
super.dispose();
}
@override
Widget build(BuildContext context) {
final theme = Theme.of(context);
if (enableSlide) {
return LayoutBuilder(
builder: (context, constraints) {
_maxWidth = constraints.maxWidth;
return AnimatedBuilder(
animation: _animController,
builder: (context, child) {
return Align(
alignment: AlignmentDirectional.topStart,
heightFactor: 1 - _animController.value,
child: child,
);
},
child: buildPage(theme),
);
},
);
}
return buildPage(theme);
}
Widget buildPage(ThemeData theme);
Widget buildList(ThemeData theme) => throw UnimplementedError();
void _onDragEnd([_]) {
final dx = _downDx!;
if (_animController.value * _maxWidth + (_isRTL ? (_maxWidth - dx) : dx) >=
100) {
Get.back();
} else {
_animController.reverse();
}
_downDx = null;
}
void _onDragUpdate(DragUpdateDetails details) {
final from = _downDx!;
final to = details.localPosition.dx;
_animController.value = max(0, _isRTL ? from - to : to - from) / _maxWidth;
}
Widget slideList(ThemeData theme) => Listener(
onPointerDown: (event) => _slideDragGestureRecognizer?.addPointer(event),
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);
}
}