52 lines
1.9 KiB
Dart
52 lines
1.9 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:flutter/services.dart';
|
|
import 'package:get/get.dart';
|
|
import 'package:video_player/video_player.dart';
|
|
|
|
/// 全屏播放页业务逻辑:横竖屏切换 + 后台自动暂停
|
|
/// 注:[playCtr] 由外部页面持有并释放,本类只借用,绝不能 dispose
|
|
class VideoFullLogic extends GetxController with WidgetsBindingObserver {
|
|
VideoFullLogic({required this.playCtr, this.isAutoV = true});
|
|
|
|
final VideoPlayerController playCtr;
|
|
final bool isAutoV; // true:按视频比例决定是否转横屏;false:强制转横屏
|
|
|
|
double get aspectRatio => playCtr.value.aspectRatio;
|
|
|
|
@override
|
|
void onInit() {
|
|
super.onInit();
|
|
WidgetsBinding.instance.addObserver(this);
|
|
// 横向视频(或调用方强制)才转横屏,竖屏视频保持竖屏全屏
|
|
if (aspectRatio > 1 || !isAutoV) {
|
|
SystemChrome.setEnabledSystemUIMode(SystemUiMode.manual, overlays: [SystemUiOverlay.bottom]);
|
|
SystemChrome.setPreferredOrientations([
|
|
DeviceOrientation.landscapeLeft,
|
|
DeviceOrientation.landscapeRight,
|
|
]);
|
|
}
|
|
// 转屏后 Get.width/height 才是新值,下一帧刷一次让画面按新尺寸铺满
|
|
WidgetsBinding.instance.addPostFrameCallback((_) => update());
|
|
}
|
|
|
|
/// 切后台暂停,防止息屏/切走后声音还在外放
|
|
@override
|
|
void didChangeAppLifecycleState(AppLifecycleState state) {
|
|
super.didChangeAppLifecycleState(state);
|
|
if (state == AppLifecycleState.paused) playCtr.pause();
|
|
}
|
|
|
|
/// 退出全屏:恢复竖屏 + 状态栏,再退页
|
|
void exitFullScreen() {
|
|
SystemChrome.setEnabledSystemUIMode(SystemUiMode.manual, overlays: [SystemUiOverlay.bottom, SystemUiOverlay.top]);
|
|
SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp]);
|
|
Get.back();
|
|
}
|
|
|
|
@override
|
|
void onClose() {
|
|
WidgetsBinding.instance.removeObserver(this);
|
|
super.onClose(); // playCtr 属于调用方,这里不释放
|
|
}
|
|
}
|