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

引入provider状态管理,避免深层嵌套传递信息
周视图和月视图,联动
develop
LXD312569496 5 years ago committed by xiaodong
parent d48e3101ce
commit 116d216850

@ -1,3 +1,4 @@
import 'package:example/main.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_custom_calendar/flutter_custom_calendar.dart'; import 'package:flutter_custom_calendar/flutter_custom_calendar.dart';
@ -19,7 +20,8 @@ class _DefaultStylePageState extends State<DefaultStylePage> {
void initState() { void initState() {
text = "${DateTime.now().year}${DateTime.now().month}"; text = "${DateTime.now().year}${DateTime.now().month}";
controller = new CalendarController(); controller = new CalendarController(
minYear: 2019, minYearMonth: 8, maxYear: 2019, maxYearMonth: 9);
controller.addMonthChangeListener( controller.addMonthChangeListener(
(year, month) { (year, month) {
@ -50,13 +52,15 @@ class _DefaultStylePageState extends State<DefaultStylePage> {
new IconButton( new IconButton(
icon: Icon(Icons.navigate_before), icon: Icon(Icons.navigate_before),
onPressed: () { onPressed: () {
controller.moveToPreviousMonth(); // controller.moveToPreviousMonth();
controller.previousPage();
}), }),
new Text(text), new Text(text),
new IconButton( new IconButton(
icon: Icon(Icons.navigate_next), icon: Icon(Icons.navigate_next),
onPressed: () { onPressed: () {
controller.moveToNextMonth(); // controller.moveToNextMonth();
controller.nextPage();
}), }),
], ],
), ),
@ -69,7 +73,9 @@ class _DefaultStylePageState extends State<DefaultStylePage> {
), ),
), ),
floatingActionButton: FloatingActionButton( floatingActionButton: FloatingActionButton(
onPressed: () {}, onPressed: () {
controller.toggleExpandStatus();
},
tooltip: 'Increment', tooltip: 'Increment',
child: Icon(Icons.add), child: Icon(Icons.add),
), // This trailing comma makes auto-formatting nicer for build methods. ), // This trailing comma makes auto-formatting nicer for build methods.

@ -10,6 +10,7 @@ import 'package:flutter_custom_calendar/model/date_model.dart';
class CalendarProvider extends ChangeNotifier { class CalendarProvider extends ChangeNotifier {
Set<DateModel> selectedDateList = new Set(); //, Set<DateModel> selectedDateList = new Set(); //,
DateModel _selectDateModel; // DateModel _selectDateModel; //
DateModel lastClickDateModel; //
DateModel get selectDateModel => _selectDateModel; DateModel get selectDateModel => _selectDateModel;
@ -19,6 +20,8 @@ class CalendarProvider extends ChangeNotifier {
notifyListeners(); notifyListeners();
} }
ValueNotifier<bool> expandStatus = ValueNotifier(true); //
// //
CalendarConfiguration calendarConfiguration; CalendarConfiguration calendarConfiguration;
@ -32,6 +35,10 @@ class CalendarProvider extends ChangeNotifier {
} }
this.selectDateModel = selectDateModel; this.selectDateModel = selectDateModel;
this.calendarConfiguration = calendarConfiguration; this.calendarConfiguration = calendarConfiguration;
//lastClickDateModelitem
this.lastClickDateModel = selectDateModel != null
? selectDateModel
: DateModel.fromDateTime(DateTime.now());
} }
//退 //退

