HomePage.dart 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. import 'package:flutter/material.dart';
  2. import 'package:flutter_habit/common/BaseArchitectural.dart';
  3. import 'package:flutter_habit/common/I18N.dart';
  4. import 'package:flutter_habit/common/provider/UserProvider.dart';
  5. import 'package:flutter_habit/view/context/ExerciseInfoContext.dart';
  6. import 'package:flutter_habit/view/context/LifeInfoContext.dart';
  7. import 'package:flutter_habit/view/context/BasicInfoContext.dart';
  8. import 'package:flutter_habit/view/context/StudyInfoContext.dart';
  9. import 'package:flutter_habit/view/drawer/HomePageDrawer.dart';
  10. import 'package:flutter_habit/view/record/BasicInfoRecordingPage.dart';
  11. import 'package:flutter_habit/view/record/ExerciseInfoRecordingPage.dart';
  12. import 'package:flutter_habit/view/record/LifeInfoRecordingPage.dart';
  13. import 'package:flutter_habit/view/record/StudyInfoRecordingPage.dart';
  14. import 'package:provider/provider.dart';
  15. class HomePage extends StatelessWidget {
  16. @override
  17. Widget build(BuildContext context) {
  18. return MultiProvider(
  19. providers: [
  20. ChangeNotifierProvider<HomePageService>(
  21. create: (_) => HomePageService(context)),
  22. ChangeNotifierProvider<HomePageModel>(
  23. create: (_) => HomePageModel(context)),
  24. ],
  25. child: _HomePageView(),
  26. );
  27. }
  28. }
  29. // model
  30. class HomePageModel extends BaseModel {
  31. HomePageModel(BuildContext context) : super(context);
  32. int currentIndex;
  33. PageController pageViewController;
  34. @override
  35. void init(BuildContext context) {
  36. super.init(context);
  37. currentIndex = 0;
  38. pageViewController = PageController(initialPage: 0);
  39. }
  40. }
  41. // service
  42. class HomePageService extends BaseService {
  43. HomePageService(BuildContext context) : super(context);
  44. void changeNavigation(BuildContext context, int index) {
  45. HomePageModel model = Provider.of<HomePageModel>(context, listen: false);
  46. model.currentIndex = index;
  47. model.pageViewController.animateToPage(index,
  48. duration: Duration(milliseconds: 300), curve: Curves.ease);
  49. model.refresh();
  50. }
  51. void onPageChanged(BuildContext context, int index) {
  52. HomePageModel model = Provider.of<HomePageModel>(context, listen: false);
  53. model.currentIndex = index;
  54. model.refresh();
  55. }
  56. Future<void> toBasicInfoRecordingPage(BuildContext context) async {
  57. await Navigator.of(context)
  58. .push(MaterialPageRoute(builder: (_) => BasicInfoRecordingPage()));
  59. }
  60. Future<void> toLifeInfoRecordingPage(BuildContext context) async {
  61. await Navigator.of(context)
  62. .push(MaterialPageRoute(builder: (_) => LifeInfoRecordingPage()));
  63. }
  64. Future<void> toExerciseInfoRecordingPage(BuildContext context) async {
  65. await Navigator.of(context)
  66. .push(MaterialPageRoute(builder: (_) => ExerciseInfoRecordingPage()));
  67. }
  68. Future<void> toStudyInfoRecordingPage(BuildContext context) async {
  69. await Navigator.of(context)
  70. .push(MaterialPageRoute(builder: (_) => StudyInfoRecordingPage()));
  71. }
  72. }
  73. // view
  74. class _HomePageView extends StatelessWidget {
  75. @override
  76. Widget build(BuildContext context) {
  77. HomePageService service =
  78. Provider.of<HomePageService>(context, listen: false);
  79. HomePageModel model = Provider.of<HomePageModel>(context, listen: true);
  80. UserProvider userProvider =
  81. Provider.of<UserProvider>(context, listen: true);
  82. return Scaffold(
  83. appBar: AppBar(
  84. title: Text(I18N.of(<String>[
  85. "基本信息",
  86. "日常生活",
  87. "体育锻炼",
  88. "课程学习",
  89. ][model.currentIndex])),
  90. actions: userProvider.token == null
  91. ? []
  92. : <Widget>[
  93. Row(
  94. children: <Widget>[
  95. Text(
  96. "${userProvider.coins} x ",
  97. style: TextStyle(fontSize: 16),
  98. ),
  99. Icon(Icons.monetization_on),
  100. Text(" "),
  101. ],
  102. ),
  103. ],
  104. ),
  105. drawer: HomePageDrawer(),
  106. body: PageView(
  107. controller: model.pageViewController,
  108. onPageChanged: (index) => service.onPageChanged(context, index),
  109. physics: NeverScrollableScrollPhysics(),
  110. children: <Widget>[
  111. BasicInfoContext(),
  112. LifeInfoContext(),
  113. ExerciseInfoContext(),
  114. StudyInfoContext(),
  115. ],
  116. ),
  117. floatingActionButton: <Widget>[
  118. FloatingActionButton(
  119. child: Icon(Icons.playlist_add),
  120. onPressed: () => service.toBasicInfoRecordingPage(context),
  121. ),
  122. FloatingActionButton(
  123. child: Icon(Icons.playlist_add),
  124. onPressed: () => service.toLifeInfoRecordingPage(context),
  125. ),
  126. FloatingActionButton(
  127. child: Icon(Icons.playlist_add),
  128. onPressed: () => service.toExerciseInfoRecordingPage(context),
  129. ),
  130. FloatingActionButton(
  131. child: Icon(Icons.playlist_add),
  132. onPressed: () => service.toStudyInfoRecordingPage(context),
  133. ),
  134. ][model.currentIndex],
  135. bottomNavigationBar: BottomNavigationBar(
  136. currentIndex: model.currentIndex,
  137. onTap: (index) => service.changeNavigation(context, index),
  138. showUnselectedLabels: true,
  139. fixedColor: Theme.of(context).colorScheme.secondary,
  140. unselectedItemColor: Theme.of(context).unselectedWidgetColor,
  141. items: <BottomNavigationBarItem>[
  142. BottomNavigationBarItem(
  143. icon: Icon(Icons.accessibility), label: I18N.of("基本信息")),
  144. BottomNavigationBarItem(
  145. icon: Icon(Icons.wb_sunny), label: I18N.of("日常生活")),
  146. BottomNavigationBarItem(
  147. icon: Icon(Icons.directions_bike), label: I18N.of("体育锻炼")),
  148. BottomNavigationBarItem(
  149. icon: Icon(Icons.school), label: I18N.of("课程学习")),
  150. ],
  151. ),
  152. );
  153. }
  154. }