improve player gesture

Signed-off-by: dom <githubaccount56556@proton.me>
This commit is contained in:
dom
2026-05-09 12:25:59 +08:00
parent 0292517947
commit bbdcc1d3ce
2 changed files with 168 additions and 244 deletions

View File

@@ -110,7 +110,7 @@ class PLVideoPlayer extends StatefulWidget {
class _PLVideoPlayerState extends State<PLVideoPlayer>
with WidgetsBindingObserver, TickerProviderStateMixin {
late AnimationController animationController;
late AnimationController _animationController;
late VideoController videoController;
late final CommonIntroController introController = widget.introController!;
late final VideoDetailController videoDetailController =
@@ -126,15 +126,7 @@ class _PLVideoPlayerState extends State<PLVideoPlayer>
late final RxBool showRestoreScaleBtn = false.obs;
GestureType? _gestureType;
Offset initialFocalPoint = Offset.zero;
//播放器放缩
bool interacting = false;
// 阅读器限制
// Timer? _accessibilityDebounce;
// double _lastAnnouncedValue = -1;
Offset? _initialFocalPoint;
bool _pauseDueToPauseUponEnteringBackgroundMode = false;
@@ -161,9 +153,9 @@ class _PLVideoPlayerState extends State<PLVideoPlayer>
}
if (visible) {
animationController.forward();
_animationController.forward();
} else {
animationController.reverse();
_animationController.reverse();
}
if (widget.videoDetailController case final controller?) {
@@ -197,9 +189,9 @@ class _PLVideoPlayerState extends State<PLVideoPlayer>
_onControlChanged,
);
transformationController = TransformationController();
_transformationController = TransformationController();
animationController = AnimationController(
_animationController = AnimationController(
vsync: this,
duration: const Duration(milliseconds: 100),
);
@@ -250,6 +242,17 @@ class _PLVideoPlayerState extends State<PLVideoPlayer>
_tapGestureRecognizer = TapGestureRecognizer()..onTapUp = _onTapUp;
_doubleTapGestureRecognizer = DoubleTapGestureRecognizer()
..onDoubleTapDown = _onDoubleTapDown;
_scaleGestureRecognizer = ScaleGestureRecognizer(
debugOwner: this,
dragStartBehavior: .start,
allowedButtonsFilter: (buttons) => buttons == kPrimaryButton,
trackpadScrollToScaleFactor: const Offset(
0,
-1 / kDefaultMouseScrollToScaleFactor,
),
trackpadScrollCausesScale: false,
);
}
@override
@@ -289,17 +292,17 @@ class _PLVideoPlayerState extends State<PLVideoPlayer>
@override
void dispose() {
removeObserverMobile(this);
_danmakuListener?.cancel();
_tapGestureRecognizer.dispose();
_longPressRecognizer?.dispose();
_doubleTapGestureRecognizer.dispose();
_scaleGestureRecognizer.dispose();
_brightnessListener?.cancel();
_controlsListener?.cancel();
animationController.dispose();
_animationController.dispose();
_transformationController.dispose();
if (PlatformUtils.isMobile) {
FlutterVolumeController.removeListener();
}
transformationController.dispose();
super.dispose();
}
@@ -800,7 +803,7 @@ class _PLVideoPlayerState extends State<PLVideoPlayer>
bool get isFullScreen => plPlayerController.isFullScreen.value;
late final TransformationController transformationController;
late final TransformationController _transformationController;
late ColorScheme colorScheme;
late double maxWidth;
@@ -812,74 +815,55 @@ class _PLVideoPlayerState extends State<PLVideoPlayer>
colorScheme = ColorScheme.of(context);
}
void _onInteractionStart(ScaleStartDetails details) {
if (plPlayerController.controlsLock.value) return;
final localFocalPoint = details.localFocalPoint;
if (localFocalPoint.dx > maxWidth - 40 ||
localFocalPoint.dy > maxHeight - 40) {
return;
}
if (details.pointerCount > 1) {
interacting = true;
}
initialFocalPoint = localFocalPoint;
void _onPanStart(ScaleStartDetails details) {
_gestureType = null;
_initialFocalPoint = details.localFocalPoint;
}
void _onInteractionUpdate(ScaleUpdateDetails details) {
showRestoreScaleBtn.value =
transformationController.value.storage[0] != 1.0;
if (interacting || initialFocalPoint == Offset.zero) {
return;
}
Offset cumulativeDelta = details.localFocalPoint - initialFocalPoint;
if (details.pointerCount > 1 && cumulativeDelta.distanceSquared < 2.25) {
interacting = true;
_gestureType = null;
return;
}
/// 锁定时禁用
if (plPlayerController.controlsLock.value) return;
void _onScaleUpdate(double scale) {
showRestoreScaleBtn.value = scale != 1.0;
}
void _onPanUpdate(ScaleUpdateDetails details) {
if (_gestureType == null) {
final cumulativeDelta = details.localFocalPoint - _initialFocalPoint!;
if (cumulativeDelta.distanceSquared < 1) return;
final dx = cumulativeDelta.dx.abs();
final dy = cumulativeDelta.dy.abs();
if (dx > 3 * dy) {
_gestureType = GestureType.horizontal;
} else if (dy > 3 * dx) {
// _gestureType = 'vertical';
if (dx > 3 * dy) {
_gestureType = .horizontal;
} else if (dy > 3 * dx) {
final double tapPosition = details.localFocalPoint.dx;
final double sectionWidth = maxWidth / 3;
if (tapPosition < sectionWidth) {
if (PlatformUtils.isDesktop) {
return;
}
// 左边区域
_gestureType = GestureType.left;
if (PlatformUtils.isDesktop) {
_gestureType = .right;
} else {
_gestureType = .left;
}
} else if (tapPosition < sectionWidth * 2) {
// 全屏
_gestureType = GestureType.center;
_gestureType = .center;
} else {
// 右边区域
_gestureType = GestureType.right;
_gestureType = .right;
}
} else {
return;
}
return;
}
Offset delta = details.focalPointDelta;
final delta = details.focalPointDelta;
if (_gestureType == GestureType.horizontal) {
if (_gestureType == .horizontal) {
// live模式下禁用
if (plPlayerController.isLive) return;
final int curSliderPosition =
final curSliderPosition =
plPlayerController.sliderPosition.inMilliseconds;
final int newPos =
final newPos =
(curSliderPosition +
(plPlayerController.sliderScale * delta.dx / maxWidth)
.round())
@@ -934,31 +918,25 @@ class _PLVideoPlayerState extends State<PLVideoPlayer>
plPlayerController.cancelSeek != true) {
plPlayerController.updatePreviewIndex(newPos ~/ 1000);
}
} else if (_gestureType == GestureType.left) {
} else if (_gestureType == .left) {
// 左边区域 👈
final double level = maxHeight * 3;
final double brightness = (_brightnessValue.value - delta.dy / level)
.clamp(0.0, 1.0);
setBrightness(brightness);
} else if (_gestureType == GestureType.center) {
} else if (_gestureType == .center) {
// 全屏
const double threshold = 2.5; // 滑动阈值
double cumulativeDy = details.localFocalPoint.dy - initialFocalPoint.dy;
double cumulativeDy = details.localFocalPoint.dy - _initialFocalPoint!.dy;
if (cumulativeDy > threshold) {
_gestureType = GestureType.center_down;
if (isFullScreen) {
plPlayerController.triggerFullScreen(status: false);
}
// if (kDebugMode) debugPrint('center_down:$cumulativeDy');
_gestureType = .center_down;
plPlayerController.triggerFullScreen(status: false);
} else if (cumulativeDy < -threshold) {
_gestureType = GestureType.center_up;
if (!isFullScreen) {
plPlayerController.triggerFullScreen(status: true);
}
// if (kDebugMode) debugPrint('center_up:$cumulativeDy');
_gestureType = .center_up;
plPlayerController.triggerFullScreen(status: true);
}
} else if (_gestureType == GestureType.right) {
} else if (_gestureType == .right) {
// 右边区域
final double level = maxHeight * 0.5;
EasyThrottle.throttle(
@@ -976,7 +954,7 @@ class _PLVideoPlayerState extends State<PLVideoPlayer>
}
}
void _onInteractionEnd(ScaleEndDetails details) {
void _onPanEnd(ScaleEndDetails details) {
if (Platform.isAndroid && _gestureType == .left) {
ScreenBrightnessPlatform.instance.restoreBrightnessMode();
}
@@ -994,15 +972,11 @@ class _PLVideoPlayerState extends State<PLVideoPlayer>
}
plPlayerController.onChangedSliderEnd();
}
interacting = false;
initialFocalPoint = Offset.zero;
_initialFocalPoint = null;
_gestureType = null;
}
void onDoubleTapDownMobile(TapDownDetails details) {
if (plPlayerController.isLive || plPlayerController.controlsLock.value) {
return;
}
final double tapPosition = details.localPosition.dx;
final double sectionWidth = maxWidth / 4;
DoubleTapType type;
@@ -1016,39 +990,21 @@ class _PLVideoPlayerState extends State<PLVideoPlayer>
plPlayerController.doubleTapFuc(type);
}
void onTapDesktop() {
if (plPlayerController.isLive || plPlayerController.controlsLock.value) {
return;
}
plPlayerController.onDoubleTapCenter();
}
void onDoubleTapDesktop() {
if (plPlayerController.controlsLock.value) {
return;
}
plPlayerController.triggerFullScreen(status: !isFullScreen);
}
void _onTapUp(TapUpDetails details) {
switch (details.kind) {
case ui.PointerDeviceKind.mouse when PlatformUtils.isDesktop:
onTapDesktop();
break;
plPlayerController.onDoubleTapCenter();
default:
plPlayerController.controls = !plPlayerController.showControls.value;
break;
}
}
void _onDoubleTapDown(TapDownDetails details) {
switch (details.kind) {
case ui.PointerDeviceKind.mouse when PlatformUtils.isDesktop:
onDoubleTapDesktop();
break;
plPlayerController.triggerFullScreen(status: !isFullScreen);
default:
onDoubleTapDownMobile(details);
break;
}
}
@@ -1062,7 +1018,18 @@ class _PLVideoPlayerState extends State<PLVideoPlayer>
plPlayerController.setLongPressStatus(false));
late final TapGestureRecognizer _tapGestureRecognizer;
late final DoubleTapGestureRecognizer _doubleTapGestureRecognizer;
StreamSubscription<bool>? _danmakuListener;
late final ScaleGestureRecognizer _scaleGestureRecognizer;
static const _kOffsetThreshold = 40.0;
bool _isPositionAllowed(Offset offset) {
if (offset.dx < _kOffsetThreshold ||
offset.dy < _kOffsetThreshold ||
offset.dx > maxWidth - _kOffsetThreshold ||
offset.dy > maxHeight - _kOffsetThreshold) {
return false;
}
return true;
}
void _onPointerDown(PointerDownEvent event) {
if (PlatformUtils.isDesktop) {
@@ -1075,20 +1042,37 @@ class _PLVideoPlayerState extends State<PLVideoPlayer>
..controlsLock.value = false
..showControls.value = false;
}
plPlayerController
.triggerFullScreen(
status: !isFullScreen,
inAppFullScreen: isSecondaryBtn,
)
.whenComplete(() => initialFocalPoint = Offset.zero);
plPlayerController.triggerFullScreen(
status: !isFullScreen,
inAppFullScreen: isSecondaryBtn,
);
return;
}
}
_tapGestureRecognizer.addPointer(event);
_doubleTapGestureRecognizer.addPointer(event);
if (!plPlayerController.isLive) {
longPressRecognizer.addPointer(event);
if (_isPositionAllowed(event.localPosition)) {
final controlsUnlock = !plPlayerController.controlsLock.value;
if (PlatformUtils.isMobile) {
_tapGestureRecognizer.addPointer(event);
if (controlsUnlock) {
if (!plPlayerController.isLive) {
_doubleTapGestureRecognizer.addPointer(event);
longPressRecognizer.addPointer(event);
}
_scaleGestureRecognizer.addPointer(event);
}
} else {
if (controlsUnlock) {
if (plPlayerController.isLive) {
_doubleTapGestureRecognizer.addPointer(event);
} else {
_tapGestureRecognizer.addPointer(event);
_doubleTapGestureRecognizer.addPointer(event);
longPressRecognizer.addPointer(event);
}
_scaleGestureRecognizer.addPointer(event);
}
}
}
}
@@ -1100,14 +1084,14 @@ class _PLVideoPlayerState extends State<PLVideoPlayer>
final dx = pan.dx.abs();
final dy = pan.dy.abs();
if (dx > 3 * dy) {
_gestureType = GestureType.horizontal;
_gestureType = .horizontal;
} else if (dy > 3 * dx) {
_gestureType = GestureType.right;
_gestureType = .right;
}
return;
}
if (_gestureType == GestureType.horizontal) {
if (_gestureType == .horizontal) {
if (plPlayerController.isLive) return;
Offset delta = event.localPanDelta;
@@ -1131,7 +1115,7 @@ class _PLVideoPlayerState extends State<PLVideoPlayer>
plPlayerController.cancelSeek != true) {
plPlayerController.updatePreviewIndex(newPos ~/ 1000);
}
} else if (_gestureType == GestureType.right) {
} else if (_gestureType == .right) {
final double level = maxHeight * 0.5;
EasyThrottle.throttle(
'setVolume',
@@ -1149,6 +1133,12 @@ class _PLVideoPlayerState extends State<PLVideoPlayer>
}
void _onPointerPanZoomEnd(PointerPanZoomEndEvent event) {
plPlayerController.showPreview.value = false;
if (plPlayerController.isSliderMoving.value) {
plPlayerController
..seekTo(plPlayerController.sliderPosition, isSeek: false)
..onChangedSliderEnd();
}
_gestureType = null;
}
@@ -1413,7 +1403,7 @@ class _PLVideoPlayerState extends State<PLVideoPlayer>
children: [
AppBarAni(
isTop: true,
controller: animationController,
controller: _animationController,
isFullScreen: isFullScreen,
child: plPlayerController.isDesktopPip
? GestureDetector(
@@ -1425,7 +1415,7 @@ class _PLVideoPlayerState extends State<PLVideoPlayer>
),
AppBarAni(
isTop: false,
controller: animationController,
controller: _animationController,
isFullScreen: isFullScreen,
child:
widget.bottomControl ??
@@ -1489,12 +1479,12 @@ class _PLVideoPlayerState extends State<PLVideoPlayer>
);
final anim = animController.drive(
Matrix4Tween(
begin: transformationController.value,
begin: _transformationController.value,
end: Matrix4.identity(),
).chain(CurveTween(curve: Curves.easeOut)),
);
void listener() {
transformationController.value = anim.value;
_transformationController.value = anim.value;
}
animController.addListener(listener);
@@ -1792,7 +1782,7 @@ class _PLVideoPlayerState extends State<PLVideoPlayer>
Widget get _videoWidget {
return Container(
clipBehavior: Clip.none,
clipBehavior: .none,
width: maxWidth,
height: maxHeight,
color: widget.fill,
@@ -1803,21 +1793,16 @@ class _PLVideoPlayerState extends State<PLVideoPlayer>
onPointerPanZoomUpdate: _onPointerPanZoomUpdate,
onPointerPanZoomEnd: _onPointerPanZoomEnd,
onPointerDown: _onPointerDown,
onInteractionStart: _onInteractionStart,
onInteractionUpdate: _onInteractionUpdate,
onInteractionEnd: _onInteractionEnd,
onPanStart: _onPanStart,
onPanUpdate: _onPanUpdate,
onPanEnd: _onPanEnd,
onScaleUpdate: _onScaleUpdate,
scaleGestureRecognizer: _scaleGestureRecognizer,
panEnabled: false,
minScale: 1.0,
maxScale: 2.0,
panAxis: PanAxis.aligned,
transformationController: transformationController,
onTranslate: () {
final storage = transformationController.value.storage;
showRestoreScaleBtn.value =
storage[12].abs() > 2.0 ||
storage[13].abs() > 2.0 ||
storage[0] != 1.0;
},
panAxis: .aligned,
transformationController: _transformationController,
childKey: _videoKey,
child: RepaintBoundary(
key: _videoKey,