calendar_main_panel.dart 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. import 'package:calendar_view/calendar_controller/controller.dart';
  2. import 'package:calendar_view/calendar_view/calendar_views/calendar_month_view.dart';
  3. import 'package:calendar_view/calendar_view/mini_calendar/mini_calendar.dart';
  4. import 'package:calendar_view/calendar_view/my_calendar/my_calendar.dart';
  5. import 'package:flutter/material.dart';
  6. import 'package:get/get.dart';
  7. /// 日历视图主体面板
  8. class CalendarMainPanel extends StatefulWidget {
  9. const CalendarMainPanel({super.key});
  10. @override
  11. State<CalendarMainPanel> createState() => _CalendarMainPanelState();
  12. }
  13. enum CalendarViewType { month, week, day, list }
  14. class _CalendarMainPanelState extends State<CalendarMainPanel> {
  15. /// TODO:[Gavin] 将 _viewType 也收入到 CalendarController 中
  16. final CalendarViewType _viewType = CalendarViewType.month;
  17. String _currYearMonthStr = ''; // 当前年月
  18. bool _isToday = true; // 是否是今天
  19. CalendarController calendarController = Get.find<CalendarController>();
  20. @override
  21. void initState() {
  22. super.initState();
  23. _isToday = calendarController.isToday;
  24. _currYearMonthStr = calendarController.currentYearMonth;
  25. calendarController.onDaysListChange.addListener(_onDaysListChange);
  26. }
  27. @override
  28. void dispose() {
  29. super.dispose();
  30. calendarController.onDaysListChange.removeListener(_onDaysListChange);
  31. }
  32. void _onDaysListChange(_, e) {
  33. setState(() {
  34. _isToday = calendarController.isToday;
  35. _currYearMonthStr = calendarController.currentYearMonth;
  36. });
  37. }
  38. /// 按钮样式
  39. final iconButtonStyle = ButtonStyle(
  40. padding:
  41. MaterialStateProperty.all<EdgeInsetsGeometry>(const EdgeInsets.all(0)),
  42. backgroundColor: MaterialStateProperty.all<Color>(Colors.transparent),
  43. shadowColor: MaterialStateProperty.all<Color>(Colors.transparent),
  44. overlayColor: MaterialStateProperty.all<Color>(
  45. const Color.fromARGB(255, 224, 226, 228)),
  46. foregroundColor: MaterialStateProperty.all<Color>(Colors.white),
  47. surfaceTintColor: MaterialStateProperty.all<Color>(Colors.white),
  48. );
  49. @override
  50. Widget build(BuildContext context) {
  51. return Container(
  52. decoration: const BoxDecoration(
  53. border: Border(
  54. right: BorderSide(
  55. color: Colors.black12,
  56. width: 1,
  57. ),
  58. ),
  59. // color: const Color.fromARGB(90, 59, 255, 124),
  60. ),
  61. child: Column(
  62. children: <Widget>[
  63. _buildMainPanelTop(),
  64. Expanded(
  65. child: _buildMainView(_viewType),
  66. ),
  67. ],
  68. ),
  69. );
  70. }
  71. /// 构建顶部【今天、年月、视图切换器、更多】
  72. Widget _buildMainPanelTop() {
  73. return Container(
  74. height: 70,
  75. child: Container(
  76. padding: const EdgeInsets.symmetric(horizontal: 10),
  77. child: Row(
  78. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  79. children: <Widget>[
  80. _buildTodayButton(),
  81. _buildChangeMonthButtons(),
  82. _buildYearAndMonth(),
  83. Expanded(
  84. child: Container(),
  85. ),
  86. _buildMoreButton(),
  87. ],
  88. ),
  89. ),
  90. );
  91. }
  92. /// 【回到今天】按钮
  93. Widget _buildTodayButton() {
  94. return SizedBox(
  95. width: 50,
  96. height: 32,
  97. child: ElevatedButton(
  98. style: ButtonStyle(
  99. padding: MaterialStateProperty.all<EdgeInsetsGeometry>(
  100. const EdgeInsets.all(0)),
  101. shadowColor: MaterialStateProperty.all<Color>(Colors.transparent),
  102. ),
  103. onPressed: _isToday ? null : calendarController.selectToday,
  104. // TODO[Gavin]: i18n
  105. child: const Text('今天'),
  106. ),
  107. );
  108. }
  109. /// 【上个月、下个月】按钮
  110. Widget _buildChangeMonthButtons() {
  111. return Container(
  112. margin: const EdgeInsets.symmetric(horizontal: 5),
  113. height: 40,
  114. child: Row(
  115. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  116. children: <Widget>[
  117. SizedBox(
  118. width: 32,
  119. height: 32,
  120. child: ElevatedButton(
  121. style: iconButtonStyle,
  122. onPressed: calendarController.handleNextMonth,
  123. child: const Icon(
  124. Icons.keyboard_arrow_left,
  125. size: 20,
  126. color: Colors.black,
  127. ),
  128. ),
  129. ),
  130. SizedBox(
  131. width: 32,
  132. height: 32,
  133. child: ElevatedButton(
  134. style: iconButtonStyle,
  135. onPressed: calendarController.handleLastMonth,
  136. child: const Icon(
  137. Icons.keyboard_arrow_right,
  138. size: 20,
  139. color: Colors.black,
  140. ),
  141. ),
  142. ),
  143. ],
  144. ),
  145. );
  146. }
  147. /// 年月
  148. Widget _buildYearAndMonth() {
  149. return Align(
  150. alignment: Alignment.center,
  151. child: Text(
  152. _currYearMonthStr,
  153. style: const TextStyle(
  154. height: 1.2,
  155. fontSize: 18,
  156. ),
  157. ),
  158. );
  159. }
  160. /// 【更多】按钮
  161. Widget _buildMoreButton() {
  162. return SizedBox(
  163. width: 50,
  164. height: 32,
  165. child: ElevatedButton(
  166. style: iconButtonStyle,
  167. onPressed: () {
  168. ScaffoldMessenger.of(context).showSnackBar(
  169. const SnackBar(
  170. duration: Duration(milliseconds: 500),
  171. content: Text('前方正在施工'),
  172. ),
  173. );
  174. },
  175. // TODO[Gavin]: i18n
  176. child: const Center(
  177. child: Icon(
  178. Icons.more_horiz,
  179. size: 20,
  180. color: Color.fromARGB(255, 116, 118, 119),
  181. ),
  182. ),
  183. ),
  184. );
  185. }
  186. /// 构建视图主体
  187. Widget _buildMainView(CalendarViewType type) {
  188. switch (type) {
  189. case CalendarViewType.month:
  190. return const CalendarMonthView();
  191. // TODO 周视图
  192. case CalendarViewType.week:
  193. return const MyCalendar();
  194. // TODO 日视图
  195. case CalendarViewType.day:
  196. return const MyCalendar();
  197. // TODO 列表视图
  198. case CalendarViewType.list:
  199. return const MyCalendar();
  200. default:
  201. return const CalendarMonthView();
  202. }
  203. }
  204. }