fix: 重回前台弹幕堆积

This commit is contained in:
guozhigq
2024-04-28 23:03:33 +08:00
parent bc4fb0e0ae
commit 811487bdd4

View File

@ -38,7 +38,7 @@ class VideoDetailPage extends StatefulWidget {
}
class _VideoDetailPageState extends State<VideoDetailPage>
with TickerProviderStateMixin, RouteAware {
with TickerProviderStateMixin, RouteAware, WidgetsBindingObserver {
late VideoDetailController vdCtr;
PlPlayerController? plPlayerController;
final ScrollController _extendNestCtr = ScrollController();
@ -61,6 +61,8 @@ class _VideoDetailPageState extends State<VideoDetailPage>
late bool autoPiP;
late Floating floating;
bool isShowing = true;
// 生命周期监听
late final AppLifecycleListener _lifecycleListener;
@override
void initState() {
@ -96,6 +98,8 @@ class _VideoDetailPageState extends State<VideoDetailPage>
floating = vdCtr.floating!;
autoEnterPip();
}
WidgetsBinding.instance.addObserver(this);
lifecycleListener();
}
// 获取视频资源,初始化播放器
@ -219,6 +223,8 @@ class _VideoDetailPageState extends State<VideoDetailPage>
floating.dispose();
}
appbarStream.close();
WidgetsBinding.instance.removeObserver(this);
_lifecycleListener.dispose();
super.dispose();
}
@ -281,6 +287,29 @@ class _VideoDetailPageState extends State<VideoDetailPage>
}
}
// 生命周期监听
void lifecycleListener() {
_lifecycleListener = AppLifecycleListener(
// onResume: () => _handleTransition('resume'),
// 后台
// onInactive: () => _handleTransition('inactive'),
// 在Android和iOS端不生效
// onHide: () => _handleTransition('hide'),
onShow: () => _handleTransition('show'),
onPause: () => _handleTransition('pause'),
onRestart: () => _handleTransition('restart'),
onDetach: () => _handleTransition('detach'),
);
}
void _handleTransition(String name) {
switch (name) {
case 'show' || 'restart':
plPlayerController?.danmakuController?.clear();
break;
}
}
@override
Widget build(BuildContext context) {
final sizeContext = MediaQuery.sizeOf(context);