@ -10,7 +10,7 @@ class CalendarConfiguration {
int selectMode; int selectMode;
// //
bool expandStatus; bool defaultExpandStatus;
// //
int minYear; int minYear;
@ -60,7 +60,6 @@ class CalendarConfiguration {
CalendarConfiguration( CalendarConfiguration(
{this.selectMode, {this.selectMode,
this.expandStatus,
this.minYear, this.minYear,
this.maxYear, this.maxYear,
this.minYearMonth, this.minYearMonth,
@ -79,10 +78,13 @@ class CalendarConfiguration {
this.monthList, this.monthList,
this.weekList, this.weekList,
this.pageController, this.pageController,
this.weekController}); this.weekController,
bool defaultExpandStatus = true}) {
this.defaultExpandStatus = defaultExpandStatus;
}
@override @override
String toString() { 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}'; return 'CalendarConfiguration{selectMode: $selectMode, expandStatus: $defaultExpandStatus, 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,5 +1,5 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_custom_calendar/CalendarProvider.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/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';
@ -16,6 +16,7 @@ import 'package:provider/provider.dart';
class CalendarController { 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 = {};
static const Duration DEFAULT_DURATION = const Duration(milliseconds: 500);
CalendarConfiguration calendarConfiguration; CalendarConfiguration calendarConfiguration;
@ -26,11 +27,9 @@ class CalendarController {
*/ */
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; //controller PageController monthController; //controller
PageController weekController; //controller PageController weekController; //controller
ValueNotifier<bool> expandChanged; //
CalendarController( CalendarController(
{int selectMode = Constants.MODE_SINGLE_SELECT, {int selectMode = Constants.MODE_SINGLE_SELECT,
bool expandStatus = true, bool expandStatus = true,
@ -52,7 +51,7 @@ 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.expandChanged = ValueNotifier(expandStatus); // this.expandChanged = ValueNotifier(expandStatus);
calendarConfiguration = CalendarConfiguration( calendarConfiguration = CalendarConfiguration(
selectMode: selectMode, selectMode: selectMode,
@ -67,6 +66,7 @@ class CalendarController {
minSelectDay: minSelectDay, minSelectDay: minSelectDay,
maxSelectYear: maxSelectYear, maxSelectYear: maxSelectYear,
maxSelectMonth: maxSelectMonth, maxSelectMonth: maxSelectMonth,
defaultExpandStatus: expandStatus,
maxSelectDay: maxSelectDay); maxSelectDay: maxSelectDay);
calendarConfiguration.dayWidgetBuilder = dayWidgetBuilder; calendarConfiguration.dayWidgetBuilder = dayWidgetBuilder;
@ -107,7 +107,7 @@ class CalendarController {
nowMonthIndex++; nowMonthIndex++;
} }
} }
this.pageController = new PageController(initialPage: initialPage); this.monthController = new PageController(initialPage: initialPage);
// //
///72 ///72
@ -125,16 +125,17 @@ class CalendarController {
DateUtil.getMonthDaysCount(maxYear, maxYearMonth)); DateUtil.getMonthDaysCount(maxYear, maxYearMonth));
print("最后一天:$lastDay"); print("最后一天:$lastDay");
for (DateTime dateTime = firstWeekDate; for (DateTime dateTime = firstWeekDate;
dateTime.isBefore(lastDay); !dateTime.isAfter(lastDay);
dateTime = dateTime.add(Duration(days: 7))) { dateTime = dateTime.add(Duration(days: 7))) {
DateModel dateModel = DateModel.fromDateTime(dateTime); DateModel dateModel = DateModel.fromDateTime(dateTime);
weekList.add(dateModel); weekList.add(dateModel);
} }
this.weekController = new PageController(); this.weekController = new PageController();
print("weekList:$weekList");
calendarConfiguration.monthList = monthList; calendarConfiguration.monthList = monthList;
calendarConfiguration.weekList = weekList; calendarConfiguration.weekList = weekList;
calendarConfiguration.pageController = pageController; calendarConfiguration.pageController = monthController;
calendarConfiguration.weekController = weekController; calendarConfiguration.weekController = weekController;
calendarConfiguration.dayWidgetBuilder = dayWidgetBuilder; calendarConfiguration.dayWidgetBuilder = dayWidgetBuilder;
calendarConfiguration.weekBarItemWidgetBuilder = weekBarItemWidgetBuilder; calendarConfiguration.weekBarItemWidgetBuilder = weekBarItemWidgetBuilder;
@ -160,6 +161,74 @@ class CalendarController {
this.calendarConfiguration.multiSelectOutOfSize = listener; this.calendarConfiguration.multiSelectOutOfSize = listener;
} }
//
void toggleExpandStatus() {
calendarProvider.expandStatus.value = !calendarProvider.expandStatus.value;
print("toggleExpandStatus${calendarProvider.expandStatus.value}");
}
//
void addExpandChangeListener(ValueChanged<bool> expandChange) {
calendarProvider.expandStatus.addListener(() {
expandChange(calendarProvider.expandStatus.value);
});
}
/**
*
*/
Future<bool> previousPage() async {
if (calendarProvider.expandStatus.value == true) {
//
int currentIndex = monthController.page.toInt();
if (currentIndex == 0) {
return false;
} else {
monthController.previousPage(
duration: DEFAULT_DURATION, curve: Curves.ease);
return true;
}
} else {
//
int currentIndex = weekController.page.toInt();
if (currentIndex == 0) {
return false;
} else {
weekController.previousPage(
duration: DEFAULT_DURATION, curve: Curves.ease);
return true;
}
}
}
/**
*
* true
* false:
*/
Future<bool> nextPage() async {
if (calendarProvider.expandStatus.value == true) {
//
int currentIndex = monthController.page.toInt();
if (monthList.length - 1 == currentIndex) {
return false;
} else {
monthController.nextPage(
duration: DEFAULT_DURATION, curve: Curves.ease);
return true;
}
} else {
//
int currentIndex = weekController.page.toInt();
if (weekList.length - 1 == currentIndex) {
return false;
} else {
weekController.nextPage(duration: DEFAULT_DURATION, curve: Curves.ease);
return true;
}
}
}
// //
void moveToCalendar(int year, int month, int day, void moveToCalendar(int year, int month, int day,
{bool needAnimation = false, {bool needAnimation = false,
@ -171,11 +240,14 @@ class CalendarController {
if (targetPage == -1) { if (targetPage == -1) {
return; return;
} }
if (monthController.hasClients == false) {
return;
}
if (needAnimation) { if (needAnimation) {
pageController.animateToPage(targetPage, monthController.animateToPage(targetPage,
duration: duration, curve: curve); duration: duration, curve: curve);
} else { } else {
pageController.jumpToPage(targetPage); monthController.jumpToPage(targetPage);
} }
} }
@ -185,7 +257,7 @@ class CalendarController {
Duration duration = const Duration(milliseconds: 500), Duration duration = const Duration(milliseconds: 500),
Curve curve = Curves.ease}) { Curve curve = Curves.ease}) {
DateTime targetDateTime = DateTime targetDateTime =
monthList[pageController.page.toInt() + 12].getDateTime(); monthList[monthController.page.toInt() + 12].getDateTime();
moveToCalendar( moveToCalendar(
targetDateTime.year, targetDateTime.month, targetDateTime.day, targetDateTime.year, targetDateTime.month, targetDateTime.day,
needAnimation: needAnimation, duration: duration, curve: curve); needAnimation: needAnimation, duration: duration, curve: curve);
@ -197,7 +269,7 @@ class CalendarController {
Duration duration = const Duration(milliseconds: 500), Duration duration = const Duration(milliseconds: 500),
Curve curve = Curves.ease}) { Curve curve = Curves.ease}) {
DateTime targetDateTime = DateTime targetDateTime =
monthList[pageController.page.toInt() - 12].getDateTime(); monthList[monthController.page.toInt() - 12].getDateTime();
moveToCalendar( moveToCalendar(
targetDateTime.year, targetDateTime.month, targetDateTime.day, targetDateTime.year, targetDateTime.month, targetDateTime.day,
needAnimation: needAnimation, duration: duration, curve: curve); needAnimation: needAnimation, duration: duration, curve: curve);
@ -208,8 +280,24 @@ 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}) {
// indexweekController
if (calendarProvider.expandStatus.value == false) {
int currentMonth = weekList[weekController.page.toInt()].month;
for (int i = weekController.page.toInt(); i < weekList.length; i++) {
if (weekList[i].month != currentMonth) {
weekController.jumpToPage(i);
break;
}
}
return;
}
if ((monthController.page.toInt() + 1) >= monthList.length) {
print("moveToNextMonth当前是最后一个月份");
return;
}
DateTime targetDateTime = DateTime targetDateTime =
monthList[pageController.page.toInt() + 1].getDateTime(); monthList[monthController.page.toInt() + 1].getDateTime();
moveToCalendar( moveToCalendar(
targetDateTime.year, targetDateTime.month, targetDateTime.day, targetDateTime.year, targetDateTime.month, targetDateTime.day,
needAnimation: needAnimation, duration: duration, curve: curve); needAnimation: needAnimation, duration: duration, curve: curve);
@ -220,8 +308,27 @@ 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}) {
// indexweekController
if (calendarProvider.expandStatus.value == false) {
int currentMonth = weekList[weekController.page.toInt()].month;
for (int i = weekController.page.toInt(); i >= 0; i--) {
if (weekList[i].month != currentMonth &&
weekList[i].isAfter(DateModel.fromDateTime(DateTime(
calendarConfiguration.minYear,
calendarConfiguration.minYearMonth)))) {
weekController.jumpToPage(i);
break;
}
}
return;
}
if ((monthController.page.toInt()) == 0) {
print("moveToPreviousMonth当前是第一个月份");
return;
}
DateTime targetDateTime = DateTime targetDateTime =
monthList[pageController.page.toInt() - 1].getDateTime(); monthList[monthController.page.toInt() - 1].getDateTime();
moveToCalendar( moveToCalendar(
targetDateTime.year, targetDateTime.month, targetDateTime.day, targetDateTime.year, targetDateTime.month, targetDateTime.day,
needAnimation: needAnimation, duration: duration, curve: curve); needAnimation: needAnimation, duration: duration, curve: curve);
@ -229,7 +336,7 @@ class CalendarController {
// //
DateModel getCurrentMonth() { DateModel getCurrentMonth() {
return monthList[pageController.page.toInt()]; return monthList[monthController.page.toInt()];
} }
//, //,
@ -241,14 +348,6 @@ class CalendarController {
DateModel getSingleSelectCalendar() { DateModel getSingleSelectCalendar() {
return calendarProvider.selectDateModel; return calendarProvider.selectDateModel;
} }
//
void toggleExpandStatus() {
expandChanged.value = !expandChanged.value;
print("toggleExpandStatus${expandChanged.value}");
}
void addExpandChangeListener() {}
} }
/** /**

@ -68,7 +68,13 @@ class DateModel {
day == dateModel.day; day == dateModel.day;
} }
//
bool isAfter(DateModel dateModel) {
return this.getDateTime().isAfter(dateModel.getDateTime());
}
//
bool isBefore(DateModel dateModel) {
return this.getDateTime().isBefore(dateModel.getDateTime());
}
} }

@ -191,6 +191,7 @@ class DateUtil {
{DateModel minSelectDate, {DateModel minSelectDate,
DateModel maxSelectDate, DateModel maxSelectDate,
Map<DateTime, Object> extraDataMap}) { Map<DateTime, Object> extraDataMap}) {
print("initCalendarForWeekView");
List<DateModel> items = List(); List<DateModel> items = List();
int weekDay = currentDate.weekday; int weekDay = currentDate.weekday;
@ -201,11 +202,24 @@ class DateUtil {
for (int i = 1; i <= 7; i++) { for (int i = 1; i <= 7; i++) {
DateModel dateModel = DateModel dateModel =
DateModel.fromDateTime(firstDayOfWeek.add(Duration(days: i))); DateModel.fromDateTime(firstDayOfWeek.add(Duration(days: i)));
items.add(dateModel);
}
print("items.toString():${items.toString()}"); //
if (dateModel.getDateTime().isAfter(minSelectDate.getDateTime()) &&
dateModel.getDateTime().isBefore(maxSelectDate.getDateTime())) {
dateModel.isInRange = true;
} else {
dateModel.isInRange = false;
}
//model
if (extraDataMap != null && extraDataMap.isNotEmpty) {
DateTime dateTime = dateModel.getDateTime();
if (extraDataMap.containsKey(dateTime)) {
dateModel.extraData = extraDataMap[dateTime];
}
}
items.add(dateModel);
}
return items; return items;
} }
} }

@ -1,8 +1,5 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter/services.dart'; import 'package:flutter_custom_calendar/calendar_provider.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/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';
@ -19,7 +16,7 @@ class CalendarViewWidget extends StatefulWidget {
BoxDecoration boxDecoration; BoxDecoration boxDecoration;
// //
CalendarController calendarController; final CalendarController calendarController;
CalendarViewWidget( CalendarViewWidget(
{Key key, @required this.calendarController, this.boxDecoration}) {Key key, @required this.calendarController, this.boxDecoration})
@ -32,8 +29,10 @@ class CalendarViewWidget extends StatefulWidget {
class _CalendarViewWidgetState extends State<CalendarViewWidget> { class _CalendarViewWidgetState extends State<CalendarViewWidget> {
@override @override
void initState() { void initState() {
//provider
widget.calendarController.calendarProvider.initData( widget.calendarController.calendarProvider.initData(
calendarConfiguration: widget.calendarController.calendarConfiguration); calendarConfiguration: widget.calendarController.calendarConfiguration);
super.initState(); super.initState();
} }
@ -44,6 +43,7 @@ class _CalendarViewWidgetState extends State<CalendarViewWidget> {
child: Container( child: Container(
// //
decoration: widget.boxDecoration, decoration: widget.boxDecoration,
//使constsetStatewidget
child: const CalendarContainer()), child: const CalendarContainer()),
); );
} }
@ -63,15 +63,17 @@ class CalendarContainerState extends State<CalendarContainer>
bool expand = true; bool expand = true;
CalendarProvider calendarProvider;
@override @override
void initState() { void initState() {
// widget.calendarController.expandChanged.addListener(() { calendarProvider = Provider.of<CalendarProvider>(context, listen: false);
// print("_CalendarViewWidgetState:$expand"); expand = calendarProvider.expandStatus.value;
// setState(() { calendarProvider.expandStatus.addListener(() {
// expand = !expand; setState(() {
// }); expand = !expand;
// }); });
// });
} }
@override @override
@ -91,84 +93,19 @@ class CalendarContainerState extends State<CalendarContainer>
/** /**
* constsetStateview * constsetStateview
*/ */
// widget.calendarController.weekBarItemWidgetBuilder(), calendarProvider.calendarConfiguration.weekBarItemWidgetBuilder(),
AnimatedContainer( AnimatedContainer(
duration: Duration(milliseconds: 500), duration: Duration(milliseconds: 200),
height: expand ? totalHeight : itemHeight, height: expand ? totalHeight : itemHeight,
child: child: expand
// expand ? ? Container(
Container(
height: totalHeight, height: totalHeight,
child: MonthViewPager( child: MonthViewPager(),
// selectMode: widget.calendarController.selectMode,
// monthChange: (int year, int month) {
// widget.calendarController.monthChange(year, month);
// },
// calendarSelect: (dateModel) {
// widget.calendarController.selectDateModel = dateModel;
// widget.calendarController.calendarSelect(dateModel);
// },
// monthList: widget.calendarController.monthList,
// 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( : Container(
// height: itemHeight, height: itemHeight,
// child: WeekViewPager( 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,
// ),
// ),
), ),
], ],
), ),

@ -0,0 +1,32 @@
import 'package:flutter/material.dart';
/**
*
*/
class FastClickWidget extends StatelessWidget {
final int between_time = 500; //500ms
Function onTap;
Widget child;
FastClickWidget({@required this.onTap, @required this.child});
int lastClickTime = 0;
@override
Widget build(BuildContext context) {
return GestureDetector(
onTap: () {
if (lastClickTime == 0 ||
DateTime.now().millisecondsSinceEpoch - lastClickTime >
between_time) {
onTap();
lastClickTime = DateTime.now().millisecondsSinceEpoch;
} else {
//500ms
}
},
child: child,
);
}
}

@ -1,5 +1,5 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_custom_calendar/CalendarProvider.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/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';
@ -10,32 +10,22 @@ import 'package:provider/provider.dart';
* *
*/ */
class MonthView extends StatefulWidget { class MonthView extends StatefulWidget {
// OnCalendarSelect onCalendarSelectListener; final int year;
final int month;
final int day;
// Set<DateModel> selectedDateList; // final DateModel minSelectDate;
// final DateModel maxSelectDate;
// DateModel selectDateModel; //,
//
// DayWidgetBuilder dayWidgetBuilder;
//
// OnMultiSelectOutOfRange multiSelectOutOfRange; //
// OnMultiSelectOutOfSize multiSelectOutOfSize; //
int year; final Map<DateTime, Object> extraDataMap; //
int month;
int day;
DateModel minSelectDate; const MonthView({
DateModel maxSelectDate;
Map<DateTime, Object> extraDataMap; //
MonthView({
@required this.year, @required this.year,
@required this.month, @required this.month,
this.day, this.day,
this.minSelectDate, this.minSelectDate,
this.maxSelectDate, this.maxSelectDate,
this.extraDataMap,
}); });
@override @override
@ -82,7 +72,7 @@ class _MonthViewState extends State<MonthView> {
CalendarConfiguration configuration = CalendarConfiguration configuration =
calendarProvider.calendarConfiguration; calendarProvider.calendarConfiguration;
print( print(
"Consumer:calendarProvider.selectDateModel:${calendarProvider.selectDateModel}"); "MonthView Consumer:calendarProvider.selectDateModel:${calendarProvider.selectDateModel}");
return new GridView.builder( return new GridView.builder(
physics: NeverScrollableScrollPhysics(), physics: NeverScrollableScrollPhysics(),
gridDelegate: new SliverGridDelegateWithFixedCrossAxisCount( gridDelegate: new SliverGridDelegateWithFixedCrossAxisCount(
@ -108,6 +98,8 @@ class _MonthViewState extends State<MonthView> {
return GestureDetector( return GestureDetector(
onTap: () { onTap: () {
print("GestureDetector onTap$dateModel"); print("GestureDetector onTap$dateModel");
print("!dateModel.isInRange:${!dateModel.isInRange}");
// //
if (!dateModel.isInRange) { if (!dateModel.isInRange) {
// //
@ -117,6 +109,8 @@ class _MonthViewState extends State<MonthView> {
return; return;
} }
calendarProvider.lastClickDateModel = dateModel;
if (configuration.selectMode == Constants.MODE_MULTI_SELECT) { if (configuration.selectMode == Constants.MODE_MULTI_SELECT) {
// //
if (calendarProvider.selectedDateList.length == if (calendarProvider.selectedDateList.length ==

@ -1,7 +1,8 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_custom_calendar/CalendarProvider.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/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/widget/month_view.dart'; import 'package:flutter_custom_calendar/widget/month_view.dart';
import 'package:provider/provider.dart'; import 'package:provider/provider.dart';
@ -13,14 +14,50 @@ class MonthViewPager extends StatefulWidget {
} }
class _MonthViewPagerState extends State<MonthViewPager> { class _MonthViewPagerState extends State<MonthViewPager> {
CalendarProvider calendarProvider;
@override @override
void initState() {} void initState() {
calendarProvider = Provider.of<CalendarProvider>(context, listen: false);
//index
DateModel dateModel = calendarProvider.lastClickDateModel;
List<DateModel> monthList =
calendarProvider.calendarConfiguration.monthList;
int index = 0;
for (int i = 0; i < monthList.length; i++) {
DateModel firstDayOfMonth = monthList[i];
DateModel lastDayOfMonth = DateModel.fromDateTime(firstDayOfMonth
.getDateTime()
.add(Duration(
days: DateUtil.getMonthDaysCount(
firstDayOfMonth.year, firstDayOfMonth.month))));
// print("firstDayOfMonth:$firstDayOfMonth");
// print("lastDayOfMonth:$lastDayOfMonth");
if ((dateModel.isAfter(firstDayOfMonth) ||
dateModel.isSameWith(firstDayOfMonth)) &&
dateModel.isBefore(lastDayOfMonth)) {
index = i;
break;
}
}
// print("monthList:$monthList");
// print("当前月:index:$index");
WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
calendarProvider.calendarConfiguration.pageController.jumpToPage(index);
});
}
@override
void dispose() {
super.dispose();
}
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
// CalendarProvider,listenfalse // CalendarProvider,listenfalse
CalendarProvider calendarProvider = calendarProvider = Provider.of<CalendarProvider>(context, listen: false);
Provider.of<CalendarProvider>(context, listen: false);
CalendarConfiguration configuration = CalendarConfiguration configuration =
calendarProvider.calendarConfiguration; calendarProvider.calendarConfiguration;
@ -35,13 +72,8 @@ class _MonthViewPagerState extends State<MonthViewPager> {
itemBuilder: (context, index) { itemBuilder: (context, index) {
DateModel dateModel = configuration.monthList[index]; DateModel dateModel = configuration.monthList[index];
return new MonthView( return new MonthView(
// selectMode: configuration.selectMode,
year: dateModel.year, year: dateModel.year,
month: dateModel.month, month: dateModel.month,
// selectDateModel: calendarProvider.selectDateModel,
// selectedDateList: calendarProvider.selectedDateList,
// onCalendarSelectListener: configuration.calendarSelect,
// dayWidgetBuilder: configuration.dayWidgetBuilder,
minSelectDate: DateModel.fromDateTime(DateTime( minSelectDate: DateModel.fromDateTime(DateTime(
configuration.minSelectYear, configuration.minSelectYear,
configuration.minSelectMonth, configuration.minSelectMonth,
@ -50,10 +82,7 @@ class _MonthViewPagerState extends State<MonthViewPager> {
configuration.maxSelectYear, configuration.maxSelectYear,
configuration.maxSelectMonth, configuration.maxSelectMonth,
configuration.maxSelectDay)), configuration.maxSelectDay)),
// maxMultiSelectCount: configuration.maxMultiSelectCount, extraDataMap: configuration.extraDataMap,
// multiSelectOutOfRange: configuration.multiSelectOutOfRange,
// multiSelectOutOfSize: configuration.multiSelectOutOfSize,
// extraDataMap: configuration.extraDataMap,
); );
}, },
itemCount: configuration.monthList.length, itemCount: configuration.monthList.length,

@ -1,50 +1,31 @@
import 'package:flutter/material.dart'; 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/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/utils/date_util.dart'; import 'package:flutter_custom_calendar/utils/date_util.dart';
import 'package:provider/provider.dart';
/** /**
* *
*/ */
class WeekView extends StatefulWidget { class WeekView extends StatefulWidget {
OnCalendarSelect onCalendarSelectListener; final int year;
final int month;
final DateModel firstDayOfWeek;
Set<DateModel> selectedDateList; // final DateModel minSelectDate;
final DateModel maxSelectDate;
DateModel selectDateModel; //, final Map<DateTime, Object> extraDataMap; //
DayWidgetBuilder dayWidgetBuilder; const WeekView(
OnMultiSelectOutOfRange multiSelectOutOfRange; //
OnMultiSelectOutOfSize multiSelectOutOfSize; //
int year;
int month;
int day;
DateModel minSelectDate;
DateModel maxSelectDate;
int selectMode;
int maxMultiSelectCount;
Map<DateTime, Object> extraDataMap; //
WeekView(
{@required this.year, {@required this.year,
@required this.month, @required this.month,
this.day, this.firstDayOfWeek,
this.onCalendarSelectListener,
this.dayWidgetBuilder,
this.selectedDateList,
this.selectDateModel,
this.minSelectDate, this.minSelectDate,
this.maxSelectDate, this.maxSelectDate,
this.selectMode,
this.multiSelectOutOfSize,
this.multiSelectOutOfRange,
this.maxMultiSelectCount,
this.extraDataMap}); this.extraDataMap});
@override @override
@ -53,19 +34,25 @@ class WeekView extends StatefulWidget {
class _WeekViewState extends State<WeekView> { class _WeekViewState extends State<WeekView> {
List<DateModel> items; List<DateModel> items;
DateModel selectDateModel; //,
@override @override
void initState() { void initState() {
super.initState(); super.initState();
items=DateUtil.initCalendarForWeekView(2019, 9, DateTime.now(), 0); items = DateUtil.initCalendarForWeekView(
2019, 9, widget.firstDayOfWeek.getDateTime(), 0,
minSelectDate: widget.minSelectDate,
maxSelectDate: widget.maxSelectDate,
extraDataMap: widget.extraDataMap);
} }
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return Consumer<CalendarProvider>(
builder: (context, calendarProvider, child) {
CalendarConfiguration configuration =
calendarProvider.calendarConfiguration;
print(
"WeekView Consumer:calendarProvider.selectDateModel:${calendarProvider.selectDateModel}");
return new GridView.builder( return new GridView.builder(
physics: NeverScrollableScrollPhysics(), physics: NeverScrollableScrollPhysics(),
gridDelegate: new SliverGridDelegateWithFixedCrossAxisCount( gridDelegate: new SliverGridDelegateWithFixedCrossAxisCount(
@ -74,14 +61,14 @@ class _WeekViewState extends State<WeekView> {
itemBuilder: (context, index) { itemBuilder: (context, index) {
DateModel dateModel = items[index]; DateModel dateModel = items[index];
// //
if (widget.selectMode == Constants.MODE_MULTI_SELECT) { if (configuration.selectMode == Constants.MODE_MULTI_SELECT) {
if (widget.selectedDateList.contains(dateModel)) { if (calendarProvider.selectedDateList.contains(dateModel)) {
dateModel.isSelected = true; dateModel.isSelected = true;
} else { } else {
dateModel.isSelected = false; dateModel.isSelected = false;
} }
} else { } else {
if (selectDateModel == dateModel) { if (calendarProvider.selectDateModel == dateModel) {
dateModel.isSelected = true; dateModel.isSelected = true;
} else { } else {
dateModel.isSelected = false; dateModel.isSelected = false;
@ -91,42 +78,44 @@ class _WeekViewState extends State<WeekView> {
return GestureDetector( return GestureDetector(
onTap: () { onTap: () {
// //
print("GestureDetector onTap$dateModel");
print("!dateModel.isInRange:${!dateModel.isInRange}");
if (!dateModel.isInRange) { if (!dateModel.isInRange) {
// //
if (widget.selectMode == Constants.MODE_MULTI_SELECT) { if (configuration.selectMode == Constants.MODE_MULTI_SELECT) {
widget.multiSelectOutOfRange(); configuration.multiSelectOutOfRange();
} }
return; return;
} }
calendarProvider.lastClickDateModel = dateModel;
if (widget.selectMode == Constants.MODE_MULTI_SELECT) { if (configuration.selectMode == Constants.MODE_MULTI_SELECT) {
// //
if (widget.selectedDateList.length == if (calendarProvider.selectedDateList.length ==
widget.maxMultiSelectCount) { configuration.maxMultiSelectCount) {
widget.multiSelectOutOfSize(); configuration.multiSelectOutOfSize();
return; return;
} }
// //
selectDateModel = dateModel; calendarProvider.selectDateModel = dateModel;
widget.selectDateModel = dateModel; configuration.calendarSelect(dateModel);
widget.onCalendarSelectListener(dateModel); // setState(() {
setState(() { if (calendarProvider.selectedDateList.contains(dateModel)) {
if (widget.selectedDateList.contains(dateModel)) { calendarProvider.selectedDateList.remove(dateModel);
widget.selectedDateList.remove(dateModel);
} else { } else {
widget.selectedDateList.add(dateModel); calendarProvider.selectedDateList.add(dateModel);
} }
}); // });
} else { } else {
selectDateModel = dateModel; calendarProvider.selectDateModel = dateModel;
widget.selectDateModel = dateModel; configuration.calendarSelect(dateModel);
widget.onCalendarSelectListener(dateModel); // setState(() {});
setState(() {});
} }
}, },
child: widget.dayWidgetBuilder(dateModel), child: configuration.dayWidgetBuilder(dateModel),
); );
}); });
});
} }
} }

@ -1,88 +1,99 @@
import 'package:flutter/material.dart'; 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/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/week_view.dart'; import 'package:flutter_custom_calendar/widget/week_view.dart';
import 'package:provider/provider.dart';
class WeekViewPager extends StatefulWidget { class WeekViewPager extends StatefulWidget {
final OnMonthChange monthChange; WeekViewPager();
final OnCalendarSelect calendarSelect; @override
final DayWidgetBuilder dayWidgetBuilder; _WeekViewPagerState createState() => _WeekViewPagerState();
OnMultiSelectOutOfRange multiSelectOutOfRange; // }
OnMultiSelectOutOfSize multiSelectOutOfSize; //
Set<DateModel> selectedDateList; //,
DateModel selectDateModel; //,
final List<DateModel> weekList; class _WeekViewPagerState extends State<WeekViewPager> {
PageController pageController; int lastMonth; //
CalendarProvider calendarProvider;
DateModel minSelectDate; @override
DateModel maxSelectDate; void initState() {
print("WeekViewPager initState");
int selectMode; calendarProvider = Provider.of<CalendarProvider>(context, listen: false);
int maxMultiSelectCount;
Map<DateTime, Object> extraDataMap; // lastMonth = calendarProvider.lastClickDateModel.month;
//index
DateModel dateModel = calendarProvider.lastClickDateModel;
List<DateModel> weekList = calendarProvider.calendarConfiguration.weekList;
int index = 0;
WeekViewPager( for (int i = 0; i < weekList.length; i++) {
{this.monthChange, DateModel firstDayOfWeek = weekList[i];
this.calendarSelect, DateModel lastDayOfWeek = DateModel.fromDateTime(
this.weekList, firstDayOfWeek.getDateTime().add(Duration(days: 7)));
this.pageController,
this.selectedDateList,
this.selectDateModel,
this.dayWidgetBuilder,
this.minSelectDate,
this.maxSelectDate,
this.selectMode,
this.maxMultiSelectCount,
this.multiSelectOutOfRange,
this.multiSelectOutOfSize,
this.extraDataMap});
@override if ((dateModel.isSameWith(weekList[i]) ||
_WeekViewPagerState createState() => _WeekViewPagerState(); dateModel.isAfter(weekList[i])) &&
dateModel.isBefore(lastDayOfWeek)) {
index = i;
break;
}
}
// print("weekList:$weekList");
// print("当前周:index:$index");
WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
calendarProvider.calendarConfiguration.weekController.jumpToPage(index);
});
} }
class _WeekViewPagerState extends State<WeekViewPager> {
int lastMonth;//
@override @override
void initState() { void dispose() {
// lastMonth= print("WeekViewPager dispose");
super.dispose();
} }
@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.weekList[position]; DateModel firstDayOfWeek = configuration.weekList[position];
widget.monthChange(dateModel.year, dateModel.month); int currentMonth = firstDayOfWeek.month;
if (lastMonth != currentMonth) {
configuration.monthChange(
firstDayOfWeek.year, firstDayOfWeek.month);
}
// DateModel dateModel = configuration.weekList[position];
// configuration.monthChange(dateModel.year, dateModel.month);
}, },
controller: widget.pageController, controller: configuration.weekController,
itemBuilder: (context, index) { itemBuilder: (context, index) {
DateModel dateModel = widget.weekList[index]; DateModel dateModel = configuration.weekList[index];
return new WeekView( return new WeekView(
selectMode: widget.selectMode,
year: dateModel.year, year: dateModel.year,
month: dateModel.month, month: dateModel.month,
selectDateModel: widget.selectDateModel, firstDayOfWeek: dateModel,
selectedDateList: widget.selectedDateList, minSelectDate: DateModel.fromDateTime(DateTime(
onCalendarSelectListener: widget.calendarSelect, configuration.minSelectYear,
dayWidgetBuilder: widget.dayWidgetBuilder, configuration.minSelectMonth,
minSelectDate: widget.minSelectDate, configuration.minSelectDay)),
maxSelectDate: widget.maxSelectDate, maxSelectDate: DateModel.fromDateTime(DateTime(
maxMultiSelectCount: widget.maxMultiSelectCount, configuration.maxSelectYear,
multiSelectOutOfRange: widget.multiSelectOutOfRange, configuration.maxSelectMonth,
multiSelectOutOfSize: widget.multiSelectOutOfSize, configuration.maxSelectDay)),
extraDataMap: widget.extraDataMap, extraDataMap: configuration.extraDataMap,
); );
}, },
itemCount: widget.weekList.length, itemCount: configuration.weekList.length,
), ),
); );
} }

@ -10,7 +10,7 @@ environment:
dependencies: dependencies:
flutter: flutter:
sdk: flutter sdk: flutter
provider: 3.0.0+1
dev_dependencies: dev_dependencies:
flutter_test: flutter_test:
sdk: flutter sdk: flutter

Loading…
Cancel
Save