weekly_calendar.dart 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. import 'package:flutter/material.dart';
  2. import 'package:intl/date_symbol_data_local.dart';
  3. import 'package:widgets/shatter/weekly_calendar/week_page.dart';
  4. import 'calendar_utils.dart' show getWeekdays;
  5. import 'day_of_week_view.dart';
  6. /// 总入口,整合顶部的Week控件与底部的Days的PageView控件
  7. class WeeklyCalendar extends StatefulWidget {
  8. const WeeklyCalendar({
  9. super.key,
  10. this.isAutoSelect = false,
  11. this.onChangedSelectedDate,
  12. this.onChangedPage,
  13. this.selectedDate,
  14. });
  15. final DateTime? selectedDate;
  16. final bool isAutoSelect;
  17. final Function(DateTime)? onChangedSelectedDate;
  18. final Function(DateTime date, PageState state)? onChangedPage;
  19. @override
  20. State<StatefulWidget> createState() => _WeeklyCalendarState();
  21. }
  22. class _WeeklyCalendarState extends State<WeeklyCalendar> {
  23. final DateTime now = DateTime.now();
  24. late DateTime selectedDate;
  25. DateTime currentPageDate = DateTime.now();
  26. @override
  27. void initState() {
  28. initializeDateFormatting("en"); //日期国际化指定
  29. super.initState();
  30. selectedDate = widget.selectedDate ?? DateTime.now();
  31. }
  32. @override
  33. Widget build(BuildContext context) {
  34. return Column(
  35. mainAxisSize: MainAxisSize.min,
  36. children: [
  37. //顶部是固定的星期布局
  38. customDayOfWeek(),
  39. const SizedBox(height: 12),
  40. //底部是每个星期的Days的PageView布局
  41. WeekPage(
  42. selectedDate: selectedDate, //默认选中的日期,如果没有填写默认是今天
  43. now: now, //今天的日期
  44. isAutoSelect: widget.isAutoSelect, //翻页是否自动选中
  45. onChangedPage: (date, state) {
  46. setState(() {
  47. currentPageDate = date;
  48. });
  49. widget.onChangedPage?.call(date, state);
  50. },
  51. onChangedSelectedDate: (date) {
  52. setState(() {
  53. selectedDate = date;
  54. });
  55. widget.onChangedSelectedDate?.call(date);
  56. },
  57. ),
  58. ],
  59. );
  60. }
  61. //星期的文本显示
  62. Widget customDayOfWeek() {
  63. final weekdays = getWeekdays(now, 0);
  64. return DayOfWeekView(weekdays: weekdays);
  65. }
  66. }