diff --git a/lib/calendar_provider.dart b/lib/calendar_provider.dart index b98daab..888ec43 100644 --- a/lib/calendar_provider.dart +++ b/lib/calendar_provider.dart @@ -110,7 +110,8 @@ class CalendarProvider extends ChangeNotifier { ? selectDateModel : DateModel.fromDateTime(DateTime.now()) ..day = 15; - if (calendarConfiguration.showMode == Constants.MODE_SHOW_ONLY_WEEK) { + if (calendarConfiguration.showMode == Constants.MODE_SHOW_ONLY_WEEK || + calendarConfiguration.showMode == Constants.MODE_SHOW_WEEK_AND_MONTH) { expandStatus = ValueNotifier(false); } else { expandStatus = ValueNotifier(true); diff --git a/lib/constants/constants.dart b/lib/constants/constants.dart index 4042090..c87b3ac 100644 --- a/lib/constants/constants.dart +++ b/lib/constants/constants.dart @@ -7,7 +7,8 @@ class Constants { //展示模式 static const int MODE_SHOW_ONLY_MONTH=1;//仅支持月视图 static const int MODE_SHOW_ONLY_WEEK=2;//仅支持星期视图 - static const int MODE_SHOW_WEEK_AND_MONTH=3;//支持月和星期视图切换 + static const int MODE_SHOW_WEEK_AND_MONTH=3;//支持两种视图,先显示周视图 + static const int MODE_SHOW_MONTH_AND_WEEK=4;//支持两种视图,先显示月视图 /** diff --git a/lib/controller.dart b/lib/controller.dart index 386aa86..27bec8c 100644 --- a/lib/controller.dart +++ b/lib/controller.dart @@ -93,8 +93,7 @@ class CalendarController { maxYearMonth, ))}"); - if (showMode == Constants.MODE_SHOW_ONLY_MONTH || - showMode == Constants.MODE_SHOW_WEEK_AND_MONTH) { + if (showMode != Constants.MODE_SHOW_ONLY_WEEK) { //初始化pageController,initialPage默认是当前时间对于的页面 int initialPage = 0; int nowMonthIndex = 0; @@ -132,8 +131,7 @@ class CalendarController { "初始化月份视图的信息:一共有${monthList.length}个月,initialPage为${nowMonthIndex}"); } - if (showMode == Constants.MODE_SHOW_ONLY_WEEK || - showMode == Constants.MODE_SHOW_WEEK_AND_MONTH) { + if (showMode != Constants.MODE_SHOW_ONLY_MONTH) { //计算一共多少周 //计算方法:第一天是周几,最后一天是周几,中间的天数/7后加上2就是结果了 int initialWeekPage = 0; @@ -230,7 +228,8 @@ class CalendarController { calendarProvider.calendarConfiguration.monthController .previousPage(duration: DEFAULT_DURATION, curve: Curves.ease); calendarProvider.calendarConfiguration.monthChange( - monthList[currentIndex-1].year, monthList[currentIndex-1].month); + monthList[currentIndex - 1].year, + monthList[currentIndex - 1].month); DateModel temp = new DateModel(); temp.year = monthList[currentIndex].year; temp.month = monthList[currentIndex].month; @@ -268,7 +267,8 @@ class CalendarController { calendarProvider.calendarConfiguration.monthController .nextPage(duration: DEFAULT_DURATION, curve: Curves.ease); calendarProvider.calendarConfiguration.monthChange( - monthList[currentIndex+1].year, monthList[currentIndex+1].month); + monthList[currentIndex + 1].year, + monthList[currentIndex + 1].month); DateModel temp = new DateModel(); temp.year = monthList[currentIndex].year; temp.month = monthList[currentIndex].month; diff --git a/lib/widget/calendar_view.dart b/lib/widget/calendar_view.dart index 4d22306..b403203 100644 --- a/lib/widget/calendar_view.dart +++ b/lib/widget/calendar_view.dart @@ -73,12 +73,10 @@ class CalendarContainerState extends State double itemHeight; double totalHeight; - bool expand = true; + bool expand; CalendarProvider calendarProvider; - var state = CrossFadeState.showFirst; - List widgets = []; int index = 0; @@ -95,20 +93,28 @@ class CalendarContainerState extends State Constants.MODE_SHOW_WEEK_AND_MONTH) { widgets.add(const MonthViewPager()); widgets.add(const WeekViewPager()); + index = 1; + } else if (calendarProvider.calendarConfiguration.showMode == + Constants.MODE_SHOW_MONTH_AND_WEEK) { + widgets.add(const MonthViewPager()); + widgets.add(const WeekViewPager()); + index = 0; } else { //默认是只显示月视图 widgets.add(const MonthViewPager()); } + expand = calendarProvider.expandStatus.value; //如果需要视图切换的话,才需要添加监听,不然不需要监听变化 if (calendarProvider.calendarConfiguration.showMode == - Constants.MODE_SHOW_WEEK_AND_MONTH) { + Constants.MODE_SHOW_WEEK_AND_MONTH || + calendarProvider.calendarConfiguration.showMode == + Constants.MODE_SHOW_MONTH_AND_WEEK) { calendarProvider.expandStatus.addListener(() { setState(() { + print( + "calendarProvider.expandStatus.value:${calendarProvider.expandStatus.value}"); expand = calendarProvider.expandStatus.value; - state = (state == CrossFadeState.showSecond - ? CrossFadeState.showFirst - : CrossFadeState.showSecond); if (expand) { index = 0; //周视图切换到月视图 @@ -122,6 +128,9 @@ class CalendarContainerState extends State } }); }); + }else{ + index=0; + } // widget.calendarController.addMonthChangeListener((year, month) { @@ -151,7 +160,6 @@ class CalendarContainerState extends State MediaQueryData mediaQueryData = MediaQueryData.fromWindow(WidgetsBinding.instance.window); - print("mediaQueryData.orientation:${mediaQueryData}"); //如果itemSize为空,默认是宽度/7。网页版的话是高度/7 itemHeight = calendarProvider.calendarConfiguration.itemSize != null ? calendarProvider.calendarConfiguration.itemSize diff --git a/lib/widget/month_view.dart b/lib/widget/month_view.dart index 6ee8fee..010dcf0 100644 --- a/lib/widget/month_view.dart +++ b/lib/widget/month_view.dart @@ -186,7 +186,7 @@ class ItemContainerState extends State { @override Widget build(BuildContext context) { - LogUtil.log(TAG: this.runtimeType, message: "ItemContainerState build"); +// LogUtil.log(TAG: this.runtimeType, message: "ItemContainerState build"); calendarProvider = Provider.of(context, listen: false); configuration = calendarProvider.calendarConfiguration; @@ -243,21 +243,21 @@ class ItemContainerState extends State { @override void deactivate() { - LogUtil.log( - TAG: this.runtimeType, message: "ItemContainerState deactivate"); +// LogUtil.log( +// TAG: this.runtimeType, message: "ItemContainerState deactivate"); super.deactivate(); } @override void dispose() { - LogUtil.log(TAG: this.runtimeType, message: "ItemContainerState dispose"); +// LogUtil.log(TAG: this.runtimeType, message: "ItemContainerState dispose"); super.dispose(); } @override void didUpdateWidget(ItemContainer oldWidget) { - LogUtil.log( - TAG: this.runtimeType, message: "ItemContainerState didUpdateWidget"); +// LogUtil.log( +// TAG: this.runtimeType, message: "ItemContainerState didUpdateWidget"); super.didUpdateWidget(oldWidget); } }