100 lines
3.1 KiB
Dart
100 lines
3.1 KiB
Dart
import 'package:flutter/material.dart';
|
||
import '../constants/app_constants.dart';
|
||
import './home/home_page.dart';
|
||
import './discover_page.dart';
|
||
import './favorites_page.dart';
|
||
import './profile/profile_page.dart';
|
||
|
||
/// 时间: 2025-03-21
|
||
/// 功能: 主导航页面,包含底部导航栏和4个主要页面
|
||
/// 介绍: 这是应用的主要导航容器,提供底部导航栏来切换主页、发现、收藏和个人页面
|
||
/// 最新变化: 新创建文件,实现了底部导航栏功能,添加黑屏检查
|
||
|
||
class MainNavigation extends StatefulWidget {
|
||
const MainNavigation({super.key});
|
||
|
||
@override
|
||
State<MainNavigation> createState() => _MainNavigationState();
|
||
}
|
||
|
||
class _MainNavigationState extends State<MainNavigation> {
|
||
int _currentIndex = 0;
|
||
final GlobalKey<State<ProfilePage>> _profileKey =
|
||
GlobalKey<State<ProfilePage>>();
|
||
|
||
late final List<Widget> _pages = [
|
||
const HomePage(),
|
||
const DiscoverPage(),
|
||
const FavoritesPage(),
|
||
ProfilePage(key: _profileKey),
|
||
];
|
||
|
||
final List<BottomNavigationBarItem> _bottomNavItems = [
|
||
const BottomNavigationBarItem(
|
||
icon: Icon(Icons.home),
|
||
activeIcon: Icon(Icons.home, color: AppConstants.primaryColor),
|
||
label: '主页',
|
||
),
|
||
const BottomNavigationBarItem(
|
||
icon: Icon(Icons.explore),
|
||
activeIcon: Icon(Icons.explore, color: AppConstants.primaryColor),
|
||
label: '发现',
|
||
),
|
||
const BottomNavigationBarItem(
|
||
icon: Icon(Icons.favorite_border),
|
||
activeIcon: Icon(Icons.favorite, color: AppConstants.primaryColor),
|
||
label: '收藏',
|
||
),
|
||
const BottomNavigationBarItem(
|
||
icon: Icon(Icons.person_outline),
|
||
activeIcon: Icon(Icons.person, color: AppConstants.primaryColor),
|
||
label: '个人',
|
||
),
|
||
];
|
||
|
||
@override
|
||
Widget build(BuildContext context) {
|
||
return Scaffold(
|
||
body: IndexedStack(index: _currentIndex, children: _pages),
|
||
bottomNavigationBar: Container(
|
||
decoration: BoxDecoration(
|
||
color: Colors.white,
|
||
boxShadow: [
|
||
BoxShadow(
|
||
color: Colors.black.withValues(alpha: 0.1),
|
||
blurRadius: 8,
|
||
offset: const Offset(0, -2),
|
||
),
|
||
],
|
||
),
|
||
child: SafeArea(
|
||
top: false,
|
||
child: BottomNavigationBar(
|
||
currentIndex: _currentIndex,
|
||
onTap: (index) {
|
||
setState(() {
|
||
_currentIndex = index;
|
||
});
|
||
// 切换到个人页面时刷新数据
|
||
if (index == 3) {
|
||
final profileState = _profileKey.currentState;
|
||
if (profileState != null && profileState.mounted) {
|
||
(profileState as dynamic).refreshData();
|
||
}
|
||
}
|
||
},
|
||
type: BottomNavigationBarType.fixed,
|
||
selectedItemColor: AppConstants.primaryColor,
|
||
unselectedItemColor: Colors.grey[600],
|
||
backgroundColor: Colors.white,
|
||
elevation: 0,
|
||
items: _bottomNavItems,
|
||
selectedFontSize: 12,
|
||
unselectedFontSize: 12,
|
||
),
|
||
),
|
||
),
|
||
);
|
||
}
|
||
}
|