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

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

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

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

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

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

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

@ -29,29 +29,32 @@ class WeekView extends StatefulWidget {
class _WeekViewState extends State<WeekView> { class _WeekViewState extends State<WeekView> {
List<DateModel> items; List<DateModel> items;
DateModel minSelectDate;
DateModel maxSelectDate;
Map<DateModel, Object> extraDataMap; // Map<DateModel, Object> extraDataMap; //
@override @override
void initState() { void initState() {
super.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; extraDataMap = widget.configuration.extraDataMap;
items = DateUtil.initCalendarForWeekView( items = DateUtil.initCalendarForWeekView(
widget.year, widget.month, widget.firstDayOfWeek.getDateTime(), 0, widget.year, widget.month, widget.firstDayOfWeek.getDateTime(), 0,
minSelectDate: minSelectDate, minSelectDate: widget.configuration.minSelectDate,
maxSelectDate: maxSelectDate, maxSelectDate: widget.configuration.maxSelectDate,
extraDataMap: extraDataMap); 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 @override

Loading…
Cancel
Save