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

引入provider状态管理,避免深层嵌套传递信息
develop
xiaodong 5 years ago
parent e9f98bcd99
commit d48e3101ce

@ -0,0 +1,43 @@
import 'package:flutter/material.dart';
import 'package:flutter_custom_calendar/configuration.dart';
import 'package:flutter_custom_calendar/model/date_model.dart';
/**
* provider
*
* rebuild
*/
class CalendarProvider extends ChangeNotifier {
Set<DateModel> selectedDateList = new Set(); //,
DateModel _selectDateModel; //
DateModel get selectDateModel => _selectDateModel;
set selectDateModel(DateModel value) {
_selectDateModel = value;
print("notifyListeners:$value");
notifyListeners();
}
//
CalendarConfiguration calendarConfiguration;
void initData(
{Set<DateModel> selectedDateList,
DateModel selectDateModel,
CalendarConfiguration calendarConfiguration}) {
print("CalendarProvider init");
if (selectedDateList != null) {
this.selectedDateList.addAll(selectedDateList);
}
this.selectDateModel = selectDateModel;
this.calendarConfiguration = calendarConfiguration;
}
//退
void clearData() {
print("CalendarProvider clearData");
selectedDateList.clear();
selectDateModel = null;
}
}

@ -0,0 +1,88 @@
import 'package:flutter/material.dart';
import 'package:flutter_custom_calendar/controller.dart';
import 'package:flutter_custom_calendar/model/date_model.dart';
/**
*
*/
class CalendarConfiguration {
//,MODE_SINGLE_SELECTMODE_MULTI_SELECT
int selectMode;
//
bool expandStatus;
//
int minYear;
int maxYear;
//
int minYearMonth;
int maxYearMonth;
//
int nowYear;
int nowMonth;
//,
int minSelectYear;
int minSelectMonth;
int minSelectDay;
int maxSelectYear;
int maxSelectMonth;
int maxSelectDay; //
Set<DateModel> defaultSelectedDateList = new Set(); //
int maxMultiSelectCount; //
Map<DateTime, Object> extraDataMap = new Map(); //
/**
* UI
*/
//
OnMonthChange monthChange; //
OnCalendarSelect calendarSelect; //
OnMultiSelectOutOfRange multiSelectOutOfRange; //
OnMultiSelectOutOfSize multiSelectOutOfSize; //
//
DayWidgetBuilder dayWidgetBuilder; //item
WeekBarItemWidgetBuilder weekBarItemWidgetBuilder; //weekbar
/**
*
*/
List<DateModel> monthList = new List(); //list
List<DateModel> weekList = new List(); //list
PageController pageController; //controller
PageController weekController; //controller
CalendarConfiguration(
{this.selectMode,
this.expandStatus,
this.minYear,
this.maxYear,
this.minYearMonth,
this.maxYearMonth,
this.nowYear,
this.nowMonth,
this.minSelectYear,
this.minSelectMonth,
this.minSelectDay,
this.maxSelectYear,
this.maxSelectMonth,
this.maxSelectDay,
this.defaultSelectedDateList,
this.maxMultiSelectCount,
this.extraDataMap,
this.monthList,
this.weekList,
this.pageController,
this.weekController});
@override
String toString() {
return 'CalendarConfiguration{selectMode: $selectMode, expandStatus: $expandStatus, minYear: $minYear, maxYear: $maxYear, minYearMonth: $minYearMonth, maxYearMonth: $maxYearMonth, nowYear: $nowYear, nowMonth: $nowMonth, minSelectYear: $minSelectYear, minSelectMonth: $minSelectMonth, minSelectDay: $minSelectDay, maxSelectYear: $maxSelectYear, maxSelectMonth: $maxSelectMonth, maxSelectDay: $maxSelectDay, defaultSelectedDateList: $defaultSelectedDateList, maxMultiSelectCount: $maxMultiSelectCount, extraDataMap: $extraDataMap, monthList: $monthList, weekList: $weekList, pageController: $pageController, weekController: $weekController}';
}
}

