From 7f006e6081480a76e0ada30bf23d02669649750c Mon Sep 17 00:00:00 2001 From: LXD312569496 <450468291@qq.com> Date: Wed, 9 Oct 2019 13:24:36 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=9B=E5=BB=BAconfiguration=E7=B1=BB?= =?UTF-8?q?=EF=BC=8C=E5=B0=86=E9=85=8D=E7=BD=AE=E7=9A=84=E4=BF=A1=E6=81=AF?= =?UTF-8?q?=E6=94=BE=E5=88=B0=E8=BF=99=E9=87=8C=20=E5=BC=95=E5=85=A5provid?= =?UTF-8?q?er=E7=8A=B6=E6=80=81=E7=AE=A1=E7=90=86,=E9=81=BF=E5=85=8D?= =?UTF-8?q?=E6=B7=B1=E5=B1=82=E5=B5=8C=E5=A5=97=E4=BC=A0=E9=80=92=E4=BF=A1?= =?UTF-8?q?=E6=81=AF=20=E5=91=A8=E8=A7=86=E5=9B=BE=E5=92=8C=E6=9C=88?= =?UTF-8?q?=E8=A7=86=E5=9B=BE=EF=BC=8C=E8=81=94=E5=8A=A8=20=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0=E6=97=A5=E5=BF=97=E8=BE=93=E5=87=BA=E7=B1=BBLogUtil?= =?UTF-8?q?=EF=BC=8C=E6=96=B9=E4=BE=BF=E6=9F=A5=E7=9C=8B=E8=B0=83=E8=AF=95?= =?UTF-8?q?=20=E5=A2=9E=E5=8A=A0example=E4=BE=8B=E5=AD=90=20=E6=80=A7?= =?UTF-8?q?=E8=83=BD=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 4 +++- example/lib/custom_sign_page.dart | 6 ++++++ example/lib/custom_style_page.dart | 6 ++++++ example/lib/default_style_page.dart | 5 ++--- lib/cache_data.dart | 8 ++++++++ lib/calendar_provider.dart | 10 +++------- lib/configuration.dart | 11 ++--------- lib/constants/constants.dart | 8 +++++--- lib/controller.dart | 18 +++++++++++++----- lib/widget/calendar_view.dart | 27 +++++++++++++++++++++------ lib/widget/month_view_pager.dart | 19 ++++++++++++------- 11 files changed, 81 insertions(+), 41 deletions(-) diff --git a/README.md b/README.md index f4e9fb0..2add2a6 100644 --- a/README.md +++ b/README.md @@ -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 | 选择模式,表示单选或者多选 | 默认是单选
static const int MODE_SINGLE_SELECT = 1;
static const int MODE_MULTI_SELECT = 2; +showMode|展示模式| 默认是只展示月视图
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;//支持月和星期视图切换 minYear | 日历显示的最小年份| 1971 maxYear | 日历显示的最大年份| 2055 minYearMonth | 日历显示的最小年份的月份| 1 diff --git a/example/lib/custom_sign_page.dart b/example/lib/custom_sign_page.dart index 4f6717d..4ea59cb 100644 --- a/example/lib/custom_sign_page.dart +++ b/example/lib/custom_sign_page.dart @@ -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: diff --git a/example/lib/custom_style_page.dart b/example/lib/custom_style_page.dart index fe5d9ad..711c058 100644 --- a/example/lib/custom_style_page.dart +++ b/example/lib/custom_style_page.dart @@ -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 diff --git a/example/lib/default_style_page.dart b/example/lib/default_style_page.dart index 595fd0b..455782f 100644 --- a/example/lib/default_style_page.dart +++ b/example/lib/default_style_page.dart @@ -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 { 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) { diff --git a/lib/cache_data.dart b/lib/cache_data.dart index eb48796..5362177 100644 --- a/lib/cache_data.dart +++ b/lib/cache_data.dart @@ -20,6 +20,14 @@ class CacheData { } return _instance; } + + + void clearData(){ + monthListCache.clear(); + weekListCache.clear(); + } + + } diff --git a/lib/calendar_provider.dart b/lib/calendar_provider.dart index 5ee04f2..9ad1a00 100644 --- a/lib/calendar_provider.dart +++ b/lib/calendar_provider.dart @@ -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; } } diff --git a/lib/configuration.dart b/lib/configuration.dart index e2ca071..eeca2fc 100644 --- a/lib/configuration.dart +++ b/lib/configuration.dart @@ -12,9 +12,6 @@ class CalendarConfiguration { //仅展示月视图,仅展示周视图,支持月视图和周视图切换 int showMode; - bool defaultExpandStatus; //展开状态,true代表是月视图,false代表是周视图 - bool enableExpand; //是否可以展开,true只展示月视图,false仅展示周视图, - //日历显示的最小年份和最大年份 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}'; } } diff --git a/lib/constants/constants.dart b/lib/constants/constants.dart index 9af2d56..4042090 100644 --- a/lib/constants/constants.dart +++ b/lib/constants/constants.dart @@ -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;//支持月和星期视图切换 + /** * 一周七天 diff --git a/lib/controller.dart b/lib/controller.dart index 4ae9fab..fc4a984 100644 --- a/lib/controller.dart +++ b/lib/controller.dart @@ -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 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(); + } + } /** diff --git a/lib/widget/calendar_view.dart b/lib/widget/calendar_view.dart index 14f1716..605340c 100644 --- a/lib/widget/calendar_view.dart +++ b/lib/widget/calendar_view.dart @@ -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 { super.initState(); } + @override + void dispose() { + widget.calendarController.clearData(); + super.dispose(); + } + @override Widget build(BuildContext context) { return ChangeNotifierProvider.value( @@ -70,7 +77,7 @@ class CalendarContainerState extends State var state = CrossFadeState.showFirst; - List widgets; + List widgets = []; int index = 0; @@ -79,13 +86,21 @@ class CalendarContainerState extends State calendarProvider = Provider.of(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; diff --git a/lib/widget/month_view_pager.dart b/lib/widget/month_view_pager.dart index e6546e2..ca47e92 100644 --- a/lib/widget/month_view_pager.dart +++ b/lib/widget/month_view_pager.dart @@ -50,8 +50,6 @@ class _MonthViewPagerState extends State { // WidgetsBinding.instance.addPostFrameCallback((timeStamp) { // calendarProvider.calendarConfiguration.monthController.jumpToPage(index); // }); - - } @override @@ -69,15 +67,22 @@ class _MonthViewPagerState extends State { 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;