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

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

@ -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<DateModel> 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;
}

@ -38,7 +38,17 @@ class CalendarConfiguration {
Map<DateTime, Object> extraDataMap = new Map(); //
/**
* UI
* UI
*/
double verticalSpacing ;//item10
//
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;
}

@ -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<DateTime> selectedDateTimeList = EMPTY_SET,
DateModel selectDateModel,
int maxMultiSelectCount = 9999,
double verticalSpacing = 10,
Map<DateTime, Object> 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}");
//
///72
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,6 +254,7 @@ class CalendarController {
{bool needAnimation = false,
Duration duration = const Duration(milliseconds: 500),
Curve curve = Curves.ease}) {
if (calendarProvider.expandStatus.value == true) {
DateModel dateModel = DateModel.fromDateTime(DateTime(year, month, 1));
//
int targetPage = monthList.indexOf(dateModel);
@ -249,6 +270,28 @@ class CalendarController {
} else {
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) {
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 =

@ -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 '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<DateModel> 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<DateTime, Object> extraDataMap}) {
print("initCalendarForWeekView");
LogUtil.log(TAG: "DateUtil", message: "initCalendarForWeekView");
List<DateModel> items = List();
int weekDay = currentDate.weekday;

@ -2143,8 +2143,6 @@ class SolarTermUtil {
List<String> solarTerms = new List(24);
List<String> preOffset = getSolarTermsPreOffset(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(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/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<CalendarContainer>
@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);

@ -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<MonthView> {
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<MonthView> {
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<MonthView> {
return GestureDetector(
onTap: () {
print("GestureDetector onTap$dateModel");
print("!dateModel.isInRange:${!dateModel.isInRange}");
LogUtil.log(
TAG: this.runtimeType,
message: "GestureDetector onTap: $dateModel}");
//
if (!dateModel.isInRange) {

@ -16,6 +16,8 @@ class MonthViewPager extends StatefulWidget {
class _MonthViewPagerState extends State<MonthViewPager> {
CalendarProvider calendarProvider;
var totalHeight;
@override
void initState() {
calendarProvider = Provider.of<CalendarProvider>(context, listen: false);
@ -32,8 +34,6 @@ class _MonthViewPagerState extends State<MonthViewPager> {
.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<MonthViewPager> {
break;
}
}
// print("monthList:$monthList");
// print("当前月:index:$index");
WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
calendarProvider.calendarConfiguration.pageController.jumpToPage(index);
});
@ -61,12 +59,27 @@ class _MonthViewPagerState extends State<MonthViewPager> {
CalendarConfiguration configuration =
calendarProvider.calendarConfiguration;
return Container(
child: PageView.builder(
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) {
@ -86,7 +99,6 @@ class _MonthViewPagerState extends State<MonthViewPager> {
);
},
itemCount: configuration.monthList.length,
),
);
}
}

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

Loading…
Cancel
Save