116 lines
2.8 KiB
Dart
116 lines
2.8 KiB
Dart
import 'package:blog_app/layout/drawer.dart';
|
|
import 'package:blog_app/layout/menu.dart';
|
|
import 'package:blog_app/pages/about_page.dart';
|
|
import 'package:blog_app/pages/blog_page.dart';
|
|
import 'package:blog_app/pages/category_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;
|
|
|
|
@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(), CategoryPage(), SettingsPage(), AboutPage()],
|
|
);
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
appBar: AppBar(
|
|
title: Text(
|
|
pages[_currentPageIndex].title,
|
|
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()),
|
|
);
|
|
}
|
|
|
|
void onTapDrawerItem(int index) {
|
|
setState(() {
|
|
_currentPageIndex = index;
|
|
});
|
|
_pageController.animateToPage(
|
|
index,
|
|
duration: Duration(milliseconds: 300),
|
|
curve: Curves.easeInOut,
|
|
);
|
|
Navigator.pop(context);
|
|
}
|
|
|
|
Widget _buildDrawerBody() {
|
|
return ListView(
|
|
padding: EdgeInsets.zero,
|
|
children: [
|
|
...List.generate(
|
|
pages.length,
|
|
(index) => buildDrawerItem(
|
|
context: context,
|
|
page: pages[index],
|
|
isSelected: index == _currentPageIndex,
|
|
onTap: () => onTapDrawerItem(index),
|
|
),
|
|
),
|
|
],
|
|
);
|
|
}
|
|
|
|
Widget _buildDrawer() {
|
|
return Drawer(
|
|
child: Container(
|
|
color: Colors.white,
|
|
child: Column(
|
|
children: [
|
|
// 抽屉头部
|
|
buildDrawerHeader(context),
|
|
// 菜单项列表
|
|
Expanded(child: _buildDrawerBody()),
|
|
],
|
|
),
|
|
),
|
|
);
|
|
}
|
|
}
|