多选模式bug修复:超过指定个数后,报错空指针

多选模式无效的问题
develop^2
xiaodong 5 years ago
parent 390a826be4
commit 0049168104

@ -28,6 +28,9 @@ class _MultiSelectStylePageState extends State<MultiSelectStylePage> {
minSelectYear: 2019,
minSelectMonth: 5,
minSelectDay: 20,
selectedDateTimeList: {
DateTime.now(),
}
);
controller.addMonthChangeListener(
@ -45,7 +48,7 @@ class _MultiSelectStylePageState extends State<MultiSelectStylePage> {
text = new ValueNotifier("${DateTime.now().year}${DateTime.now().month}");
selectText = new ValueNotifier(
"多选模式\n选中的时间:\n${controller.getSingleSelectCalendar()}");
"多选模式\n选中的时间:\n${controller.getMultiSelectCalendar().join("\n")}");
}
@override

@ -1,3 +1,5 @@
import 'dart:collection';
import 'package:flutter/material.dart';
import 'package:flutter_custom_calendar/cache_data.dart';
import 'package:flutter_custom_calendar/configuration.dart';
@ -15,7 +17,7 @@ import 'package:flutter_custom_calendar/widget/month_view.dart';
*/
class CalendarProvider extends ChangeNotifier {
double _totalHeight; //
Set<DateModel> selectedDateList = new Set(); //,
HashSet<DateModel> selectedDateList = new HashSet<DateModel>(); //,
DateModel _selectDateModel; //
ItemContainerState lastClickItemState;
DateModel _lastClickDateModel;
@ -117,17 +119,20 @@ class CalendarProvider extends ChangeNotifier {
WeekBarItemWidgetBuilder weekBarItemWidgetBuilder,
}) {
LogUtil.log(TAG: this.runtimeType, message: "CalendarProvider initData");
if (selectedDateList != null) {
this.selectedDateList.addAll(selectedDateList);
}
this.selectDateModel = selectDateModel;
this.calendarConfiguration = calendarConfiguration;
print(
"calendarConfiguration.defaultSelectedDateList:${calendarConfiguration.defaultSelectedDateList}");
this
.selectedDateList
.addAll(this.calendarConfiguration.defaultSelectedDateList);
this.selectDateModel = this.calendarConfiguration.selectDateModel;
this.calendarConfiguration.padding = padding;
this.calendarConfiguration.margin = margin;
this.calendarConfiguration.itemSize = itemSize;
this.calendarConfiguration.verticalSpacing = verticalSpacing;
this.calendarConfiguration.dayWidgetBuilder=dayWidgetBuilder;
this.calendarConfiguration.weekBarItemWidgetBuilder=weekBarItemWidgetBuilder;
this.calendarConfiguration.dayWidgetBuilder = dayWidgetBuilder;
this.calendarConfiguration.weekBarItemWidgetBuilder =
weekBarItemWidgetBuilder;
//lastClickDateModelitem
this.lastClickDateModel = selectDateModel != null
@ -149,8 +154,8 @@ class CalendarProvider extends ChangeNotifier {
MediaQueryData.fromWindow(WidgetsBinding.instance.window);
if (mediaQueryData.orientation == Orientation.landscape) {
calendarConfiguration.itemSize = (mediaQueryData.size.height -
calendarConfiguration.padding.horizontal -
calendarConfiguration.margin.horizontal) /
calendarConfiguration.padding.vertical -
calendarConfiguration.margin.vertical) /
7;
} else {
calendarConfiguration.itemSize = (mediaQueryData.size.width -
@ -171,8 +176,6 @@ class CalendarProvider extends ChangeNotifier {
calendarConfiguration.nowYear, calendarConfiguration.nowMonth);
totalHeight = calendarConfiguration.itemSize * (lineCount) +
calendarConfiguration.verticalSpacing * (lineCount - 1);
print(
"1111111:totalHeight:$totalHeight,lineCount:$lineCount,calendarConfiguration.nowYear:${calendarConfiguration.nowYear},calendarConfiguration.nowMonth:${calendarConfiguration.nowMonth}");
} else {
totalHeight = calendarConfiguration.itemSize;
}

@ -1,3 +1,5 @@
import 'dart:collection';
import 'package:flutter/material.dart';
import 'package:flutter_custom_calendar/controller.dart';
import 'package:flutter_custom_calendar/model/date_model.dart';
@ -34,7 +36,8 @@ class CalendarConfiguration {
int maxSelectMonth;
int maxSelectDay; //
Set<DateModel> defaultSelectedDateList = new Set(); //
DateModel selectDateModel; //item
HashSet<DateModel> defaultSelectedDateList; //set
int maxMultiSelectCount; //
Map<DateModel, Object> extraDataMap = new Map(); //
@ -70,6 +73,8 @@ class CalendarConfiguration {
List<DateModel> weekList = new List(); //list
PageController monthController; //controller
PageController weekController; //controller
DateModel minSelectDate;
DateModel maxSelectDate;
CalendarConfiguration(
{this.selectMode,
@ -86,6 +91,7 @@ class CalendarConfiguration {
this.maxSelectMonth,
this.maxSelectDay,
this.defaultSelectedDateList,
this.selectDateModel,
this.maxMultiSelectCount,
this.extraDataMap,
this.monthList,

@ -1,3 +1,5 @@
import 'dart:collection';
import 'package:flutter/material.dart';
import 'package:flutter_custom_calendar/calendar_provider.dart';
import 'package:flutter_custom_calendar/configuration.dart';
@ -45,8 +47,8 @@ class CalendarController {
int maxSelectYear = 2055,
int maxSelectMonth = 12,
int maxSelectDay = 30,
Set<DateTime> selectedDateTimeList = EMPTY_SET,
DateModel selectDateModel,
Set<DateTime> selectedDateTimeList = EMPTY_SET, //item
DateModel selectDateModel, //item
int maxMultiSelectCount = 9999,
Map<DateModel, Object> extraDataMap = EMPTY_MAP}) {
LogUtil.log(TAG: this.runtimeType, message: "init CalendarConfiguration");
@ -58,29 +60,41 @@ class CalendarController {
nowMonth = DateTime.now().month;
}
calendarConfiguration = CalendarConfiguration(
selectMode: selectMode,
showMode: showMode,
minYear: minYear,
maxYear: maxYear,
maxYearMonth: maxYearMonth,
nowYear: nowYear,
nowMonth: nowMonth,
minSelectYear: minSelectYear,
minSelectMonth: minSelectMonth,
minYearMonth: minYearMonth,
minSelectDay: minSelectDay,
maxSelectYear: maxSelectYear,
maxSelectMonth: maxSelectMonth,
extraDataMap: extraDataMap,
maxSelectDay: maxSelectDay,
);
if (selectedDateTimeList != null && selectedDateTimeList.isNotEmpty) {
calendarConfiguration.defaultSelectedDateList
.addAll(selectedDateTimeList.map((dateTime) {
return DateModel.fromDateTime(dateTime);
}).toSet());
}
selectMode: selectMode,
showMode: showMode,
minYear: minYear,
maxYear: maxYear,
maxYearMonth: maxYearMonth,
nowYear: nowYear,
nowMonth: nowMonth,
minSelectYear: minSelectYear,
minSelectMonth: minSelectMonth,
minYearMonth: minYearMonth,
minSelectDay: minSelectDay,
maxSelectYear: maxSelectYear,
maxSelectMonth: maxSelectMonth,
extraDataMap: extraDataMap,
maxSelectDay: maxSelectDay,
maxMultiSelectCount: maxMultiSelectCount,
selectDateModel: selectDateModel);
calendarConfiguration.defaultSelectedDateList = new HashSet<DateModel>();
calendarConfiguration.defaultSelectedDateList
.addAll(selectedDateTimeList.map((dateTime) {
return DateModel.fromDateTime(dateTime);
}).toSet());
//provider
calendarProvider.selectDateModel = selectDateModel;
calendarProvider.selectedDateList =
calendarConfiguration.defaultSelectedDateList;
calendarConfiguration.minSelectDate = DateModel.fromDateTime(DateTime(
calendarConfiguration.minSelectYear,
calendarConfiguration.minSelectMonth,
calendarConfiguration.minSelectDay));
calendarConfiguration.maxSelectDate = DateModel.fromDateTime(DateTime(
calendarConfiguration.maxSelectYear,
calendarConfiguration.maxSelectMonth,
calendarConfiguration.maxSelectDay));
LogUtil.log(
TAG: this.runtimeType,
@ -109,11 +123,6 @@ class CalendarController {
dateModel.year = i;
dateModel.month = j;
//
if (nowYear == -1 || nowMonth == -1) {
nowYear = DateTime.now().year;
nowMonth = DateTime.now().month;
}
if (i == nowYear && j == nowMonth) {
initialPage = nowMonthIndex;
}
@ -127,14 +136,13 @@ class CalendarController {
LogUtil.log(
TAG: this.runtimeType,
message:
"初始化月份视图的信息:一共有${monthList.length}个月initialPage为${nowMonthIndex}");
"初始化月份视图的信息:一共有${monthList.length}个月initialPage为$nowMonthIndex");
}
if (showMode != CalendarConstants.MODE_SHOW_ONLY_MONTH) {
//
///72
int initialWeekPage = 0;
int nowWeekIndex = 0;
weekList.clear();
//
if (nowYear == -1 || nowMonth == -1) {
@ -166,7 +174,7 @@ class CalendarController {
LogUtil.log(
TAG: this.runtimeType,
message:
"初始化星期视图的信息:一共有${weekList.length}个星期initialPage为${initialWeekPage}");
"初始化星期视图的信息:一共有${weekList.length}个星期initialPage为$initialWeekPage");
this.weekController = new PageController(initialPage: initialWeekPage);
}
@ -218,6 +226,19 @@ class CalendarController {
this.calendarProvider.generation.value++;
}
//item
void changeDefaultSelectedDateList(Set<DateModel> defaultSelectedDateList) {
this.calendarConfiguration.defaultSelectedDateList =
defaultSelectedDateList;
this.calendarProvider.generation.value++;
}
//item
void changeDefaultSelectedDateModel(DateModel dateModel) {
this.calendarProvider.selectDateModel = dateModel;
this.calendarProvider.generation.value++;
}
/**
*
*/

@ -28,6 +28,7 @@ class CalendarViewWidget extends StatefulWidget {
//item10
final double verticalSpacing;
//item
final DayWidgetBuilder dayWidgetBuilder;
final WeekBarItemWidgetBuilder weekBarItemWidgetBuilder;

@ -36,27 +36,11 @@ class _MonthViewState extends State<MonthView>
List<DateModel> items = List();
int lineCount;
// double itemHeight;
// double totalHeight;
// double mainSpacing = 10;
DateModel minSelectDate;
DateModel maxSelectDate;
Map<DateModel, Object> extraDataMap; //
@override
void initState() {
super.initState();
minSelectDate = DateModel.fromDateTime(DateTime(
widget.configuration.minSelectYear,
widget.configuration.minSelectMonth,
widget.configuration.minSelectDay));
maxSelectDate = DateModel.fromDateTime(DateTime(
widget.configuration.maxSelectYear,
widget.configuration.maxSelectMonth,
widget.configuration.maxSelectDay));
extraDataMap = widget.configuration.extraDataMap;
DateModel firstDayOfMonth =
@ -73,12 +57,12 @@ class _MonthViewState extends State<MonthView>
lineCount = DateUtil.getMonthViewLineCount(widget.year, widget.month);
//
//,generation
WidgetsBinding.instance.addPostFrameCallback((callback) {
Provider.of<CalendarProvider>(context, listen: false)
.generation
.addListener(() async {
extraDataMap={};
extraDataMap = widget.configuration.extraDataMap;
await getItems();
});
});
@ -88,8 +72,8 @@ class _MonthViewState extends State<MonthView>
items = await compute(initCalendarForMonthView, {
'year': widget.year,
'month': widget.month,
'minSelectDate': minSelectDate,
'maxSelectDate': maxSelectDate,
'minSelectDate': widget.configuration.minSelectDate,
'maxSelectDate': widget.configuration.maxSelectDate,
'extraDataMap': extraDataMap
});
setState(() {});
@ -139,7 +123,8 @@ class _MonthViewState extends State<MonthView>
return ItemContainer(
dateModel: dateModel,
key: ObjectKey(dateModel),//使objectKey1flutter2statefulElementstate
key: ObjectKey(
dateModel), //使objectKey1flutter2statefulElementstate
);
});
}
@ -175,6 +160,13 @@ class ItemContainerState extends State<ItemContainer> {
super.initState();
dateModel = widget.dateModel;
isSelected = ValueNotifier(dateModel.isSelected);
WidgetsBinding.instance.addPostFrameCallback((callback) {
if (configuration.selectMode == CalendarConstants.MODE_SINGLE_SELECT &&
dateModel.isSelected) {
calendarProvider.lastClickItemState = this;
}
});
}
/**
@ -220,16 +212,17 @@ class ItemContainerState extends State<ItemContainer> {
calendarProvider.lastClickDateModel = dateModel;
if (configuration.selectMode == CalendarConstants.MODE_MULTI_SELECT) {
//
if (calendarProvider.selectedDateList.length ==
configuration.maxMultiSelectCount) {
configuration.multiSelectOutOfSize();
return;
}
if (calendarProvider.selectedDateList.contains(dateModel)) {
calendarProvider.selectedDateList.remove(dateModel);
} else {
//
if (calendarProvider.selectedDateList.length ==
configuration.maxMultiSelectCount) {
if (configuration.multiSelectOutOfSize != null) {
configuration.multiSelectOutOfSize();
}
return;
}
calendarProvider.selectedDateList.add(dateModel);
}
configuration.calendarSelect(dateModel);
@ -241,8 +234,10 @@ class ItemContainerState extends State<ItemContainer> {
configuration.calendarSelect(dateModel);
//item
calendarProvider.lastClickItemState?.refreshItem();
calendarProvider.lastClickItemState = this;
if (calendarProvider.lastClickItemState != this) {
calendarProvider.lastClickItemState?.refreshItem();
calendarProvider.lastClickItemState = this;
}
}
refreshItem();

@ -29,29 +29,32 @@ class WeekView extends StatefulWidget {
class _WeekViewState extends State<WeekView> {
List<DateModel> items;
DateModel minSelectDate;
DateModel maxSelectDate;
Map<DateModel, Object> extraDataMap; //
@override
void initState() {
super.initState();
minSelectDate = DateModel.fromDateTime(DateTime(
widget.configuration.minSelectYear,
widget.configuration.minSelectMonth,
widget.configuration.minSelectDay));
maxSelectDate = DateModel.fromDateTime(DateTime(
widget.configuration.maxSelectYear,
widget.configuration.maxSelectMonth,
widget.configuration.maxSelectDay));
extraDataMap = widget.configuration.extraDataMap;
items = DateUtil.initCalendarForWeekView(
widget.year, widget.month, widget.firstDayOfWeek.getDateTime(), 0,
minSelectDate: minSelectDate,
maxSelectDate: maxSelectDate,
minSelectDate: widget.configuration.minSelectDate,
maxSelectDate: widget.configuration.maxSelectDate,
extraDataMap: extraDataMap);
//,generation
WidgetsBinding.instance.addPostFrameCallback((callback) {
Provider.of<CalendarProvider>(context, listen: false)
.generation
.addListener(() async {
items = DateUtil.initCalendarForWeekView(
widget.year, widget.month, widget.firstDayOfWeek.getDateTime(), 0,
minSelectDate: widget.configuration.minSelectDate,
maxSelectDate: widget.configuration.maxSelectDate,
extraDataMap: extraDataMap);
setState(() {});
});
});
}
@override

Loading…
Cancel
Save