import 'package:blog_app/pages/about_page.dart'; import 'package:blog_app/pages/blog_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 { int _currentPageIndex = 0; late PageController _pageController; // 页面标题列表 final List _pageTitles = ['博客', '消息中心', '设置', '关于我们']; // 页面图标列表 final List _pageIcons = [ Icons.article, Icons.person, Icons.message, Icons.settings, Icons.info, ]; @override void initState() { super.initState(); _pageController = PageController(initialPage: _currentPageIndex); } @override void dispose() { _pageController.dispose(); super.dispose(); } Widget _buildPageView() { return PageView( controller: _pageController, onPageChanged: (index) { setState(() { _currentPageIndex = index; }); }, children: [BlogPage(), MessagePage(), SettingsPage(), AboutPage()], ); } @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text( _pageTitles[_currentPageIndex], style: TextStyle(color: Colors.white), ), backgroundColor: Theme.of(context).colorScheme.primary, 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: Padding(padding: EdgeInsets.all(10), child: _buildPageView()), ); } 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; } } }