创建configuration类,将配置的信息放到这里

引入provider状态管理,避免深层嵌套传递信息
周视图和月视图,联动
增加日志输出类LogUtil,方便查看调试
develop
xiaodong 5 years ago
parent 116d216850
commit 95f2ca12ac

@ -1,6 +1,7 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_custom_calendar/configuration.dart'; import 'package:flutter_custom_calendar/configuration.dart';
import 'package:flutter_custom_calendar/model/date_model.dart'; import 'package:flutter_custom_calendar/model/date_model.dart';
import 'package:flutter_custom_calendar/utils/LogUtil.dart';
/** /**
* provider * provider
@ -16,7 +17,9 @@ class CalendarProvider extends ChangeNotifier {
set selectDateModel(DateModel value) { set selectDateModel(DateModel value) {
_selectDateModel = value; _selectDateModel = value;
print("notifyListeners:$value"); LogUtil.log(
TAG: this.runtimeType,
message: "selectDateModel change:${selectDateModel}");
notifyListeners(); notifyListeners();
} }
@ -29,7 +32,7 @@ class CalendarProvider extends ChangeNotifier {
{Set<DateModel> selectedDateList, {Set<DateModel> selectedDateList,
DateModel selectDateModel, DateModel selectDateModel,
CalendarConfiguration calendarConfiguration}) { CalendarConfiguration calendarConfiguration}) {
print("CalendarProvider init"); LogUtil.log(TAG: this.runtimeType, message: "CalendarProvider initData");
if (selectedDateList != null) { if (selectedDateList != null) {
this.selectedDateList.addAll(selectedDateList); this.selectedDateList.addAll(selectedDateList);
} }
@ -43,7 +46,7 @@ class CalendarProvider extends ChangeNotifier {
//退 //退
void clearData() { void clearData() {
print("CalendarProvider clearData"); LogUtil.log(TAG: this.runtimeType, message: "CalendarProvider clearData");
selectedDateList.clear(); selectedDateList.clear();
selectDateModel = null; selectDateModel = null;
} }

@ -38,7 +38,17 @@ class CalendarConfiguration {
Map<DateTime, Object> extraDataMap = new Map(); // Map<DateTime, Object> extraDataMap = new Map(); //
/** /**
* UI * UI
*/
double verticalSpacing ;//item10
//
DayWidgetBuilder dayWidgetBuilder; //item
WeekBarItemWidgetBuilder weekBarItemWidgetBuilder; //weekbar
/**
*
*/ */
// //
OnMonthChange monthChange; // OnMonthChange monthChange; //
@ -46,10 +56,6 @@ class CalendarConfiguration {
OnMultiSelectOutOfRange multiSelectOutOfRange; // OnMultiSelectOutOfRange multiSelectOutOfRange; //
OnMultiSelectOutOfSize multiSelectOutOfSize; // OnMultiSelectOutOfSize multiSelectOutOfSize; //
//
DayWidgetBuilder dayWidgetBuilder; //item
WeekBarItemWidgetBuilder weekBarItemWidgetBuilder; //weekbar
/** /**
* *
*/ */
@ -79,6 +85,7 @@ class CalendarConfiguration {
this.weekList, this.weekList,
this.pageController, this.pageController,
this.weekController, this.weekController,
this.verticalSpacing,
bool defaultExpandStatus = true}) { bool defaultExpandStatus = true}) {
this.defaultExpandStatus = defaultExpandStatus; this.defaultExpandStatus = defaultExpandStatus;
} }

