118 lines
2.8 KiB
Dart
118 lines
2.8 KiB
Dart
import 'package:fitnote/layout/app_navbar.dart';
|
|
import 'package:fitnote/models/menu.dart';
|
|
import 'package:fitnote/provider/health_provider.dart';
|
|
import 'package:fitnote/service/health_service.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:provider/provider.dart';
|
|
|
|
class HomePage extends StatefulWidget {
|
|
const HomePage({super.key});
|
|
|
|
@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 _buildLeading() {
|
|
return Builder(
|
|
builder:
|
|
(context) =>
|
|
IconButton(
|
|
icon: Icon(Icons.menu, color: Colors.white),
|
|
onPressed: () => Scaffold.of(context).openDrawer(),
|
|
),
|
|
);
|
|
}
|
|
|
|
Widget _buildAppBarTitle() {
|
|
return Text(
|
|
pages[_currentPageIndex].title,
|
|
style: TextStyle(color: Colors.white),
|
|
);
|
|
}
|
|
|
|
Widget _buildPageView() {
|
|
return PageView(
|
|
controller: _pageController,
|
|
onPageChanged: (index) {
|
|
setState(() {
|
|
if (index == 0) {
|
|
_refreshRecord();
|
|
}
|
|
_currentPageIndex = index;
|
|
});
|
|
},
|
|
children: pageWidgets,
|
|
);
|
|
}
|
|
|
|
void _refreshRecord() {
|
|
final HealthService service = HealthService();
|
|
final provider = context.read<HealthProvider>();
|
|
provider.updateSelectDate(DateTime.now());
|
|
provider.updateRecord(service.getRecordByDate(provider.selectedDate));
|
|
}
|
|
|
|
void _onTapNavbarItem(int index) {
|
|
setState(() {
|
|
_currentPageIndex = index;
|
|
});
|
|
_pageController.animateToPage(
|
|
index,
|
|
duration: Duration(milliseconds: 300),
|
|
curve: Curves.easeInOut,
|
|
);
|
|
}
|
|
|
|
void onTapDrawerItem(int index) {
|
|
setState(() {
|
|
_currentPageIndex = index;
|
|
});
|
|
_pageController.animateToPage(
|
|
index,
|
|
duration: Duration(milliseconds: 300),
|
|
curve: Curves.easeInOut,
|
|
);
|
|
Navigator.pop(context);
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
return Scaffold(
|
|
appBar: AppBar(
|
|
title: _buildAppBarTitle(),
|
|
backgroundColor: Theme
|
|
.of(context)
|
|
.colorScheme
|
|
.primary,
|
|
elevation: 0,
|
|
leading: _buildLeading(),
|
|
),
|
|
// drawer: AppDrawer(
|
|
// currentPageIndex: _currentPageIndex,
|
|
// onTapDrawerItem: onTapDrawerItem,
|
|
// ),
|
|
body: Padding(padding: EdgeInsets.all(10), child: _buildPageView()),
|
|
bottomNavigationBar: AppNavbar(
|
|
currentPageIndex: _currentPageIndex,
|
|
onTapNavbarItem: _onTapNavbarItem,
|
|
),
|
|
);
|
|
}
|
|
}
|