You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

143 lines
4.7 KiB

6 years ago
import 'package:flutter/material.dart';
import 'package:flutter_custom_calendar/calendar_provider.dart';
import 'package:flutter_custom_calendar/configuration.dart';
6 years ago
import 'package:flutter_custom_calendar/constants/constants.dart';
import 'package:flutter_custom_calendar/model/date_model.dart';
import 'package:flutter_custom_calendar/utils/date_util.dart';
import 'package:provider/provider.dart';
6 years ago
/**
*
*/
class MonthView extends StatefulWidget {
final int year;
final int month;
final int day;
6 years ago
final DateModel minSelectDate;
final DateModel maxSelectDate;
6 years ago
final Map<DateTime, Object> extraDataMap; //自定义额外的数据
6 years ago
const MonthView({
@required this.year,
@required this.month,
this.day,
this.minSelectDate,
this.maxSelectDate,
this.extraDataMap,
});
6 years ago
@override
_MonthViewState createState() => _MonthViewState();
}
class _MonthViewState extends State<MonthView> {
List<DateModel> items;
int lineCount;
double itemHeight;
double totalHeight;
double mainSpacing = 10;
@override
void initState() {
super.initState();
items = DateUtil.initCalendarForMonthView(
widget.year, widget.month, DateTime.now(), DateTime.sunday,
6 years ago
minSelectDate: widget.minSelectDate,
maxSelectDate: widget.maxSelectDate,
extraDataMap: widget.extraDataMap);
lineCount = DateUtil.getMonthViewLineCount(widget.year, widget.month);
6 years ago
}
@override
Widget build(BuildContext context) {
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()));
}
Widget getView() {
return Consumer<CalendarProvider>(
builder: (context, calendarProvider, child) {
CalendarConfiguration configuration =
calendarProvider.calendarConfiguration;
print(
"MonthView Consumer:calendarProvider.selectDateModel:${calendarProvider.selectDateModel}");
return new GridView.builder(
physics: NeverScrollableScrollPhysics(),
gridDelegate: new SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 7, mainAxisSpacing: 10),
itemCount: 7 * lineCount,
itemBuilder: (context, index) {
DateModel dateModel = items[index];
//判断是否被选择
if (configuration.selectMode == Constants.MODE_MULTI_SELECT) {
if (calendarProvider.selectedDateList.contains(dateModel)) {
dateModel.isSelected = true;
} else {
dateModel.isSelected = false;
}
6 years ago
} else {
if (calendarProvider.selectDateModel == dateModel) {
dateModel.isSelected = true;
} else {
dateModel.isSelected = false;
6 years ago
}
}
6 years ago
return GestureDetector(
onTap: () {
print("GestureDetector onTap$dateModel");
print("!dateModel.isInRange:${!dateModel.isInRange}");
//范围外不可点击
if (!dateModel.isInRange) {
//多选回调
if (configuration.selectMode == Constants.MODE_MULTI_SELECT) {
configuration.multiSelectOutOfRange();
}
6 years ago
return;
}
calendarProvider.lastClickDateModel = dateModel;
if (configuration.selectMode == Constants.MODE_MULTI_SELECT) {
//多选,判断是否超过限制,超过范围
if (calendarProvider.selectedDateList.length ==
configuration.maxMultiSelectCount) {
configuration.multiSelectOutOfSize();
return;
6 years ago
}
//多选也可以弄这些单选的代码
calendarProvider.selectDateModel = dateModel;
configuration.calendarSelect(dateModel);
setState(() {
if (calendarProvider.selectedDateList.contains(dateModel)) {
calendarProvider.selectedDateList.remove(dateModel);
} else {
calendarProvider.selectedDateList.add(dateModel);
}
});
} else {
calendarProvider.selectDateModel = dateModel;
configuration.calendarSelect(dateModel);
}
},
child: configuration.dayWidgetBuilder(dateModel),
);
});
});
6 years ago
}
}