@ -1,6 +1,9 @@
import 'dart:developer';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_custom_calendar/calendar_provider.dart'; import 'package:flutter_custom_calendar/calendar_provider.dart';
import 'package:flutter_custom_calendar/configuration.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/utils/date_util.dart';
import 'package:flutter_custom_calendar/widget/default_combine_day_view.dart'; import 'package:flutter_custom_calendar/widget/default_combine_day_view.dart';
import 'package:flutter_custom_calendar/widget/default_custom_day_view.dart'; import 'package:flutter_custom_calendar/widget/default_custom_day_view.dart';
@ -50,9 +53,9 @@ class CalendarController {
Set<DateTime> selectedDateTimeList = EMPTY_SET, Set<DateTime> selectedDateTimeList = EMPTY_SET,
DateModel selectDateModel, DateModel selectDateModel,
int maxMultiSelectCount = 9999, int maxMultiSelectCount = 9999,
double verticalSpacing = 10,
Map<DateTime, Object> extraDataMap = EMPTY_MAP}) { Map<DateTime, Object> extraDataMap = EMPTY_MAP}) {
// this.expandChanged = ValueNotifier(expandStatus); LogUtil.log(TAG: this.runtimeType, message: "init CalendarConfiguration");
calendarConfiguration = CalendarConfiguration( calendarConfiguration = CalendarConfiguration(
selectMode: selectMode, selectMode: selectMode,
minYear: minYear, minYear: minYear,
@ -67,7 +70,8 @@ class CalendarController {
maxSelectYear: maxSelectYear, maxSelectYear: maxSelectYear,
maxSelectMonth: maxSelectMonth, maxSelectMonth: maxSelectMonth,
defaultExpandStatus: expandStatus, defaultExpandStatus: expandStatus,
maxSelectDay: maxSelectDay); maxSelectDay: maxSelectDay,
verticalSpacing: verticalSpacing);
calendarConfiguration.dayWidgetBuilder = dayWidgetBuilder; calendarConfiguration.dayWidgetBuilder = dayWidgetBuilder;
calendarConfiguration.weekBarItemWidgetBuilder = weekBarItemWidgetBuilder; calendarConfiguration.weekBarItemWidgetBuilder = weekBarItemWidgetBuilder;
@ -108,22 +112,33 @@ class CalendarController {
} }
} }
this.monthController = new PageController(initialPage: initialPage); 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}");
// //
///72 ///72
int initialWeekPage; int initialWeekPage = 0;
int nowWeekIndex = 0; int nowWeekIndex = 0;
weekList.clear(); weekList.clear();
int sumDays = 0; //
DateTime firstDayOfMonth = DateTime(minYear, minYearMonth, 1); DateTime firstDayOfMonth = DateTime(minYear, minYearMonth, 1);
// //
DateTime firstWeekDate = DateTime firstWeekDate =
firstDayOfMonth.add(Duration(days: -(firstDayOfMonth.weekday - 1))); firstDayOfMonth.add(Duration(days: -(firstDayOfMonth.weekday - 1)));
print("第一个星期的第一天的日期firstWeekDate:$firstWeekDate");
DateTime lastDay = DateTime(maxYear, maxYearMonth, DateTime lastDay = DateTime(maxYear, maxYearMonth,
DateUtil.getMonthDaysCount(maxYear, maxYearMonth)); DateUtil.getMonthDaysCount(maxYear, maxYearMonth));
print("最后一天:$lastDay");
for (DateTime dateTime = firstWeekDate; for (DateTime dateTime = firstWeekDate;
!dateTime.isAfter(lastDay); !dateTime.isAfter(lastDay);
dateTime = dateTime.add(Duration(days: 7))) { dateTime = dateTime.add(Duration(days: 7))) {
@ -131,7 +146,10 @@ class CalendarController {
weekList.add(dateModel); weekList.add(dateModel);
} }
this.weekController = new PageController(); this.weekController = new PageController();
print("weekList:$weekList"); LogUtil.log(
TAG: this.runtimeType,
message:
"初始化星期视图的信息:一共有${weekList.length}个星期initialPage为${initialWeekPage}");
calendarConfiguration.monthList = monthList; calendarConfiguration.monthList = monthList;
calendarConfiguration.weekList = weekList; calendarConfiguration.weekList = weekList;
@ -164,7 +182,9 @@ class CalendarController {
// //
void toggleExpandStatus() { void toggleExpandStatus() {
calendarProvider.expandStatus.value = !calendarProvider.expandStatus.value; calendarProvider.expandStatus.value = !calendarProvider.expandStatus.value;
print("toggleExpandStatus${calendarProvider.expandStatus.value}"); LogUtil.log(
TAG: this.runtimeType,
message: "toggleExpandStatus${calendarProvider.expandStatus.value}");
} }
// //
@ -234,6 +254,7 @@ class CalendarController {
{bool needAnimation = false, {bool needAnimation = false,
Duration duration = const Duration(milliseconds: 500), Duration duration = const Duration(milliseconds: 500),
Curve curve = Curves.ease}) { Curve curve = Curves.ease}) {
if (calendarProvider.expandStatus.value == true) {
DateModel dateModel = DateModel.fromDateTime(DateTime(year, month, 1)); DateModel dateModel = DateModel.fromDateTime(DateTime(year, month, 1));
// //
int targetPage = monthList.indexOf(dateModel); int targetPage = monthList.indexOf(dateModel);
@ -249,6 +270,28 @@ class CalendarController {
} else { } else {
monthController.jumpToPage(targetPage); monthController.jumpToPage(targetPage);
} }
} else {
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) { if ((monthController.page.toInt() + 1) >= monthList.length) {
print("moveToNextMonth当前是最后一个月份"); LogUtil.log(TAG: this.runtimeType, message: "moveToNextMonth当前是最后一个月份");
return; return;
} }
DateTime targetDateTime = DateTime targetDateTime =
@ -324,7 +367,8 @@ class CalendarController {
} }
if ((monthController.page.toInt()) == 0) { if ((monthController.page.toInt()) == 0) {
print("moveToPreviousMonth当前是第一个月份"); LogUtil.log(
TAG: this.runtimeType, message: "moveToPreviousMonth当前是第一个月份");
return; return;
} }
DateTime targetDateTime = DateTime targetDateTime =

@ -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");
}
}
}

