import 'package:flutter/material.dart'; import 'package:intl/date_symbol_data_local.dart'; import 'package:widgets/shatter/weekly_calendar/week_page.dart'; import 'calendar_utils.dart' show getWeekdays; import 'day_of_week_view.dart'; /// 总入口,整合顶部的Week控件与底部的Days的PageView控件 class WeeklyCalendar extends StatefulWidget { const WeeklyCalendar({ super.key, this.isAutoSelect = false, this.onChangedSelectedDate, this.onChangedPage, this.selectedDate, }); final DateTime? selectedDate; final bool isAutoSelect; final Function(DateTime)? onChangedSelectedDate; final Function(DateTime date, PageState state)? onChangedPage; @override State createState() => _WeeklyCalendarState(); } class _WeeklyCalendarState extends State { final DateTime now = DateTime.now(); late DateTime selectedDate; DateTime currentPageDate = DateTime.now(); @override void initState() { initializeDateFormatting("en"); //日期国际化指定 super.initState(); selectedDate = widget.selectedDate ?? DateTime.now(); } @override Widget build(BuildContext context) { return Column( mainAxisSize: MainAxisSize.min, children: [ //顶部是固定的星期布局 customDayOfWeek(), const SizedBox(height: 12), //底部是每个星期的Days的PageView布局 WeekPage( selectedDate: selectedDate, //默认选中的日期,如果没有填写默认是今天 now: now, //今天的日期 isAutoSelect: widget.isAutoSelect, //翻页是否自动选中 onChangedPage: (date, state) { setState(() { currentPageDate = date; }); widget.onChangedPage?.call(date, state); }, onChangedSelectedDate: (date) { setState(() { selectedDate = date; }); widget.onChangedSelectedDate?.call(date); }, ), ], ); } //星期的文本显示 Widget customDayOfWeek() { final weekdays = getWeekdays(now, 0); return DayOfWeekView(weekdays: weekdays); } }