@ -1,9 +1,13 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_custom_calendar/CalendarProvider.dart';
import 'package:flutter_custom_calendar/configuration.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';
import 'package:flutter_custom_calendar/model/date_model.dart'; import 'package:flutter_custom_calendar/model/date_model.dart';
import 'package:flutter_custom_calendar/widget/default_week_bar.dart'; import 'package:flutter_custom_calendar/widget/default_week_bar.dart';
import 'package:flutter_custom_calendar/constants/constants.dart'; import 'package:flutter_custom_calendar/constants/constants.dart';
import 'package:provider/provider.dart';
/** /**
* controller * controller
@ -13,55 +17,19 @@ class CalendarController {
static const Set<DateTime> EMPTY_SET = {}; static const Set<DateTime> EMPTY_SET = {};
static const Map<DateTime, Object> EMPTY_MAP = {}; static const Map<DateTime, Object> EMPTY_MAP = {};
//,MODE_SINGLE_SELECTMODE_MULTI_SELECT CalendarConfiguration calendarConfiguration;
int selectMode;
// CalendarProvider calendarProvider = CalendarProvider();
bool expandStatus;
ValueNotifier<bool> expandChanged;
//
int minYear;
int maxYear;
//
int minYearMonth;
int maxYearMonth;
//
int nowYear;
int nowMonth;
//,
int minSelectYear;
int minSelectMonth;
int minSelectDay;
int maxSelectYear;
int maxSelectMonth;
int maxSelectDay; //
Set<DateModel> selectedDateList = new Set(); //,
DateModel selectDateModel; //,
int maxMultiSelectCount; //
Map<DateTime, Object> extraDataMap = new Map(); //
//
OnMonthChange monthChange; //
OnCalendarSelect calendarSelect; //
OnMultiSelectOutOfRange multiSelectOutOfRange; //
OnMultiSelectOutOfSize multiSelectOutOfSize; //
//
DayWidgetBuilder dayWidgetBuilder; //item
WeekBarItemWidgetBuilder weekBarItemWidgetBuilder; //weekbar
/** /**
* *
*/ */
List<DateModel> monthList = new List(); //list List<DateModel> monthList = new List(); //list
List<DateModel> weekList=new List();//list List<DateModel> weekList = new List(); //list
PageController pageController; PageController pageController; //controller
PageController weekController; //controller
ValueNotifier<bool> expandChanged; //
CalendarController( CalendarController(
{int selectMode = Constants.MODE_SINGLE_SELECT, {int selectMode = Constants.MODE_SINGLE_SELECT,
@ -84,32 +52,31 @@ class CalendarController {
DateModel selectDateModel, DateModel selectDateModel,
int maxMultiSelectCount = 9999, int maxMultiSelectCount = 9999,
Map<DateTime, Object> extraDataMap = EMPTY_MAP}) { Map<DateTime, Object> extraDataMap = EMPTY_MAP}) {
this.selectMode = selectMode; this.expandChanged = ValueNotifier(expandStatus);
this.minYear = minYear;
this.maxYear = maxYear; calendarConfiguration = CalendarConfiguration(
this.minYearMonth = minYearMonth; selectMode: selectMode,
this.maxYearMonth = maxYearMonth; minYear: minYear,
this.nowYear = nowYear; maxYear: maxYear,
this.nowMonth = nowMonth; maxYearMonth: maxYearMonth,
this.minSelectYear = minSelectYear; nowYear: nowYear,
this.minSelectMonth = minSelectMonth; nowMonth: nowMonth,
this.minSelectDay = minSelectDay; minSelectYear: minSelectYear,
this.maxSelectYear = maxSelectYear; minSelectMonth: minSelectMonth,
this.maxSelectMonth = maxSelectMonth; minYearMonth: minYearMonth,
this.maxSelectDay = maxSelectDay; minSelectDay: minSelectDay,
this.selectDateModel = selectDateModel; maxSelectYear: maxSelectYear,
this.dayWidgetBuilder = dayWidgetBuilder; maxSelectMonth: maxSelectMonth,
this.weekBarItemWidgetBuilder = weekBarItemWidgetBuilder; maxSelectDay: maxSelectDay);
this.maxMultiSelectCount = maxMultiSelectCount;
this.extraDataMap = extraDataMap; calendarConfiguration.dayWidgetBuilder = dayWidgetBuilder;
this.expandStatus = expandStatus; calendarConfiguration.weekBarItemWidgetBuilder = weekBarItemWidgetBuilder;
this.expandChanged=ValueNotifier(expandStatus);
this.selectedDateList = Set();
if (selectedDateTimeList != null && selectedDateTimeList.isNotEmpty) { if (selectedDateTimeList != null && selectedDateTimeList.isNotEmpty) {
this.selectedDateList.addAll(selectedDateTimeList.map((dateTime) { calendarConfiguration.defaultSelectedDateList
return DateModel.fromDateTime(dateTime); .addAll(selectedDateTimeList.map((dateTime) {
}).toSet()); return DateModel.fromDateTime(dateTime);
}).toSet());
} }
//pageController,initialPage //pageController,initialPage
@ -141,26 +108,56 @@ class CalendarController {
} }
} }
this.pageController = new PageController(initialPage: initialPage); this.pageController = new PageController(initialPage: initialPage);
//
///72
int initialWeekPage;
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.isBefore(lastDay);
dateTime = dateTime.add(Duration(days: 7))) {
DateModel dateModel = DateModel.fromDateTime(dateTime);
weekList.add(dateModel);
}
this.weekController = new PageController();
calendarConfiguration.monthList = monthList;
calendarConfiguration.weekList = weekList;
calendarConfiguration.pageController = pageController;
calendarConfiguration.weekController = weekController;
calendarConfiguration.dayWidgetBuilder = dayWidgetBuilder;
calendarConfiguration.weekBarItemWidgetBuilder = weekBarItemWidgetBuilder;
} }
// //
void addMonthChangeListener(OnMonthChange listener) { void addMonthChangeListener(OnMonthChange listener) {
this.monthChange = listener; this.calendarConfiguration.monthChange = listener;
} }
// //
void addOnCalendarSelectListener(OnCalendarSelect listener) { void addOnCalendarSelectListener(OnCalendarSelect listener) {
this.calendarSelect = listener; this.calendarConfiguration.calendarSelect = listener;
} }
// //
void addOnMultiSelectOutOfRangeListener(OnMultiSelectOutOfRange listener) { void addOnMultiSelectOutOfRangeListener(OnMultiSelectOutOfRange listener) {
this.multiSelectOutOfRange = listener; this.calendarConfiguration.multiSelectOutOfRange = listener;
} }
// //
void addOnMultiSelectOutOfSizeListener(OnMultiSelectOutOfSize listener) { void addOnMultiSelectOutOfSizeListener(OnMultiSelectOutOfSize listener) {
this.multiSelectOutOfSize = listener; this.calendarConfiguration.multiSelectOutOfSize = listener;
} }
// //
@ -237,30 +234,21 @@ class CalendarController {
//, //,
Set<DateModel> getMultiSelectCalendar() { Set<DateModel> getMultiSelectCalendar() {
if (selectedDateList.isEmpty) { return calendarProvider.selectedDateList;
return null;
}
return selectedDateList;
} }
// //
DateModel getSingleSelectCalendar() { DateModel getSingleSelectCalendar() {
if (selectDateModel == null) { return calendarProvider.selectDateModel;
return null;
}
return selectDateModel;
} }
// //
void toggleExpandStatus(){ void toggleExpandStatus() {
expandChanged.value=!expandChanged.value; expandChanged.value = !expandChanged.value;
print("toggleExpandStatus${expandChanged.value}"); print("toggleExpandStatus${expandChanged.value}");
} }
void addExpandChangeListener(){ void addExpandChangeListener() {}
}
} }
/** /**

@ -14,6 +14,16 @@ class DateUtil {
dateTime.weekday == DateTime.sunday; dateTime.weekday == DateTime.sunday;
} }
/**
*
*/
static int getYearDaysCount(int year) {
if (isLeapYear(year)) {
return 366;
}
return 365;
}
/** /**
* *
* *

@ -1,13 +1,19 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_custom_calendar/CalendarProvider.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/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/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';
/** /**
* *
*/ */
//StatefulWidgetStatelessWidget
class CalendarViewWidget extends StatefulWidget { class CalendarViewWidget extends StatefulWidget {
// //
BoxDecoration boxDecoration; BoxDecoration boxDecoration;
@ -15,125 +21,155 @@ class CalendarViewWidget extends StatefulWidget {
// //
CalendarController calendarController; CalendarController calendarController;
CalendarViewWidget({@required this.calendarController, this.boxDecoration}); CalendarViewWidget(
{Key key, @required this.calendarController, this.boxDecoration})
: super(key: key);
@override @override
_CalendarViewWidgetState createState() => _CalendarViewWidgetState(); _CalendarViewWidgetState createState() => _CalendarViewWidgetState();
} }
class _CalendarViewWidgetState extends State<CalendarViewWidget> class _CalendarViewWidgetState extends State<CalendarViewWidget> {
@override
void initState() {
widget.calendarController.calendarProvider.initData(
calendarConfiguration: widget.calendarController.calendarConfiguration);
super.initState();
}
@override
Widget build(BuildContext context) {
return ChangeNotifierProvider<CalendarProvider>.value(
value: widget.calendarController.calendarProvider,
child: Container(
//
decoration: widget.boxDecoration,
child: const CalendarContainer()),
);
}
}
class CalendarContainer extends StatefulWidget {
const CalendarContainer();
@override
CalendarContainerState createState() => CalendarContainerState();
}
class CalendarContainerState extends State<CalendarContainer>
with SingleTickerProviderStateMixin { with SingleTickerProviderStateMixin {
double itemHeight; double itemHeight;
double totalHeight; double totalHeight;
bool expand = true; bool expand = true;
AnimationController _animationController;
@override @override
void initState() { void initState() {
_animationController = // widget.calendarController.expandChanged.addListener(() {
AnimationController(duration: Duration(milliseconds: 200), vsync: this); // print("_CalendarViewWidgetState:$expand");
// setState(() {
// expand = !expand;
// });
// });
//
}
widget.calendarController.expandChanged.addListener(() { @override
print("_CalendarViewWidgetState:$expand"); void dispose() {
setState(() { super.dispose();
expand = !expand;
});
});
} }
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
print("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);
return Container( return Container(
//
decoration: widget.boxDecoration,
child: new Column( child: new Column(
children: <Widget>[ children: <Widget>[
/** /**
* constsetStateview * constsetStateview
*/ */
widget.calendarController.weekBarItemWidgetBuilder(), // widget.calendarController.weekBarItemWidgetBuilder(),
AnimatedContainer( AnimatedContainer(
duration: Duration(milliseconds: 500), duration: Duration(milliseconds: 500),
height: expand ? totalHeight : itemHeight, height: expand ? totalHeight : itemHeight,
child: expand child:
? Container( // expand ?
height: totalHeight, Container(
child: MonthViewPager( height: totalHeight,
selectMode: widget.calendarController.selectMode, child: MonthViewPager(
monthChange: (int year, int month) { // selectMode: widget.calendarController.selectMode,
widget.calendarController.monthChange(year, month); // monthChange: (int year, int month) {
}, // widget.calendarController.monthChange(year, month);
calendarSelect: (dateModel) { // },
widget.calendarController.selectDateModel = dateModel; // calendarSelect: (dateModel) {
widget.calendarController.calendarSelect(dateModel); // widget.calendarController.selectDateModel = dateModel;
}, // widget.calendarController.calendarSelect(dateModel);
monthList: widget.calendarController.monthList, // },
pageController: widget.calendarController.pageController, // monthList: widget.calendarController.monthList,
selectedDateList: // pageController: widget.calendarController.pageController,
widget.calendarController.selectedDateList, // selectedDateList:
selectDateModel: // widget.calendarController.selectedDateList,
widget.calendarController.selectDateModel, // selectDateModel:
dayWidgetBuilder: // widget.calendarController.selectDateModel,
widget.calendarController.dayWidgetBuilder, // dayWidgetBuilder:
minSelectDate: DateModel() // widget.calendarController.dayWidgetBuilder,
..year = widget.calendarController.minSelectYear // minSelectDate: DateModel()
..month = widget.calendarController.minSelectMonth // ..year = widget.calendarController.minSelectYear
..day = widget.calendarController.minSelectDay, // ..month = widget.calendarController.minSelectMonth
maxSelectDate: DateModel() // ..day = widget.calendarController.minSelectDay,
..year = widget.calendarController.maxSelectYear // maxSelectDate: DateModel()
..month = widget.calendarController.maxSelectMonth // ..year = widget.calendarController.maxSelectYear
..day = widget.calendarController.maxSelectDay, // ..month = widget.calendarController.maxSelectMonth
maxMultiSelectCount: // ..day = widget.calendarController.maxSelectDay,
widget.calendarController.maxMultiSelectCount, // maxMultiSelectCount:
multiSelectOutOfRange: // widget.calendarController.maxMultiSelectCount,
widget.calendarController.multiSelectOutOfRange, // multiSelectOutOfRange:
multiSelectOutOfSize: // widget.calendarController.multiSelectOutOfRange,
widget.calendarController.multiSelectOutOfSize, // multiSelectOutOfSize:
extraDataMap: widget.calendarController.extraDataMap, // widget.calendarController.multiSelectOutOfSize,
), // extraDataMap: widget.calendarController.extraDataMap,
)
: Container(
height: itemHeight,
child: WeekViewPager(
selectMode: widget.calendarController.selectMode,
monthChange: (int year, int month) {
widget.calendarController.monthChange(year, month);
},
calendarSelect: (dateModel) {
widget.calendarController.selectDateModel = dateModel;
widget.calendarController.calendarSelect(dateModel);
},
weekList: widget.calendarController.weekList,
pageController: widget.calendarController.pageController,
selectedDateList:
widget.calendarController.selectedDateList,
selectDateModel:
widget.calendarController.selectDateModel,
dayWidgetBuilder:
widget.calendarController.dayWidgetBuilder,
minSelectDate: DateModel()
..year = widget.calendarController.minSelectYear
..month = widget.calendarController.minSelectMonth
..day = widget.calendarController.minSelectDay,
maxSelectDate: DateModel()
..year = widget.calendarController.maxSelectYear
..month = widget.calendarController.maxSelectMonth
..day = widget.calendarController.maxSelectDay,
maxMultiSelectCount:
widget.calendarController.maxMultiSelectCount,
multiSelectOutOfRange:
widget.calendarController.multiSelectOutOfRange,
multiSelectOutOfSize:
widget.calendarController.multiSelectOutOfSize,
extraDataMap: widget.calendarController.extraDataMap,
), ),
), )
), // : Container(
// height: itemHeight,
// child: WeekViewPager(
// selectMode: widget.calendarController.selectMode,
// monthChange: (int year, int month) {
// widget.calendarController.monthChange(year, month);
// },
// calendarSelect: (dateModel) {
// widget.calendarController.selectDateModel = dateModel;
// widget.calendarController.calendarSelect(dateModel);
// },
// weekList: widget.calendarController.weekList,
// pageController: widget.calendarController.pageController,
// selectedDateList:
// widget.calendarController.selectedDateList,
// selectDateModel:
// widget.calendarController.selectDateModel,
// dayWidgetBuilder:
// widget.calendarController.dayWidgetBuilder,
// minSelectDate: DateModel()
// ..year = widget.calendarController.minSelectYear
// ..month = widget.calendarController.minSelectMonth
// ..day = widget.calendarController.minSelectDay,
// maxSelectDate: DateModel()
// ..year = widget.calendarController.maxSelectYear
// ..month = widget.calendarController.maxSelectMonth
// ..day = widget.calendarController.maxSelectDay,
// maxMultiSelectCount:
// widget.calendarController.maxMultiSelectCount,
// multiSelectOutOfRange:
// widget.calendarController.multiSelectOutOfRange,
// multiSelectOutOfSize:
// widget.calendarController.multiSelectOutOfSize,
// extraDataMap: widget.calendarController.extraDataMap,
// ),
// ),
),
], ],
), ),
); );

@ -1,23 +1,25 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_custom_calendar/CalendarProvider.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/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/date_util.dart'; import 'package:flutter_custom_calendar/utils/date_util.dart';
import 'package:provider/provider.dart';
/** /**
* *
*/ */
class MonthView extends StatefulWidget { class MonthView extends StatefulWidget {
OnCalendarSelect onCalendarSelectListener; // OnCalendarSelect onCalendarSelectListener;
Set<DateModel> selectedDateList; // // Set<DateModel> selectedDateList; //
//
DateModel selectDateModel; //, // DateModel selectDateModel; //,
//
DayWidgetBuilder dayWidgetBuilder; // DayWidgetBuilder dayWidgetBuilder;
//
OnMultiSelectOutOfRange multiSelectOutOfRange; // // OnMultiSelectOutOfRange multiSelectOutOfRange; //
OnMultiSelectOutOfSize multiSelectOutOfSize; // // OnMultiSelectOutOfSize multiSelectOutOfSize; //
int year; int year;
int month; int month;
@ -26,26 +28,15 @@ class MonthView extends StatefulWidget {
DateModel minSelectDate; DateModel minSelectDate;
DateModel maxSelectDate; DateModel maxSelectDate;
int selectMode;
int maxMultiSelectCount;
Map<DateTime, Object> extraDataMap; // Map<DateTime, Object> extraDataMap; //
MonthView( MonthView({
{@required this.year, @required this.year,
@required this.month, @required this.month,
this.day, this.day,
this.onCalendarSelectListener, this.minSelectDate,
this.dayWidgetBuilder, this.maxSelectDate,
this.selectedDateList, });
this.selectDateModel,
this.minSelectDate,
this.maxSelectDate,
this.selectMode,
this.multiSelectOutOfSize,
this.multiSelectOutOfRange,
this.maxMultiSelectCount,
this.extraDataMap});
@override @override
_MonthViewState createState() => _MonthViewState(); _MonthViewState createState() => _MonthViewState();
@ -60,27 +51,17 @@ class _MonthViewState extends State<MonthView> {
double totalHeight; double totalHeight;
double mainSpacing = 10; double mainSpacing = 10;
int year;
int month;
DateModel selectDateModel; //,
@override @override
void initState() { void initState() {
super.initState(); super.initState();
year = widget.year;
month = widget.month;
items = DateUtil.initCalendarForMonthView( items = DateUtil.initCalendarForMonthView(
year, month, DateTime.now(), DateTime.sunday, widget.year, widget.month, DateTime.now(), DateTime.sunday,
minSelectDate: widget.minSelectDate, minSelectDate: widget.minSelectDate,
maxSelectDate: widget.maxSelectDate, maxSelectDate: widget.maxSelectDate,
extraDataMap: widget.extraDataMap); extraDataMap: widget.extraDataMap);
lineCount = DateUtil.getMonthViewLineCount(year, month); lineCount = DateUtil.getMonthViewLineCount(widget.year, widget.month);
selectDateModel = widget.selectDateModel;
} }
@override @override
@ -96,67 +77,72 @@ class _MonthViewState extends State<MonthView> {
} }
Widget getView() { Widget getView() {
return new GridView.builder( return Consumer<CalendarProvider>(
physics: NeverScrollableScrollPhysics(), builder: (context, calendarProvider, child) {
gridDelegate: new SliverGridDelegateWithFixedCrossAxisCount( CalendarConfiguration configuration =
crossAxisCount: 7, mainAxisSpacing: 10), calendarProvider.calendarConfiguration;
itemCount: 7 * lineCount, print(
itemBuilder: (context, index) { "Consumer:calendarProvider.selectDateModel:${calendarProvider.selectDateModel}");
DateModel dateModel = items[index]; return new GridView.builder(
// physics: NeverScrollableScrollPhysics(),
if (widget.selectMode == Constants.MODE_MULTI_SELECT) { gridDelegate: new SliverGridDelegateWithFixedCrossAxisCount(
if (widget.selectedDateList.contains(dateModel)) { crossAxisCount: 7, mainAxisSpacing: 10),
dateModel.isSelected = true; itemCount: 7 * lineCount,
} else { itemBuilder: (context, index) {
dateModel.isSelected = false; DateModel dateModel = items[index];
} //
} else { if (configuration.selectMode == Constants.MODE_MULTI_SELECT) {
if (selectDateModel == dateModel) { if (calendarProvider.selectedDateList.contains(dateModel)) {
dateModel.isSelected = true; dateModel.isSelected = true;
} else {
dateModel.isSelected = false;
}
} else { } else {
dateModel.isSelected = false; if (calendarProvider.selectDateModel == dateModel) {
} dateModel.isSelected = true;
} } else {
dateModel.isSelected = false;
return GestureDetector(
onTap: () {
//
if (!dateModel.isInRange) {
//
if (widget.selectMode == Constants.MODE_MULTI_SELECT) {
widget.multiSelectOutOfRange();
}
return;
} }
}
if (widget.selectMode == Constants.MODE_MULTI_SELECT) { return GestureDetector(
// onTap: () {
if (widget.selectedDateList.length == print("GestureDetector onTap$dateModel");
widget.maxMultiSelectCount) { //
widget.multiSelectOutOfSize(); if (!dateModel.isInRange) {
//
if (configuration.selectMode == Constants.MODE_MULTI_SELECT) {
configuration.multiSelectOutOfRange();
}
return; return;
} }
// if (configuration.selectMode == Constants.MODE_MULTI_SELECT) {
selectDateModel = dateModel; //
widget.selectDateModel = dateModel; if (calendarProvider.selectedDateList.length ==
widget.onCalendarSelectListener(dateModel); configuration.maxMultiSelectCount) {
setState(() { configuration.multiSelectOutOfSize();
if (widget.selectedDateList.contains(dateModel)) { return;
widget.selectedDateList.remove(dateModel);
} else {
widget.selectedDateList.add(dateModel);
} }
});
} else { //
selectDateModel = dateModel; calendarProvider.selectDateModel = dateModel;
widget.selectDateModel = dateModel; configuration.calendarSelect(dateModel);
widget.onCalendarSelectListener(dateModel); setState(() {
setState(() {}); if (calendarProvider.selectedDateList.contains(dateModel)) {
} calendarProvider.selectedDateList.remove(dateModel);
}, } else {
child: widget.dayWidgetBuilder(dateModel), calendarProvider.selectedDateList.add(dateModel);
); }
}); });
} else {
calendarProvider.selectDateModel = dateModel;
configuration.calendarSelect(dateModel);
}
},
child: configuration.dayWidgetBuilder(dateModel),
);
});
});
} }
} }

@ -1,44 +1,12 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_custom_calendar/controller.dart'; import 'package:flutter_custom_calendar/CalendarProvider.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/widget/month_view.dart'; import 'package:flutter_custom_calendar/widget/month_view.dart';
import 'package:provider/provider.dart';
class MonthViewPager extends StatefulWidget { class MonthViewPager extends StatefulWidget {
final OnMonthChange monthChange; MonthViewPager();
final OnCalendarSelect calendarSelect;
final DayWidgetBuilder dayWidgetBuilder;
OnMultiSelectOutOfRange multiSelectOutOfRange; //
OnMultiSelectOutOfSize multiSelectOutOfSize; //
Set<DateModel> selectedDateList; //,
DateModel selectDateModel; //,
final List<DateModel> monthList;
PageController pageController;
DateModel minSelectDate;
DateModel maxSelectDate;
int selectMode;
int maxMultiSelectCount;
Map<DateTime, Object> extraDataMap ; //
MonthViewPager(
{this.monthChange,
this.calendarSelect,
this.monthList,
this.pageController,
this.selectedDateList,
this.selectDateModel,
this.dayWidgetBuilder,
this.minSelectDate,
this.maxSelectDate,
this.selectMode,
this.maxMultiSelectCount,
this.multiSelectOutOfRange,
this.multiSelectOutOfSize,
this.extraDataMap});
@override @override
_MonthViewPagerState createState() => _MonthViewPagerState(); _MonthViewPagerState createState() => _MonthViewPagerState();
@ -50,47 +18,46 @@ class _MonthViewPagerState extends State<MonthViewPager> {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
// CalendarProvider,listenfalse
CalendarProvider calendarProvider =
Provider.of<CalendarProvider>(context, listen: false);
CalendarConfiguration configuration =
calendarProvider.calendarConfiguration;
return Container( return Container(
child: PageView.builder( child: PageView.builder(
onPageChanged: (position) { onPageChanged: (position) {
// //
DateModel dateModel = widget.monthList[position]; DateModel dateModel = configuration.monthList[position];
widget.monthChange(dateModel.year, dateModel.month); configuration.monthChange(dateModel.year, dateModel.month);
}, },
controller: widget.pageController, controller: configuration.pageController,
itemBuilder: (context, index) { itemBuilder: (context, index) {
DateModel dateModel = widget.monthList[index]; DateModel dateModel = configuration.monthList[index];
return new MonthView( return new MonthView(
selectMode: widget.selectMode, // selectMode: configuration.selectMode,
year: dateModel.year, year: dateModel.year,
month: dateModel.month, month: dateModel.month,
selectDateModel: widget.selectDateModel, // selectDateModel: calendarProvider.selectDateModel,
selectedDateList: widget.selectedDateList, // selectedDateList: calendarProvider.selectedDateList,
onCalendarSelectListener: widget.calendarSelect, // onCalendarSelectListener: configuration.calendarSelect,
dayWidgetBuilder: widget.dayWidgetBuilder, // dayWidgetBuilder: configuration.dayWidgetBuilder,
minSelectDate: widget.minSelectDate, minSelectDate: DateModel.fromDateTime(DateTime(
maxSelectDate: widget.maxSelectDate, configuration.minSelectYear,
maxMultiSelectCount: widget.maxMultiSelectCount, configuration.minSelectMonth,
multiSelectOutOfRange: widget.multiSelectOutOfRange, configuration.minSelectDay)),
multiSelectOutOfSize: widget.multiSelectOutOfSize, maxSelectDate: DateModel.fromDateTime(DateTime(
extraDataMap: widget.extraDataMap, configuration.maxSelectYear,
configuration.maxSelectMonth,
configuration.maxSelectDay)),
// maxMultiSelectCount: configuration.maxMultiSelectCount,
// multiSelectOutOfRange: configuration.multiSelectOutOfRange,
// multiSelectOutOfSize: configuration.multiSelectOutOfSize,
// extraDataMap: configuration.extraDataMap,
); );
}, },
itemCount: widget.monthList.length, itemCount: configuration.monthList.length,
), ),
); );
// return SliverFillViewport(
// delegate: SliverChildBuilderDelegate((context, index) {
// DateModel dateModel = widget.monthList[index];
// return new MonthView(
// year: dateModel.year,
// month: dateModel.month,
// selectDateModel: widget.selectDateModel,
// selectedDateList: widget.selectedDateList,
// onCalendarSelectListener: widget.calendarSelect,
// );
// }, childCount: widget.monthList.length),
// );
} }
} }

Loading…
Cancel
Save