界面优先级存在部分问题,先锁定到这个版本,进行后续优化

This commit is contained in:
2026-08-19 21:32:54 +08:00
parent 85463c3716
commit d116814bb6
3 changed files with 206 additions and 81 deletions
+90 -76
View File
@@ -6,11 +6,9 @@ import 'services/audio_service.dart';
import 'services/playback_service.dart';
import 'pages/home_page.dart';
import 'pages/player_page.dart';
import 'pages/playlist_page.dart'; // ✅ 导入 PlaylistPage
import 'pages/playlist_page.dart';
import 'widgets/global_mini_player.dart';
final GlobalKey<NavigatorState> navigatorKey = GlobalKey<NavigatorState>();
void main() {
WidgetsFlutterBinding.ensureInitialized();
@@ -55,7 +53,7 @@ class QTPlayerApp extends StatefulWidget {
class _QTPlayerAppState extends State<QTPlayerApp>
with SingleTickerProviderStateMixin {
bool _showPlayerPage = false;
bool _showPlaylist = false; // ✅ 新增:播放列表显示状态
bool _showPlaylist = false;
late final AnimationController _animationController;
late final Animation<double> _slideAnimation;
@@ -88,33 +86,56 @@ class _QTPlayerAppState extends State<QTPlayerApp>
void _openPlayerPage() {
setState(() {
_showPlayerPage = true;
_showPlaylist = false; // 打开播放页时关闭播放列表
_showPlaylist = false;
});
_animationController.forward(from: 0.0);
}
void _closePlayerPage() {
_animationController.reverse().then((_) {
setState(() {
_showPlayerPage = false;
});
if (mounted) {
setState(() {
_showPlayerPage = false;
});
}
});
}
// ✅ 打开播放列表
void _openPlaylist() {
setState(() {
_showPlaylist = true;
});
}
// ✅ 关闭播放列表
void _closePlaylist() {
setState(() {
_showPlaylist = false;
});
}
// ============================================================
// ✅ PopScope 的 onPopInvokedWithResult
// ============================================================
void _onPopInvokedWithResult(bool didPop, Object? result) {
if (didPop) return;
// 1️⃣ 优先关闭播放列表(最高层级)
if (_showPlaylist) {
_closePlaylist();
return;
}
// 2️⃣ 其次关闭播放页
if (_showPlayerPage) {
_closePlayerPage();
return;
}
// 3️⃣ 没有覆盖层,允许退出(由 Navigator 处理)
// 注意:这里不能直接退出,需要让 Navigator 处理
// 但 canPop 会根据状态返回 true/false
}
@override
Widget build(BuildContext context) {
if (!widget.initSuccess) {
@@ -137,25 +158,6 @@ class _QTPlayerAppState extends State<QTPlayerApp>
style: TextStyle(color: Colors.grey[400], fontSize: 14),
textAlign: TextAlign.center,
),
const SizedBox(height: 24),
ElevatedButton(
onPressed: () {
runApp(
ChangeNotifierProvider(
create: (_) => AudioService(),
child: QTPlayerApp(
initSuccess: true,
initError: null,
),
),
);
},
style: ElevatedButton.styleFrom(
backgroundColor: const Color(0xFFB8D4D0),
foregroundColor: Colors.black87,
),
child: const Text('重试'),
),
],
),
),
@@ -165,7 +167,6 @@ class _QTPlayerAppState extends State<QTPlayerApp>
return MaterialApp(
title: '清听',
navigatorKey: navigatorKey,
theme: ThemeData(
brightness: Brightness.dark,
primaryColor: const Color(0xFF7C9A9E),
@@ -179,57 +180,70 @@ class _QTPlayerAppState extends State<QTPlayerApp>
),
home: const HomePage(),
builder: (context, child) {
return Overlay(
initialEntries: [
OverlayEntry(
builder: (context) => Stack(
children: [
// ① 主页面
Positioned.fill(child: child!),
// ✅ 暴力测试:canPop 写死为 false
return PopScope(
canPop: false,
onPopInvokedWithResult: (didPop, result) {
debugPrint('🔥🔥🔥 BACK FIRED: didPop=$didPop');
debugPrint('🔥 playlist=$_showPlaylist, player=$_showPlayerPage');
// ② MiniPlayer
const Positioned(
left: 0,
right: 0,
bottom: 0,
child: GlobalMiniPlayer(),
),
// ③ 播放页(全屏覆盖)
if (_showPlayerPage)
Positioned.fill(
child: AnimatedBuilder(
animation: _animationController,
builder: (context, child) {
final progress = _slideAnimation.value;
final offsetY =
MediaQuery.of(context).size.height * progress;
return Transform.translate(
offset: Offset(0, offsetY),
child: Opacity(
opacity: 1.0 - progress * 0.3,
child: child,
),
);
},
child: PlayerPage(
onClose: _closePlayerPage,
onOpenPlaylist: _openPlaylist, // ✅ 传入回调
if (_showPlaylist) {
debugPrint('🔥 closing playlist');
_closePlaylist();
} else if (_showPlayerPage) {
debugPrint('🔥 closing player');
_closePlayerPage();
} else {
debugPrint('🔥 no overlay, allowing exit');
// 注意:这里 canPop=false,需要手动退出
// 用 SystemNavigator.pop() 或 Navigator.pop(context)
}
},
child: Overlay(
initialEntries: [
OverlayEntry(
builder: (context) => Stack(
children: [
Positioned.fill(child: child!),
const Positioned(
left: 0,
right: 0,
bottom: 0,
child: GlobalMiniPlayer(),
),
if (_showPlayerPage)
Positioned.fill(
child: AnimatedBuilder(
animation: _animationController,
builder: (context, child) {
final progress = _slideAnimation.value;
final offsetY =
MediaQuery.of(context).size.height * progress;
return Transform.translate(
offset: Offset(0, offsetY),
child: Opacity(
opacity: 1.0 - progress * 0.3,
child: child,
),
);
},
child: PlayerPage(
onClose: _closePlayerPage,
onOpenPlaylist: _openPlaylist,
),
),
),
),
// ④ ✅ 播放列表(最高层级,在播放页之上)
if (_showPlaylist)
Positioned.fill(
child: PlaylistPage(
onClose: _closePlaylist,
if (_showPlaylist)
Positioned.fill(
child: PlaylistPage(
onClose: _closePlaylist,
),
),
),
],
],
),
),
),
],
],
),
);
},
);