diff --git a/lib/calendar_provider.dart b/lib/calendar_provider.dart index 1455214..5150a87 100644 --- a/lib/calendar_provider.dart +++ b/lib/calendar_provider.dart @@ -1,6 +1,7 @@ import 'package:flutter/material.dart'; import 'package:flutter_custom_calendar/configuration.dart'; import 'package:flutter_custom_calendar/model/date_model.dart'; +import 'package:flutter_custom_calendar/utils/LogUtil.dart'; /** * 引入provider的状态管理,保存一些临时信息 @@ -16,7 +17,9 @@ class CalendarProvider extends ChangeNotifier { set selectDateModel(DateModel value) { _selectDateModel = value; - print("notifyListeners:$value"); + LogUtil.log( + TAG: this.runtimeType, + message: "selectDateModel change:${selectDateModel}"); notifyListeners(); } @@ -29,7 +32,7 @@ class CalendarProvider extends ChangeNotifier { {Set selectedDateList, DateModel selectDateModel, CalendarConfiguration calendarConfiguration}) { - print("CalendarProvider init"); + LogUtil.log(TAG: this.runtimeType, message: "CalendarProvider initData"); if (selectedDateList != null) { this.selectedDateList.addAll(selectedDateList); } @@ -43,7 +46,7 @@ class CalendarProvider extends ChangeNotifier { //退出的时候,清除数据 void clearData() { - print("CalendarProvider clearData"); + LogUtil.log(TAG: this.runtimeType, message: "CalendarProvider clearData"); selectedDateList.clear(); selectDateModel = null; } diff --git a/lib/configuration.dart b/lib/configuration.dart index 2b148d5..7298b0d 100644 --- a/lib/configuration.dart +++ b/lib/configuration.dart @@ -38,7 +38,17 @@ class CalendarConfiguration { Map extraDataMap = new Map(); //自定义额外的数据 /** - * UI绘制相关的配置 + * UI绘制方面的绘制 + */ + double verticalSpacing ;//日历item之间的竖直方向间距,默认10 + + + //支持自定义绘制 + DayWidgetBuilder dayWidgetBuilder; //创建日历item + WeekBarItemWidgetBuilder weekBarItemWidgetBuilder; //创建顶部的weekbar + + /** + * 监听变化 */ //各种事件回调 OnMonthChange monthChange; //月份切换事件 @@ -46,10 +56,6 @@ class CalendarConfiguration { OnMultiSelectOutOfRange multiSelectOutOfRange; //多选超出指定范围 OnMultiSelectOutOfSize multiSelectOutOfSize; //多选超出限制个数 - //支持自定义绘制 - DayWidgetBuilder dayWidgetBuilder; //创建日历item - WeekBarItemWidgetBuilder weekBarItemWidgetBuilder; //创建顶部的weekbar - /** * 下面的信息不是配置的,是根据配置信息进行计算出来的 */ @@ -79,6 +85,7 @@ class CalendarConfiguration { this.weekList, this.pageController, this.weekController, + this.verticalSpacing, bool defaultExpandStatus = true}) { this.defaultExpandStatus = defaultExpandStatus; } diff --git a/lib/controller.dart b/lib/controller.dart index 280d4b2..de4b83b 100644 --- a/lib/controller.dart +++ b/lib/controller.dart @@ -1,6 +1,9 @@ +import 'dart:developer'; + import 'package:flutter/material.dart'; import 'package:flutter_custom_calendar/calendar_provider.dart'; import 'package:flutter_custom_calendar/configuration.dart'; +import 'package:flutter_custom_calendar/utils/LogUtil.dart'; import 'package:flutter_custom_calendar/utils/date_util.dart'; import 'package:flutter_custom_calendar/widget/default_combine_day_view.dart'; import 'package:flutter_custom_calendar/widget/default_custom_day_view.dart'; @@ -50,9 +53,9 @@ class CalendarController { Set selectedDateTimeList = EMPTY_SET, DateModel selectDateModel, int maxMultiSelectCount = 9999, + double verticalSpacing = 10, Map extraDataMap = EMPTY_MAP}) { -// this.expandChanged = ValueNotifier(expandStatus); - + LogUtil.log(TAG: this.runtimeType, message: "init CalendarConfiguration"); calendarConfiguration = CalendarConfiguration( selectMode: selectMode, minYear: minYear, @@ -67,7 +70,8 @@ class CalendarController { maxSelectYear: maxSelectYear, maxSelectMonth: maxSelectMonth, defaultExpandStatus: expandStatus, - maxSelectDay: maxSelectDay); + maxSelectDay: maxSelectDay, + verticalSpacing: verticalSpacing); calendarConfiguration.dayWidgetBuilder = dayWidgetBuilder; calendarConfiguration.weekBarItemWidgetBuilder = weekBarItemWidgetBuilder; @@ -108,22 +112,33 @@ class CalendarController { } } this.monthController = new PageController(initialPage: initialPage); + LogUtil.log( + TAG: this.runtimeType, + message: "start:${DateModel.fromDateTime(DateTime( + minYear, + minYearMonth, + ))},end:${DateModel.fromDateTime(DateTime( + maxYear, + maxYearMonth, + ))}"); + LogUtil.log( + TAG: this.runtimeType, + message: + "初始化月份视图的信息:一共有${monthList.length}个月,initialPage为${nowMonthIndex}"); //计算一共多少周 //计算方法:第一天是周几,最后一天是周几,中间的天数/7后加上2就是结果了 - int initialWeekPage; + int initialWeekPage = 0; int nowWeekIndex = 0; weekList.clear(); - int sumDays = 0; //总天数 DateTime firstDayOfMonth = DateTime(minYear, minYearMonth, 1); //计算第一个星期的第一天的日期 DateTime firstWeekDate = firstDayOfMonth.add(Duration(days: -(firstDayOfMonth.weekday - 1))); - print("第一个星期的第一天的日期firstWeekDate:$firstWeekDate"); + DateTime lastDay = DateTime(maxYear, maxYearMonth, DateUtil.getMonthDaysCount(maxYear, maxYearMonth)); - print("最后一天:$lastDay"); for (DateTime dateTime = firstWeekDate; !dateTime.isAfter(lastDay); dateTime = dateTime.add(Duration(days: 7))) { @@ -131,7 +146,10 @@ class CalendarController { weekList.add(dateModel); } this.weekController = new PageController(); - print("weekList:$weekList"); + LogUtil.log( + TAG: this.runtimeType, + message: + "初始化星期视图的信息:一共有${weekList.length}个星期,initialPage为${initialWeekPage}"); calendarConfiguration.monthList = monthList; calendarConfiguration.weekList = weekList; @@ -164,7 +182,9 @@ class CalendarController { //切换展开状态 void toggleExpandStatus() { calendarProvider.expandStatus.value = !calendarProvider.expandStatus.value; - print("toggleExpandStatus:${calendarProvider.expandStatus.value}"); + LogUtil.log( + TAG: this.runtimeType, + message: "toggleExpandStatus:${calendarProvider.expandStatus.value}"); } //监听展开变化 @@ -234,20 +254,43 @@ class CalendarController { {bool needAnimation = false, Duration duration = const Duration(milliseconds: 500), Curve curve = Curves.ease}) { - DateModel dateModel = DateModel.fromDateTime(DateTime(year, month, 1)); - //计算目标索引 - int targetPage = monthList.indexOf(dateModel); - if (targetPage == -1) { - return; - } - if (monthController.hasClients == false) { - return; - } - if (needAnimation) { - monthController.animateToPage(targetPage, - duration: duration, curve: curve); + if (calendarProvider.expandStatus.value == true) { + DateModel dateModel = DateModel.fromDateTime(DateTime(year, month, 1)); + //计算目标索引 + int targetPage = monthList.indexOf(dateModel); + if (targetPage == -1) { + return; + } + if (monthController.hasClients == false) { + return; + } + if (needAnimation) { + monthController.animateToPage(targetPage, + duration: duration, curve: curve); + } else { + monthController.jumpToPage(targetPage); + } } else { - monthController.jumpToPage(targetPage); + DateModel dateModel = DateModel.fromDateTime(DateTime(year, month, 1)); + //计算目标索引 + int targetPage = 0; + for (int i = 0; i < weekList.length - 1; i++) { + DateModel first = weekList[i]; + DateModel next = weekList[i + 1]; + if (!first.isAfter(dateModel) && next.isAfter(dateModel)) { + targetPage = i; + return; + } + } + if (weekController.hasClients == false) { + return; + } + if (needAnimation) { + weekController.animateToPage(targetPage, + duration: duration, curve: curve); + } else { + weekController.jumpToPage(targetPage); + } } } @@ -293,7 +336,7 @@ class CalendarController { } if ((monthController.page.toInt() + 1) >= monthList.length) { - print("moveToNextMonth:当前是最后一个月份"); + LogUtil.log(TAG: this.runtimeType, message: "moveToNextMonth:当前是最后一个月份"); return; } DateTime targetDateTime = @@ -324,7 +367,8 @@ class CalendarController { } if ((monthController.page.toInt()) == 0) { - print("moveToPreviousMonth:当前是第一个月份"); + LogUtil.log( + TAG: this.runtimeType, message: "moveToPreviousMonth:当前是第一个月份"); return; } DateTime targetDateTime = diff --git a/lib/utils/LogUtil.dart b/lib/utils/LogUtil.dart new file mode 100644 index 0000000..73feb87 --- /dev/null +++ b/lib/utils/LogUtil.dart @@ -0,0 +1,23 @@ +import 'package:flutter/cupertino.dart'; + +/** + * 打印日志,外部可以控制日历信息的打印显示,方便调试查错 + */ +class LogUtil { + static bool _enableLog = true; //是否可以打印日志 + + static set enableLog(bool value) { + _enableLog = value; + } + + /** + * TAG:类名 + * message:一般就方法名+自定义信息吧 + */ + static void log( + {@required dynamic TAG , String message = ""}) { + if (_enableLog) { + print("flutter_custom_calendar------$TAG------>$message"); + } + } +} diff --git a/lib/utils/date_util.dart b/lib/utils/date_util.dart index f6bae9f..364f83c 100644 --- a/lib/utils/date_util.dart +++ b/lib/utils/date_util.dart @@ -1,6 +1,7 @@ import 'dart:math'; import 'package:flutter_custom_calendar/model/date_model.dart'; +import 'package:flutter_custom_calendar/utils/LogUtil.dart'; /** * 工具类 @@ -119,7 +120,10 @@ class DateUtil { //获取该月的天数 int monthDayCount = getMonthDaysCount(year, month); - print("$year年$month月,有$monthDayCount天,第一天的index为${mPreDiff}"); + LogUtil.log( + TAG: "DateUtil", + message: + "initCalendarForMonthView:$year年$month月,有$monthDayCount天,第一天的index为${mPreDiff}"); List result = new List(); @@ -179,7 +183,10 @@ class DateUtil { int preIndex = firstDayOfMonth.weekday - 1; // int lastIndex = lastDayOfMonth.weekday; - print("$year年$month月:有${((preIndex + monthDayCount) / 7).toInt() + 1}行"); + LogUtil.log( + TAG: "DateUtil", + message: + "getMonthViewLineCount:$year年$month月:有${((preIndex + monthDayCount) / 7).toInt() + 1}行"); return ((preIndex + monthDayCount) / 7).toInt() + 1; } @@ -191,7 +198,7 @@ class DateUtil { {DateModel minSelectDate, DateModel maxSelectDate, Map extraDataMap}) { - print("initCalendarForWeekView"); + LogUtil.log(TAG: "DateUtil", message: "initCalendarForWeekView"); List items = List(); int weekDay = currentDate.weekday; diff --git a/lib/utils/solar_term_util.dart b/lib/utils/solar_term_util.dart index 9320df0..4ddb53c 100644 --- a/lib/utils/solar_term_util.dart +++ b/lib/utils/solar_term_util.dart @@ -2143,8 +2143,6 @@ class SolarTermUtil { List solarTerms = new List(24); List preOffset = getSolarTermsPreOffset(year - 1); List nextOffset = getSolarTermsNextOffset(year - 1); -// print("getSolarTerms:" + preOffset.toString()); -// print("getSolarTerms:" + nextOffset.toString()); System.arraycopy(preOffset, 0, solarTerms, 0, preOffset.length); System.arraycopy(nextOffset, 0, solarTerms, 22, nextOffset.length); diff --git a/lib/widget/calendar_view.dart b/lib/widget/calendar_view.dart index cdf3647..241ceeb 100644 --- a/lib/widget/calendar_view.dart +++ b/lib/widget/calendar_view.dart @@ -2,6 +2,7 @@ import 'package:flutter/material.dart'; import 'package:flutter_custom_calendar/calendar_provider.dart'; import 'package:flutter_custom_calendar/controller.dart'; import 'package:flutter_custom_calendar/model/date_model.dart'; +import 'package:flutter_custom_calendar/utils/LogUtil.dart'; import 'package:flutter_custom_calendar/widget/month_view_pager.dart'; import 'package:flutter_custom_calendar/widget/week_view_pager.dart'; import 'package:provider/provider.dart'; @@ -83,7 +84,7 @@ class CalendarContainerState extends State @override Widget build(BuildContext context) { - print("CalendarContainerState build"); + LogUtil.log(TAG: this.runtimeType, message: "CalendarContainerState build"); //暂时先这样写死,提前计算布局的高度,不然会出现问题:a horizontal viewport was given an unlimited amount of I/flutter ( 6759): vertical space in which to expand. itemHeight = MediaQuery.of(context).size.width / 7; totalHeight = itemHeight * 6 + 10 * (6 - 1); diff --git a/lib/widget/month_view.dart b/lib/widget/month_view.dart index 83d3ad1..e2a1e50 100644 --- a/lib/widget/month_view.dart +++ b/lib/widget/month_view.dart @@ -3,6 +3,7 @@ import 'package:flutter_custom_calendar/calendar_provider.dart'; import 'package:flutter_custom_calendar/configuration.dart'; import 'package:flutter_custom_calendar/constants/constants.dart'; import 'package:flutter_custom_calendar/model/date_model.dart'; +import 'package:flutter_custom_calendar/utils/LogUtil.dart'; import 'package:flutter_custom_calendar/utils/date_util.dart'; import 'package:provider/provider.dart'; @@ -59,11 +60,7 @@ class _MonthViewState extends State { itemHeight = MediaQuery.of(context).size.width / 7; totalHeight = itemHeight * lineCount + mainSpacing * (lineCount - 1); - return GestureDetector( - onVerticalDragStart: (DragStartDetails detail) { - print("onHorizontalDragStart:$detail"); - }, - child: Container(height: totalHeight, child: getView())); + return Container(height: totalHeight, child: getView()); } Widget getView() { @@ -71,8 +68,10 @@ class _MonthViewState extends State { builder: (context, calendarProvider, child) { CalendarConfiguration configuration = calendarProvider.calendarConfiguration; - print( - "MonthView Consumer:calendarProvider.selectDateModel:${calendarProvider.selectDateModel}"); + LogUtil.log( + TAG: this.runtimeType, + message: + "build calendarProvider.selectDateModel:${calendarProvider.selectDateModel}"); return new GridView.builder( physics: NeverScrollableScrollPhysics(), gridDelegate: new SliverGridDelegateWithFixedCrossAxisCount( @@ -97,8 +96,9 @@ class _MonthViewState extends State { return GestureDetector( onTap: () { - print("GestureDetector onTap:$dateModel"); - print("!dateModel.isInRange:${!dateModel.isInRange}"); + LogUtil.log( + TAG: this.runtimeType, + message: "GestureDetector onTap: $dateModel}"); //范围外不可点击 if (!dateModel.isInRange) { diff --git a/lib/widget/month_view_pager.dart b/lib/widget/month_view_pager.dart index 77338b1..854b47a 100644 --- a/lib/widget/month_view_pager.dart +++ b/lib/widget/month_view_pager.dart @@ -16,6 +16,8 @@ class MonthViewPager extends StatefulWidget { class _MonthViewPagerState extends State { CalendarProvider calendarProvider; + var totalHeight; + @override void initState() { calendarProvider = Provider.of(context, listen: false); @@ -32,8 +34,6 @@ class _MonthViewPagerState extends State { .add(Duration( days: DateUtil.getMonthDaysCount( firstDayOfMonth.year, firstDayOfMonth.month)))); -// print("firstDayOfMonth:$firstDayOfMonth"); -// print("lastDayOfMonth:$lastDayOfMonth"); if ((dateModel.isAfter(firstDayOfMonth) || dateModel.isSameWith(firstDayOfMonth)) && @@ -42,8 +42,6 @@ class _MonthViewPagerState extends State { break; } } -// print("monthList:$monthList"); -// print("当前月:index:$index"); WidgetsBinding.instance.addPostFrameCallback((timeStamp) { calendarProvider.calendarConfiguration.pageController.jumpToPage(index); }); @@ -61,32 +59,46 @@ class _MonthViewPagerState extends State { CalendarConfiguration configuration = calendarProvider.calendarConfiguration; - return Container( - child: PageView.builder( - onPageChanged: (position) { - //月份的变化 - DateModel dateModel = configuration.monthList[position]; - configuration.monthChange(dateModel.year, dateModel.month); - }, - controller: configuration.pageController, - itemBuilder: (context, index) { - DateModel dateModel = configuration.monthList[index]; - return new MonthView( - year: dateModel.year, - month: dateModel.month, - minSelectDate: DateModel.fromDateTime(DateTime( - configuration.minSelectYear, - configuration.minSelectMonth, - configuration.minSelectDay)), - maxSelectDate: DateModel.fromDateTime(DateTime( - configuration.maxSelectYear, - configuration.maxSelectMonth, - configuration.maxSelectDay)), - extraDataMap: configuration.extraDataMap, - ); - }, - itemCount: configuration.monthList.length, - ), + return PageView.builder( + onPageChanged: (position) { + //月份的变化 + DateModel dateModel = configuration.monthList[position]; + configuration.monthChange(dateModel.year, dateModel.month); + // + calendarProvider.lastClickDateModel = configuration.monthList[position]; + //计算下高度,使PageView自适应高度 + +// double itemHeight = MediaQuery.of(context).size.width / 7; +// +// int lineCount = +// DateUtil.getMonthViewLineCount(dateModel.year, dateModel.month); +// double newHeight = itemHeight * lineCount + +// configuration.verticalSpacing * (lineCount - 1); +// if(totalHeight!=newHeight){ +// totalHeight=newHeight; +// setState(() { +// +// }); +// } + }, + controller: configuration.pageController, + itemBuilder: (context, index) { + DateModel dateModel = configuration.monthList[index]; + return new MonthView( + year: dateModel.year, + month: dateModel.month, + minSelectDate: DateModel.fromDateTime(DateTime( + configuration.minSelectYear, + configuration.minSelectMonth, + configuration.minSelectDay)), + maxSelectDate: DateModel.fromDateTime(DateTime( + configuration.maxSelectYear, + configuration.maxSelectMonth, + configuration.maxSelectDay)), + extraDataMap: configuration.extraDataMap, + ); + }, + itemCount: configuration.monthList.length, ); } } diff --git a/lib/widget/week_view_pager.dart b/lib/widget/week_view_pager.dart index fe439df..b1885fb 100644 --- a/lib/widget/week_view_pager.dart +++ b/lib/widget/week_view_pager.dart @@ -72,6 +72,8 @@ class _WeekViewPagerState extends State { configuration.monthChange( firstDayOfWeek.year, firstDayOfWeek.month); } + calendarProvider.lastClickDateModel = + configuration.weekList[position]; // DateModel dateModel = configuration.weekList[position]; // configuration.monthChange(dateModel.year, dateModel.month); },