Files
blog_app/lib/pages/home_page.dart
2025-11-14 17:29:54 +08:00

328 lines
8.7 KiB
Dart

import 'package:blog_app/pages/about_page.dart';
import 'package:blog_app/pages/message_page.dart';
import 'package:blog_app/pages/profile_page.dart';
import 'package:blog_app/pages/settings_page.dart';
import 'package:flutter/material.dart';
class HomePage extends StatefulWidget {
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
int _currentPageIndex = 0;
late PageController _pageController;
// 页面标题列表
final List<String> _pageTitles = [
'个人资料',
'消息中心',
'设置',
'关于我们',
];
// 页面图标列表
final List<IconData> _pageIcons = [
Icons.home,
Icons.person,
Icons.message,
Icons.settings,
Icons.info,
];
@override
void initState() {
super.initState();
_pageController = PageController(initialPage: _currentPageIndex);
}
@override
void dispose() {
_pageController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(_pageTitles[_currentPageIndex]),
backgroundColor: Colors.blue,
elevation: 0,
leading: Builder(
builder: (context) => IconButton(
icon: Icon(Icons.menu, color: Colors.white),
onPressed: () => Scaffold.of(context).openDrawer(),
),
),
actions: [
IconButton(
icon: Icon(Icons.search, color: Colors.white),
onPressed: () {},
),
],
),
drawer: _buildDrawer(),
body: PageView(
controller: _pageController,
onPageChanged: (index) {
setState(() {
_currentPageIndex = index;
});
},
children: [
ProfilePage(),
MessagePage(),
SettingsPage(),
AboutPage(),
],
),
);
}
Widget _buildDrawer() {
return Drawer(
child: Container(
color: Colors.white,
child: Column(
children: [
// 抽屉头部
_buildDrawerHeader(),
// 菜单项列表
Expanded(
child: ListView(
padding: EdgeInsets.zero,
children: [
...List.generate(_pageTitles.length, (index) =>
_buildDrawerItem(
icon: _pageIcons[index],
title: _pageTitles[index],
index: index,
)
),
],
),
),
],
),
),
);
}
Widget _buildDrawerHeader() {
return Container(
width: double.infinity,
height: 200,
decoration: BoxDecoration(
gradient: LinearGradient(
begin: Alignment.topLeft,
end: Alignment.bottomRight,
colors: [Colors.blue, Colors.lightBlue],
),
),
child: SafeArea(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
CircleAvatar(
radius: 40,
backgroundColor: Colors.white.withOpacity(0.3),
child: Icon(
Icons.person,
size: 50,
color: Colors.white,
),
),
SizedBox(height: 16),
Text(
'用户名',
style: TextStyle(
color: Colors.white,
fontSize: 20,
fontWeight: FontWeight.bold,
),
),
SizedBox(height: 4),
Text(
'user@example.com',
style: TextStyle(
color: Colors.white70,
fontSize: 14,
),
),
],
),
),
);
}
Widget _buildDrawerItem({
required IconData icon,
required String title,
required int index,
}) {
final bool isSelected = index == _currentPageIndex;
final bool isSpecialItem = index == -1; // 特殊菜单项标识
return Container(
margin: EdgeInsets.symmetric(horizontal: 12, vertical: 4),
decoration: BoxDecoration(
color: isSelected ? Colors.blue.withOpacity(0.1) : Colors.transparent,
borderRadius: BorderRadius.circular(12),
),
child: ListTile(
leading: Icon(
icon,
color: isSelected ? Colors.blue :
isSpecialItem ? Colors.grey[600] : Colors.grey[700],
size: 24,
),
title: Text(
title,
style: TextStyle(
color: isSelected ? Colors.blue :
isSpecialItem ? Colors.grey[600] : Colors.grey[800],
fontWeight: isSelected ? FontWeight.bold : FontWeight.normal,
fontSize: 16,
),
),
trailing: isSelected ? Icon(
Icons.arrow_forward_ios,
size: 16,
color: Colors.blue,
) : null,
onTap: () {
if (!isSpecialItem) {
// 正常页面切换
setState(() {
_currentPageIndex = index;
});
_pageController.animateToPage(
index,
duration: Duration(milliseconds: 300),
curve: Curves.easeInOut,
);
} else {
// 特殊菜单项处理
_handleSpecialItemTap(title);
}
Navigator.pop(context); // 关闭抽屉
},
),
);
}
void _handleSpecialItemTap(String title) {
// 处理特殊菜单项的点击事件
switch (title) {
case '帮助中心':
print('打开帮助中心');
break;
case '意见反馈':
print('打开意见反馈');
break;
case '退出登录':
print('执行退出登录');
break;
}
}
}
// 各个页面组件
class HomeContentPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return SingleChildScrollView(
padding: EdgeInsets.all(16),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
// 欢迎卡片
Card(
elevation: 4,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(16),
),
child: Padding(
padding: EdgeInsets.all(20),
child: Row(
children: [
Icon(Icons.waving_hand, size: 40, color: Colors.amber),
SizedBox(width: 16),
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
'欢迎回来!',
style: TextStyle(
fontSize: 20,
fontWeight: FontWeight.bold,
),
),
SizedBox(height: 4),
Text(
'今天也是美好的一天',
style: TextStyle(color: Colors.grey),
),
],
),
),
],
),
),
),
SizedBox(height: 20),
// 功能网格
GridView.count(
shrinkWrap: true,
physics: NeverScrollableScrollPhysics(),
crossAxisCount: 2,
crossAxisSpacing: 16,
mainAxisSpacing: 16,
children: [
_buildFeatureCard('数据统计', Icons.analytics, Colors.blue),
_buildFeatureCard('文件管理', Icons.folder, Colors.green),
_buildFeatureCard('消息通知', Icons.notifications, Colors.orange),
_buildFeatureCard('系统设置', Icons.settings, Colors.purple),
],
),
],
),
);
}
Widget _buildFeatureCard(String title, IconData icon, Color color) {
return Card(
elevation: 2,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(12),
),
child: InkWell(
borderRadius: BorderRadius.circular(12),
onTap: () {},
child: Container(
padding: EdgeInsets.all(16),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Icon(icon, size: 40, color: color),
SizedBox(height: 8),
Text(
title,
style: TextStyle(
fontWeight: FontWeight.bold,
fontSize: 14,
),
textAlign: TextAlign.center,
),
],
),
),
),
);
}
}