增加周视图支持,week_view_pager,先提交一部分代码

develop
xiaodong 5 years ago
parent e7b3f73d76
commit e9f98bcd99

@ -4,6 +4,10 @@ class Constants {
static const int MODE_SINGLE_SELECT = 1; static const int MODE_SINGLE_SELECT = 1;
static const int MODE_MULTI_SELECT = 2; static const int MODE_MULTI_SELECT = 2;
//
static const int STATUS_EXPAND=1;//
static const int STATUS_SHRINK=2;//
/** /**
* *
*/ */

@ -16,6 +16,10 @@ class CalendarController {
//,MODE_SINGLE_SELECTMODE_MULTI_SELECT //,MODE_SINGLE_SELECTMODE_MULTI_SELECT
int selectMode; int selectMode;
//
bool expandStatus;
ValueNotifier<bool> expandChanged;
// //
int minYear; int minYear;
int maxYear; int maxYear;
@ -56,10 +60,12 @@ class CalendarController {
* *
*/ */
List<DateModel> monthList = new List(); //list List<DateModel> monthList = new List(); //list
List<DateModel> weekList=new List();//list
PageController pageController; PageController pageController;
CalendarController( CalendarController(
{int selectMode = Constants.MODE_SINGLE_SELECT, {int selectMode = Constants.MODE_SINGLE_SELECT,
bool expandStatus = true,
DayWidgetBuilder dayWidgetBuilder = defaultCustomDayWidget, DayWidgetBuilder dayWidgetBuilder = defaultCustomDayWidget,
WeekBarItemWidgetBuilder weekBarItemWidgetBuilder = defaultWeekBarWidget, WeekBarItemWidgetBuilder weekBarItemWidgetBuilder = defaultWeekBarWidget,
int minYear = 1971, int minYear = 1971,
@ -96,6 +102,8 @@ class CalendarController {
this.weekBarItemWidgetBuilder = weekBarItemWidgetBuilder; this.weekBarItemWidgetBuilder = weekBarItemWidgetBuilder;
this.maxMultiSelectCount = maxMultiSelectCount; this.maxMultiSelectCount = maxMultiSelectCount;
this.extraDataMap = extraDataMap; this.extraDataMap = extraDataMap;
this.expandStatus = expandStatus;
this.expandChanged=ValueNotifier(expandStatus);
this.selectedDateList = Set(); this.selectedDateList = Set();
if (selectedDateTimeList != null && selectedDateTimeList.isNotEmpty) { if (selectedDateTimeList != null && selectedDateTimeList.isNotEmpty) {
@ -242,6 +250,17 @@ class CalendarController {
} }
return selectDateModel; return selectDateModel;
} }
//
void toggleExpandStatus(){
expandChanged.value=!expandChanged.value;
print("toggleExpandStatus${expandChanged.value}");
}
void addExpandChangeListener(){
}
} }
/** /**

@ -26,7 +26,8 @@ class DateModel {
bool isInRange = false; //, bool isInRange = false; //,
bool isSelected; // bool isSelected; //
bool isCanClick = true; //todo:truefalse bool isCanClick =
true; //todo:truefalse
@override @override
String toString() { String toString() {
@ -59,4 +60,15 @@ class DateModel {
@override @override
int get hashCode => year.hashCode ^ month.hashCode ^ day.hashCode; int get hashCode => year.hashCode ^ month.hashCode ^ day.hashCode;
//
bool isSameWith(DateModel dateModel) {
return year == dateModel.year &&
month == dateModel.month &&
day == dateModel.day;
}
} }

@ -172,4 +172,30 @@ class DateUtil {
print("$year$month月:有${((preIndex + monthDayCount) / 7).toInt() + 1}"); print("$year$month月:有${((preIndex + monthDayCount) / 7).toInt() + 1}");
return ((preIndex + monthDayCount) / 7).toInt() + 1; return ((preIndex + monthDayCount) / 7).toInt() + 1;
} }
/**
* 7item
*/
static List<DateModel> initCalendarForWeekView(
int year, int month, DateTime currentDate, int weekStart,
{DateModel minSelectDate,
DateModel maxSelectDate,
Map<DateTime, Object> extraDataMap}) {
List<DateModel> items = List();
int weekDay = currentDate.weekday;
//
DateTime firstDayOfWeek = currentDate.add(Duration(days: -weekDay));
for (int i = 1; i <= 7; i++) {
DateModel dateModel =
DateModel.fromDateTime(firstDayOfWeek.add(Duration(days: i)));
items.add(dateModel);
}
print("items.toString():${items.toString()}");
return items;
}
} }

@ -3,7 +3,7 @@ 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';
/** /**
* *
@ -21,12 +21,27 @@ class CalendarViewWidget extends StatefulWidget {
_CalendarViewWidgetState createState() => _CalendarViewWidgetState(); _CalendarViewWidgetState createState() => _CalendarViewWidgetState();
} }
class _CalendarViewWidgetState extends State<CalendarViewWidget> { class _CalendarViewWidgetState extends State<CalendarViewWidget>
with SingleTickerProviderStateMixin {
double itemHeight; double itemHeight;
double totalHeight; double totalHeight;
bool expand = true;
AnimationController _animationController;
@override @override
void initState() {} void initState() {
_animationController =
AnimationController(duration: Duration(milliseconds: 200), vsync: this);
widget.calendarController.expandChanged.addListener(() {
print("_CalendarViewWidgetState:$expand");
setState(() {
expand = !expand;
});
});
}
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
@ -42,38 +57,82 @@ class _CalendarViewWidgetState extends State<CalendarViewWidget> {
* constsetStateview * constsetStateview
*/ */
widget.calendarController.weekBarItemWidgetBuilder(), widget.calendarController.weekBarItemWidgetBuilder(),
Container( AnimatedContainer(
height: totalHeight, duration: Duration(milliseconds: 500),
child: MonthViewPager( height: expand ? totalHeight : itemHeight,
selectMode: widget.calendarController.selectMode, child: expand
monthChange: (int year, int month) { ? Container(
widget.calendarController.monthChange(year, month); height: totalHeight,
}, child: MonthViewPager(
calendarSelect: (dateModel) { selectMode: widget.calendarController.selectMode,
widget.calendarController.selectDateModel =dateModel; monthChange: (int year, int month) {
widget.calendarController.calendarSelect(dateModel); widget.calendarController.monthChange(year, month);
}, },
monthList: widget.calendarController.monthList, calendarSelect: (dateModel) {
pageController: widget.calendarController.pageController, widget.calendarController.selectDateModel = dateModel;
selectedDateList: widget.calendarController.selectedDateList, widget.calendarController.calendarSelect(dateModel);
selectDateModel: widget.calendarController.selectDateModel, },
dayWidgetBuilder: widget.calendarController.dayWidgetBuilder, monthList: widget.calendarController.monthList,
minSelectDate: DateModel() pageController: widget.calendarController.pageController,
..year = widget.calendarController.minSelectYear selectedDateList:
..month = widget.calendarController.minSelectMonth widget.calendarController.selectedDateList,
..day = widget.calendarController.minSelectDay, selectDateModel:
maxSelectDate: DateModel() widget.calendarController.selectDateModel,
..year = widget.calendarController.maxSelectYear dayWidgetBuilder:
..month = widget.calendarController.maxSelectMonth widget.calendarController.dayWidgetBuilder,
..day = widget.calendarController.maxSelectDay, minSelectDate: DateModel()
maxMultiSelectCount: ..year = widget.calendarController.minSelectYear
widget.calendarController.maxMultiSelectCount, ..month = widget.calendarController.minSelectMonth
multiSelectOutOfRange: ..day = widget.calendarController.minSelectDay,
widget.calendarController.multiSelectOutOfRange, maxSelectDate: DateModel()
multiSelectOutOfSize: ..year = widget.calendarController.maxSelectYear
widget.calendarController.multiSelectOutOfSize, ..month = widget.calendarController.maxSelectMonth
extraDataMap: widget.calendarController.extraDataMap, ..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,31 +1,132 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.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/utils/date_util.dart'; import 'package:flutter_custom_calendar/utils/date_util.dart';
/** /**
* *
*/ */
class WeekView extends StatefulWidget { class WeekView extends StatefulWidget {
OnCalendarSelect onCalendarSelectListener;
Set<DateModel> selectedDateList; //
DateModel selectDateModel; //,
DayWidgetBuilder dayWidgetBuilder;
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.month,
this.day,
this.onCalendarSelectListener,
this.dayWidgetBuilder,
this.selectedDateList,
this.selectDateModel,
this.minSelectDate,
this.maxSelectDate,
this.selectMode,
this.multiSelectOutOfSize,
this.multiSelectOutOfRange,
this.maxMultiSelectCount,
this.extraDataMap});
@override @override
_WeekViewState createState() => _WeekViewState(); _WeekViewState createState() => _WeekViewState();
} }
class _WeekViewState extends State<WeekView> { class _WeekViewState extends State<WeekView> {
List<DateModel> items;
DateModel selectDateModel; //,
@override
void initState() {
super.initState();
items=DateUtil.initCalendarForWeekView(2019, 9, DateTime.now(), 0);
}
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
// DateTime dateTime = DateTime.now();
//
// print(dateTime); return new GridView.builder(
// physics: NeverScrollableScrollPhysics(),
// print(dateTime.subtract(Duration(days: 1))); gridDelegate: new SliverGridDelegateWithFixedCrossAxisCount(
// crossAxisCount: 7, mainAxisSpacing: 10),
// var berlinWallFell = new DateTime(1989, DateTime.november, 9); itemCount: 7,
// var dDay = new DateTime(1944, DateTime.june, 6); itemBuilder: (context, index) {
// Duration difference = berlinWallFell.difference(dDay); DateModel dateModel = items[index];
// print(difference.inDays); //
if (widget.selectMode == Constants.MODE_MULTI_SELECT) {
print(DateUtil.initCalendarForMonthView( if (widget.selectedDateList.contains(dateModel)) {
2019, 12, DateTime.now(), DateTime.sunday)); dateModel.isSelected = true;
} else {
return Container(); dateModel.isSelected = false;
}
} else {
if (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) {
//
if (widget.selectedDateList.length ==
widget.maxMultiSelectCount) {
widget.multiSelectOutOfSize();
return;
}
//
selectDateModel = dateModel;
widget.selectDateModel = dateModel;
widget.onCalendarSelectListener(dateModel);
setState(() {
if (widget.selectedDateList.contains(dateModel)) {
widget.selectedDateList.remove(dateModel);
} else {
widget.selectedDateList.add(dateModel);
}
});
} else {
selectDateModel = dateModel;
widget.selectDateModel = dateModel;
widget.onCalendarSelectListener(dateModel);
setState(() {});
}
},
child: widget.dayWidgetBuilder(dateModel),
);
});
} }
} }

@ -0,0 +1,89 @@
import 'package:flutter/material.dart';
import 'package:flutter_custom_calendar/controller.dart';
import 'package:flutter_custom_calendar/model/date_model.dart';
import 'package:flutter_custom_calendar/widget/week_view.dart';
class WeekViewPager extends StatefulWidget {
final OnMonthChange monthChange;
final OnCalendarSelect calendarSelect;
final DayWidgetBuilder dayWidgetBuilder;
OnMultiSelectOutOfRange multiSelectOutOfRange; //
OnMultiSelectOutOfSize multiSelectOutOfSize; //
Set<DateModel> selectedDateList; //,
DateModel selectDateModel; //,
final List<DateModel> weekList;
PageController pageController;
DateModel minSelectDate;
DateModel maxSelectDate;
int selectMode;
int maxMultiSelectCount;
Map<DateTime, Object> extraDataMap; //
WeekViewPager(
{this.monthChange,
this.calendarSelect,
this.weekList,
this.pageController,
this.selectedDateList,
this.selectDateModel,
this.dayWidgetBuilder,
this.minSelectDate,
this.maxSelectDate,
this.selectMode,
this.maxMultiSelectCount,
this.multiSelectOutOfRange,
this.multiSelectOutOfSize,
this.extraDataMap});
@override
_WeekViewPagerState createState() => _WeekViewPagerState();
}
class _WeekViewPagerState extends State<WeekViewPager> {
int lastMonth;//
@override
void initState() {
// lastMonth=
}
@override
Widget build(BuildContext context) {
return Container(
child: PageView.builder(
onPageChanged: (position) {
//
DateModel dateModel = widget.weekList[position];
widget.monthChange(dateModel.year, dateModel.month);
},
controller: widget.pageController,
itemBuilder: (context, index) {
DateModel dateModel = widget.weekList[index];
return new WeekView(
selectMode: widget.selectMode,
year: dateModel.year,
month: dateModel.month,
selectDateModel: widget.selectDateModel,
selectedDateList: widget.selectedDateList,
onCalendarSelectListener: widget.calendarSelect,
dayWidgetBuilder: widget.dayWidgetBuilder,
minSelectDate: widget.minSelectDate,
maxSelectDate: widget.maxSelectDate,
maxMultiSelectCount: widget.maxMultiSelectCount,
multiSelectOutOfRange: widget.multiSelectOutOfRange,
multiSelectOutOfSize: widget.multiSelectOutOfSize,
extraDataMap: widget.extraDataMap,
);
},
itemCount: widget.weekList.length,
),
);
}
}
Loading…
Cancel
Save