87 lines
2.8 KiB
Dart
87 lines
2.8 KiB
Dart
import 'package:flutter/material.dart';
|
||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||
|
||
import '../../core/theme/app_theme.dart';
|
||
import '../../providers/feed_provider.dart';
|
||
import '../../screens/category/category_screen.dart';
|
||
import '../../screens/feed/feed_screen.dart';
|
||
import '../../screens/notification/notifications_screen.dart';
|
||
import '../../screens/profile/mine_screen.dart';
|
||
import '../../screens/upload/upload_screen.dart';
|
||
|
||
/// 首页壳:底部 Tab(首页/分类/发布/消息/我的),02-技术文档信息架构
|
||
/// IndexedStack 保活各 Tab 状态(Feed 播放器池 / 分类滚动位置 / 上传流程不被销毁)
|
||
class HomeShell extends ConsumerStatefulWidget {
|
||
const HomeShell({super.key});
|
||
|
||
@override
|
||
ConsumerState<HomeShell> createState() => _HomeShellState();
|
||
}
|
||
|
||
class _HomeShellState extends ConsumerState<HomeShell> {
|
||
int _index = 0;
|
||
|
||
/// Feed 三种策略的 provider keys(切 Tab 时用于暂停/恢复播放器池)
|
||
static const _feedStrategies = FeedStrategy.values;
|
||
|
||
void _onTabSelected(int i) {
|
||
if (i == _index) return;
|
||
// 切到非首页:暂停所有 Feed 策略的播放器池(IndexedStack 保活,否则声音会继续)
|
||
if (i != 0) {
|
||
for (final s in _feedStrategies) {
|
||
ref.read(feedProvider(s).notifier).pool.pauseCurrent();
|
||
}
|
||
}
|
||
setState(() => _index = i);
|
||
// 切回首页:恢复当前策略播放(FeedScreen 内部 _strategy 是 recommend)
|
||
if (i == 0) {
|
||
ref.read(feedProvider(FeedStrategy.recommend).notifier).pool.playCurrent();
|
||
}
|
||
}
|
||
|
||
@override
|
||
Widget build(BuildContext context) {
|
||
return Scaffold(
|
||
body: IndexedStack(
|
||
index: _index,
|
||
children: const [
|
||
FeedScreen(),
|
||
CategoryScreen(),
|
||
UploadScreen(),
|
||
NotificationsScreen(),
|
||
MineScreen(),
|
||
],
|
||
),
|
||
bottomNavigationBar: NavigationBar(
|
||
backgroundColor: AppColors.surface,
|
||
indicatorColor: AppColors.primary.withValues(alpha: 0.2),
|
||
selectedIndex: _index,
|
||
onDestinationSelected: _onTabSelected,
|
||
destinations: const [
|
||
NavigationDestination(
|
||
icon: Icon(Icons.home_outlined),
|
||
selectedIcon: Icon(Icons.home, color: AppColors.primary),
|
||
label: '首页',
|
||
),
|
||
NavigationDestination(
|
||
icon: Icon(Icons.grid_view_outlined),
|
||
label: '分类',
|
||
),
|
||
NavigationDestination(
|
||
icon: Icon(Icons.add_box_outlined),
|
||
label: '发布',
|
||
),
|
||
NavigationDestination(
|
||
icon: Icon(Icons.notifications_none),
|
||
label: '消息',
|
||
),
|
||
NavigationDestination(
|
||
icon: Icon(Icons.person_outline),
|
||
label: '我的',
|
||
),
|
||
],
|
||
),
|
||
);
|
||
}
|
||
}
|