@ -1,6 +1,7 @@
import 'dart:math'; import 'dart:math';
import 'package:flutter_custom_calendar/model/date_model.dart'; 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); int monthDayCount = getMonthDaysCount(year, month);
print("$year$month月,有$monthDayCount天,第一天的index为${mPreDiff}"); LogUtil.log(
TAG: "DateUtil",
message:
"initCalendarForMonthView:$year$month月,有$monthDayCount天,第一天的index为${mPreDiff}");
List<DateModel> result = new List(); List<DateModel> result = new List();
@ -179,7 +183,10 @@ class DateUtil {
int preIndex = firstDayOfMonth.weekday - 1; int preIndex = firstDayOfMonth.weekday - 1;
// int lastIndex = lastDayOfMonth.weekday; // 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; return ((preIndex + monthDayCount) / 7).toInt() + 1;
} }
@ -191,7 +198,7 @@ class DateUtil {
{DateModel minSelectDate, {DateModel minSelectDate,
DateModel maxSelectDate, DateModel maxSelectDate,
Map<DateTime, Object> extraDataMap}) { Map<DateTime, Object> extraDataMap}) {
print("initCalendarForWeekView"); LogUtil.log(TAG: "DateUtil", message: "initCalendarForWeekView");
List<DateModel> items = List(); List<DateModel> items = List();
int weekDay = currentDate.weekday; int weekDay = currentDate.weekday;

@ -2143,8 +2143,6 @@ class SolarTermUtil {
List<String> solarTerms = new List(24); List<String> solarTerms = new List(24);
List<String> preOffset = getSolarTermsPreOffset(year - 1); List<String> preOffset = getSolarTermsPreOffset(year - 1);
List<String> nextOffset = getSolarTermsNextOffset(year - 1); List<String> nextOffset = getSolarTermsNextOffset(year - 1);
// print("getSolarTerms:" + preOffset.toString());
// print("getSolarTerms:" + nextOffset.toString());
System.arraycopy(preOffset, 0, solarTerms, 0, preOffset.length); System.arraycopy(preOffset, 0, solarTerms, 0, preOffset.length);
System.arraycopy(nextOffset, 0, solarTerms, 22, nextOffset.length); System.arraycopy(nextOffset, 0, solarTerms, 22, nextOffset.length);

@ -2,6 +2,7 @@ import 'package:flutter/material.dart';
import 'package:flutter_custom_calendar/calendar_provider.dart'; import 'package:flutter_custom_calendar/calendar_provider.dart';
import 'package:flutter_custom_calendar/controller.dart'; import 'package:flutter_custom_calendar/controller.dart';
import 'package:flutter_custom_calendar/model/date_model.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/month_view_pager.dart';
import 'package:flutter_custom_calendar/widget/week_view_pager.dart'; import 'package:flutter_custom_calendar/widget/week_view_pager.dart';
import 'package:provider/provider.dart'; import 'package:provider/provider.dart';
@ -83,7 +84,7 @@ class CalendarContainerState extends State<CalendarContainer>
@override @override
Widget build(BuildContext context) { 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. //,,: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; itemHeight = MediaQuery.of(context).size.width / 7;
totalHeight = itemHeight * 6 + 10 * (6 - 1); totalHeight = itemHeight * 6 + 10 * (6 - 1);

@ -3,6 +3,7 @@ import 'package:flutter_custom_calendar/calendar_provider.dart';
import 'package:flutter_custom_calendar/configuration.dart'; import 'package:flutter_custom_calendar/configuration.dart';
import 'package:flutter_custom_calendar/constants/constants.dart'; import 'package:flutter_custom_calendar/constants/constants.dart';
import 'package:flutter_custom_calendar/model/date_model.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:flutter_custom_calendar/utils/date_util.dart';
import 'package:provider/provider.dart'; import 'package:provider/provider.dart';
@ -59,11 +60,7 @@ class _MonthViewState extends State<MonthView> {
itemHeight = MediaQuery.of(context).size.width / 7; itemHeight = MediaQuery.of(context).size.width / 7;
totalHeight = itemHeight * lineCount + mainSpacing * (lineCount - 1); totalHeight = itemHeight * lineCount + mainSpacing * (lineCount - 1);
return GestureDetector( return Container(height: totalHeight, child: getView());
onVerticalDragStart: (DragStartDetails detail) {
print("onHorizontalDragStart:$detail");
},
child: Container(height: totalHeight, child: getView()));
} }
Widget getView() { Widget getView() {
@ -71,8 +68,10 @@ class _MonthViewState extends State<MonthView> {
builder: (context, calendarProvider, child) { builder: (context, calendarProvider, child) {
CalendarConfiguration configuration = CalendarConfiguration configuration =
calendarProvider.calendarConfiguration; calendarProvider.calendarConfiguration;
print( LogUtil.log(
"MonthView Consumer:calendarProvider.selectDateModel:${calendarProvider.selectDateModel}"); TAG: this.runtimeType,
message:
"build calendarProvider.selectDateModel:${calendarProvider.selectDateModel}");
return new GridView.builder( return new GridView.builder(
physics: NeverScrollableScrollPhysics(), physics: NeverScrollableScrollPhysics(),
gridDelegate: new SliverGridDelegateWithFixedCrossAxisCount( gridDelegate: new SliverGridDelegateWithFixedCrossAxisCount(
@ -97,8 +96,9 @@ class _MonthViewState extends State<MonthView> {
return GestureDetector( return GestureDetector(
onTap: () { onTap: () {
print("GestureDetector onTap$dateModel"); LogUtil.log(
print("!dateModel.isInRange:${!dateModel.isInRange}"); TAG: this.runtimeType,
message: "GestureDetector onTap: $dateModel}");
// //
if (!dateModel.isInRange) { if (!dateModel.isInRange) {

@ -16,6 +16,8 @@ class MonthViewPager extends StatefulWidget {
class _MonthViewPagerState extends State<MonthViewPager> { class _MonthViewPagerState extends State<MonthViewPager> {
CalendarProvider calendarProvider; CalendarProvider calendarProvider;
var totalHeight;
@override @override
void initState() { void initState() {
calendarProvider = Provider.of<CalendarProvider>(context, listen: false); calendarProvider = Provider.of<CalendarProvider>(context, listen: false);
@ -32,8 +34,6 @@ class _MonthViewPagerState extends State<MonthViewPager> {
.add(Duration( .add(Duration(
days: DateUtil.getMonthDaysCount( days: DateUtil.getMonthDaysCount(
firstDayOfMonth.year, firstDayOfMonth.month)))); firstDayOfMonth.year, firstDayOfMonth.month))));
// print("firstDayOfMonth:$firstDayOfMonth");
// print("lastDayOfMonth:$lastDayOfMonth");
if ((dateModel.isAfter(firstDayOfMonth) || if ((dateModel.isAfter(firstDayOfMonth) ||
dateModel.isSameWith(firstDayOfMonth)) && dateModel.isSameWith(firstDayOfMonth)) &&
@ -42,8 +42,6 @@ class _MonthViewPagerState extends State<MonthViewPager> {
break; break;
} }
} }
// print("monthList:$monthList");
// print("当前月:index:$index");
WidgetsBinding.instance.addPostFrameCallback((timeStamp) { WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
calendarProvider.calendarConfiguration.pageController.jumpToPage(index); calendarProvider.calendarConfiguration.pageController.jumpToPage(index);
}); });
@ -61,12 +59,27 @@ class _MonthViewPagerState extends State<MonthViewPager> {
CalendarConfiguration configuration = CalendarConfiguration configuration =
calendarProvider.calendarConfiguration; calendarProvider.calendarConfiguration;
return Container( return PageView.builder(
child: PageView.builder(
onPageChanged: (position) { onPageChanged: (position) {
// //
DateModel dateModel = configuration.monthList[position]; DateModel dateModel = configuration.monthList[position];
configuration.monthChange(dateModel.year, dateModel.month); 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, controller: configuration.pageController,
itemBuilder: (context, index) { itemBuilder: (context, index) {
@ -86,7 +99,6 @@ class _MonthViewPagerState extends State<MonthViewPager> {
); );
}, },
itemCount: configuration.monthList.length, itemCount: configuration.monthList.length,
),
); );
} }
} }

@ -72,6 +72,8 @@ class _WeekViewPagerState extends State<WeekViewPager> {
configuration.monthChange( configuration.monthChange(
firstDayOfWeek.year, firstDayOfWeek.month); firstDayOfWeek.year, firstDayOfWeek.month);
} }
calendarProvider.lastClickDateModel =
configuration.weekList[position];
// DateModel dateModel = configuration.weekList[position]; // DateModel dateModel = configuration.weekList[position];
// configuration.monthChange(dateModel.year, dateModel.month); // configuration.monthChange(dateModel.year, dateModel.month);
}, },

Loading…
Cancel
Save