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

引入provider状态管理,避免深层嵌套传递信息
周视图和月视图,联动
增加日志输出类LogUtil,方便查看调试
增加example例子
性能优化
develop
LXD312569496 5 years ago committed by xiaodong
parent 8ff63215f4
commit 7f006e6081

@ -68,6 +68,7 @@ CalendarViewWidget({@required this.calendarController, this.boxDecoration});
//构造函数
CalendarController(
{int selectMode = Constants.MODE_SINGLE_SELECT,
int showMode = Constants.MODE_SHOW_ONLY_MONTH,
bool expandStatus = true,
DayWidgetBuilder dayWidgetBuilder = defaultCustomDayWidget,
WeekBarItemWidgetBuilder weekBarItemWidgetBuilder = defaultWeekBarWidget,
@ -96,7 +97,8 @@ CalendarViewWidget({@required this.calendarController, this.boxDecoration});
属性 | 含义 | 默认值
:-: | :-: | :-:
selectMode | 选择模式,表示单选或者多选 | 默认是单选MODE_SINGLE_SELECT
selectMode | 选择模式,表示单选或者多选 | 默认是单选<br>static const int MODE_SINGLE_SELECT = 1;<br>static const int MODE_MULTI_SELECT = 2;
showMode|展示模式| 默认是只展示月视图<br>static const int MODE_SHOW_ONLY_MONTH=1;//仅支持月视图<br>static const int MODE_SHOW_ONLY_WEEK=2;//仅支持星期视图<br>static const int MODE_SHOW_WEEK_AND_MONTH=3;//支持月和星期视图切换
minYear | 日历显示的最小年份| 1971
maxYear | 日历显示的最大年份| 2055
minYearMonth | 日历显示的最小年份的月份| 1

@ -139,6 +139,9 @@ class CustomStyleDayWidget extends BaseCombineDayWidget {
@override
Widget getNormalWidget(DateModel dateModel) {
if (!dateModel.isCurrentMonth) {
return Container();
}
return Container(
margin: EdgeInsets.all(8),
child: new Stack(
@ -186,6 +189,9 @@ class CustomStyleDayWidget extends BaseCombineDayWidget {
@override
Widget getSelectedWidget(DateModel dateModel) {
if (!dateModel.isCurrentMonth) {
return Container();
}
return Container(
margin: EdgeInsets.all(8),
foregroundDecoration:

@ -112,6 +112,9 @@ class CustomStyleDayWidget extends BaseCustomDayWidget {
@override
void drawNormal(DateModel dateModel, Canvas canvas, Size size) {
if (!dateModel.isCurrentMonth) {
return;
}
bool isWeekend = dateModel.isWeekend;
bool isInRange = dateModel.isInRange;
@ -148,6 +151,9 @@ class CustomStyleDayWidget extends BaseCustomDayWidget {
@override
void drawSelected(DateModel dateModel, Canvas canvas, Size size) {
if (!dateModel.isCurrentMonth) {
return;
}
//
Paint backGroundPaint = new Paint()
..color = Colors.blue

@ -1,7 +1,5 @@
import 'package:example/main.dart';
import 'package:flutter/material.dart';
import 'package:flutter_custom_calendar/flutter_custom_calendar.dart';
import 'package:provider/provider.dart';
/**
* +
@ -28,7 +26,8 @@ class _DefaultStylePageState extends State<DefaultStylePage> {
minYear: now.year,
minYearMonth: now.month - 2,
maxYear: now.year,
maxYearMonth: now.month + 1);
maxYearMonth: now.month + 1,
showMode: Constants.MODE_SHOW_WEEK_AND_MONTH);
controller.addMonthChangeListener(
(year, month) {

@ -20,6 +20,14 @@ class CacheData {
}
return _instance;
}
void clearData(){
monthListCache.clear();
weekListCache.clear();
}
}

@ -1,4 +1,5 @@
import 'package:flutter/material.dart';
import 'package:flutter_custom_calendar/cache_data.dart';
import 'package:flutter_custom_calendar/configuration.dart';
import 'package:flutter_custom_calendar/model/date_model.dart';
import 'package:flutter_custom_calendar/utils/LogUtil.dart';
@ -97,19 +98,14 @@ class CalendarProvider extends ChangeNotifier {
? selectDateModel
: DateModel.fromDateTime(DateTime.now())
..day = 15;
// expandStatus.addListener(() {
// print("1111");
// if (expandStatus.value == false) {
// calendarConfiguration.weekController.jumpToPage(weekPageIndex);
// }
// });
}
//退
void clearData() {
LogUtil.log(TAG: this.runtimeType, message: "CalendarProvider clearData");
CacheData.instance.clearData();
selectedDateList.clear();
selectDateModel = null;
calendarConfiguration = null;
}
}

@ -12,9 +12,6 @@ class CalendarConfiguration {
//
int showMode;
bool defaultExpandStatus; //,truefalse
bool enableExpand; //,truefalse
//
int minYear;
int maxYear;
@ -90,14 +87,10 @@ class CalendarConfiguration {
this.monthController,
this.weekController,
this.verticalSpacing,
this.itemSize,
this.enableExpand,
bool defaultExpandStatus}) {
this.defaultExpandStatus = defaultExpandStatus;
}
this.itemSize,this.showMode});
@override
String toString() {
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, monthController: $monthController, weekController: $weekController}';
return 'CalendarConfiguration{selectMode: $selectMode, 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, monthController: $monthController, weekController: $weekController}';
}
}

@ -4,9 +4,11 @@ class Constants {
static const int MODE_SINGLE_SELECT = 1;
static const int MODE_MULTI_SELECT = 2;
//
static const int STATUS_EXPAND=1;//
static const int STATUS_SHRINK=2;//
//
static const int MODE_SHOW_ONLY_MONTH=1;//
static const int MODE_SHOW_ONLY_WEEK=2;//
static const int MODE_SHOW_WEEK_AND_MONTH=3;//
/**
*

@ -32,7 +32,7 @@ class CalendarController {
CalendarController(
{int selectMode = Constants.MODE_SINGLE_SELECT,
bool expandStatus = true,
int showMode = Constants.MODE_SHOW_ONLY_MONTH,
DayWidgetBuilder dayWidgetBuilder = defaultCustomDayWidget,
WeekBarItemWidgetBuilder weekBarItemWidgetBuilder = defaultWeekBarWidget,
int minYear = 1971,
@ -51,11 +51,12 @@ class CalendarController {
DateModel selectDateModel,
int maxMultiSelectCount = 9999,
double verticalSpacing = 10,
bool enableExpand = true,
double itemSize,
Map<DateModel, Object> extraDataMap = EMPTY_MAP}) {
LogUtil.log(TAG: this.runtimeType, message: "init CalendarConfiguration");
calendarConfiguration = CalendarConfiguration(
selectMode: selectMode,
showMode: showMode,
minYear: minYear,
maxYear: maxYear,
maxYearMonth: maxYearMonth,
@ -67,11 +68,10 @@ class CalendarController {
minSelectDay: minSelectDay,
maxSelectYear: maxSelectYear,
maxSelectMonth: maxSelectMonth,
defaultExpandStatus: expandStatus,
extraDataMap: extraDataMap,
maxSelectDay: maxSelectDay,
enableExpand: enableExpand,
verticalSpacing: verticalSpacing);
verticalSpacing: verticalSpacing,
itemSize: itemSize);
calendarConfiguration.dayWidgetBuilder = dayWidgetBuilder;
calendarConfiguration.weekBarItemWidgetBuilder = weekBarItemWidgetBuilder;
@ -440,6 +440,14 @@ class CalendarController {
DateModel getSingleSelectCalendar() {
return calendarProvider.selectDateModel;
}
//
void clearData(){
monthList.clear();
weekList.clear();
calendarProvider.clearData();
}
}
/**

@ -1,6 +1,7 @@
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_custom_calendar/calendar_provider.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/LogUtil.dart';
@ -39,6 +40,12 @@ class _CalendarViewWidgetState extends State<CalendarViewWidget> {
super.initState();
}
@override
void dispose() {
widget.calendarController.clearData();
super.dispose();
}
@override
Widget build(BuildContext context) {
return ChangeNotifierProvider<CalendarProvider>.value(
@ -70,7 +77,7 @@ class CalendarContainerState extends State<CalendarContainer>
var state = CrossFadeState.showFirst;
List<Widget> widgets;
List<Widget> widgets = [];
int index = 0;
@ -79,13 +86,21 @@ class CalendarContainerState extends State<CalendarContainer>
calendarProvider = Provider.of<CalendarProvider>(context, listen: false);
expand = calendarProvider.expandStatus.value;
widgets = [
const MonthViewPager(),
const WeekViewPager(),
];
if (calendarProvider.calendarConfiguration.showMode ==
Constants.MODE_SHOW_ONLY_WEEK) {
widgets.add(const WeekViewPager());
} else if (calendarProvider.calendarConfiguration.showMode ==
Constants.MODE_SHOW_WEEK_AND_MONTH) {
widgets.add(const MonthViewPager());
widgets.add(const WeekViewPager());
} else {
//
widgets.add(const MonthViewPager());
}
//
if (calendarProvider.calendarConfiguration.enableExpand == true) {
if (calendarProvider.calendarConfiguration.showMode ==
Constants.MODE_SHOW_WEEK_AND_MONTH) {
calendarProvider.expandStatus.addListener(() {
setState(() {
expand = calendarProvider.expandStatus.value;

@ -50,8 +50,6 @@ class _MonthViewPagerState extends State<MonthViewPager> {
// WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
// calendarProvider.calendarConfiguration.monthController.jumpToPage(index);
// });
}
@override
@ -69,15 +67,22 @@ class _MonthViewPagerState extends State<MonthViewPager> {
return PageView.builder(
onPageChanged: (position) {
if (calendarProvider.expandStatus.value == false) {
return;
}
//
DateModel dateModel = configuration.monthList[position];
configuration.monthChange(dateModel.year, dateModel.month);
//
DateModel temp = new DateModel();
temp.year = configuration.monthList[position].year;
temp.month = configuration.monthList[position].month;
temp.day = configuration.monthList[position].day + 14;
calendarProvider.lastClickDateModel = temp;
if (calendarProvider.lastClickDateModel != null ||
calendarProvider.lastClickDateModel.month != dateModel.month) {
DateModel temp = new DateModel();
temp.year = configuration.monthList[position].year;
temp.month = configuration.monthList[position].month;
temp.day = configuration.monthList[position].day + 14;
calendarProvider.lastClickDateModel = temp;
}
//使PageView
// double itemHeight = MediaQuery.of(context).size.width / 7;

Loading…
Cancel
Save