diff --git a/example/lib/custom_sign_page.dart b/example/lib/custom_sign_page.dart index 642a87e..4f6717d 100644 --- a/example/lib/custom_sign_page.dart +++ b/example/lib/custom_sign_page.dart @@ -16,7 +16,8 @@ class CustomSignPage extends StatefulWidget { } class _CustomSignPageState extends State { - String text; + ValueNotifier text; + ValueNotifier selectText; CalendarController controller; @@ -42,8 +43,6 @@ class _CustomSignPageState extends State { @override void initState() { - text = "${DateTime.now().year}年${DateTime.now().month}月"; - controller = new CalendarController( weekBarItemWidgetBuilder: () { return CustomStyleWeekBarItem(); @@ -55,16 +54,20 @@ class _CustomSignPageState extends State { controller.addMonthChangeListener( (year, month) { - setState(() { - text = "$year年$month月"; - }); + text.value = "$year年$month月"; }, ); controller.addOnCalendarSelectListener((dateModel) { //刷新选择的时间 - setState(() {}); + selectText.value = + "单选模式\n选中的时间:\n${controller.getSingleSelectCalendar()}"; }); + + text = new ValueNotifier("${DateTime.now().year}年${DateTime.now().month}月"); + + selectText = new ValueNotifier( + "单选模式\n选中的时间:\n${controller.getSingleSelectCalendar()}"); } @override @@ -84,7 +87,11 @@ class _CustomSignPageState extends State { onPressed: () { controller.moveToPreviousMonth(); }), - new Text(text), + ValueListenableBuilder( + valueListenable: text, + builder: (context, value, child) { + return new Text(text.value); + }), new IconButton( icon: Icon(Icons.navigate_next), onPressed: () { @@ -95,8 +102,11 @@ class _CustomSignPageState extends State { CalendarViewWidget( calendarController: controller, ), - new Text( - "自定义创建Item\n选中的时间:\n${controller.getSingleSelectCalendar().toString()}"), + ValueListenableBuilder( + valueListenable: selectText, + builder: (context, value, child) { + return new Text(selectText.value); + }), ], ), ), @@ -210,13 +220,13 @@ class CustomStyleDayWidget extends BaseCombineDayWidget { ), dateModel.extraData != null ? Positioned( - child: Text( - "${dateModel.extraData}", - style: TextStyle(fontSize: 10, color: RandomColor.next()), - ), - right: 0, - top: 0, - ) + child: Text( + "${dateModel.extraData}", + style: TextStyle(fontSize: 10, color: RandomColor.next()), + ), + right: 0, + top: 0, + ) : Container() ], ), diff --git a/example/lib/custom_style_page.dart b/example/lib/custom_style_page.dart index 4c24be8..fe5d9ad 100644 --- a/example/lib/custom_style_page.dart +++ b/example/lib/custom_style_page.dart @@ -1,7 +1,9 @@ import 'package:flutter/material.dart'; import 'package:flutter_custom_calendar/flutter_custom_calendar.dart'; - +/** + * 自定义风格+单选 + */ class CustomStylePage extends StatefulWidget { CustomStylePage({Key key, this.title}) : super(key: key); @@ -12,14 +14,13 @@ class CustomStylePage extends StatefulWidget { } class _CustomStylePageState extends State { - String text; + ValueNotifier text; + ValueNotifier selectText; CalendarController controller; @override void initState() { - text = "${DateTime.now().year}年${DateTime.now().month}月"; - controller = new CalendarController(weekBarItemWidgetBuilder: () { return CustomStyleWeekBarItem(); }, dayWidgetBuilder: (dateModel) { @@ -28,16 +29,20 @@ class _CustomStylePageState extends State { controller.addMonthChangeListener( (year, month) { - setState(() { - text = "$year年$month月"; - }); + text.value = "$year年$month月"; }, ); controller.addOnCalendarSelectListener((dateModel) { //刷新选择的时间 - setState(() {}); + selectText.value = + "单选模式\n选中的时间:\n${controller.getSingleSelectCalendar()}"; }); + + text = new ValueNotifier("${DateTime.now().year}年${DateTime.now().month}月"); + + selectText = new ValueNotifier( + "单选模式\n选中的时间:\n${controller.getSingleSelectCalendar()}"); } @override @@ -57,7 +62,11 @@ class _CustomStylePageState extends State { onPressed: () { controller.moveToPreviousMonth(); }), - new Text(text), + ValueListenableBuilder( + valueListenable: text, + builder: (context, value, child) { + return new Text(text.value); + }), new IconButton( icon: Icon(Icons.navigate_next), onPressed: () { @@ -68,8 +77,11 @@ class _CustomStylePageState extends State { CalendarViewWidget( calendarController: controller, ), - new Text( - "自定义创建Item\n选中的时间:\n${controller.getSingleSelectCalendar().toString()}"), + ValueListenableBuilder( + valueListenable: selectText, + builder: (context, value, child) { + return new Text(selectText.value); + }), ], ), ), diff --git a/example/lib/default_style_page.dart b/example/lib/default_style_page.dart index 9ac4a99..9b84822 100644 --- a/example/lib/default_style_page.dart +++ b/example/lib/default_style_page.dart @@ -1,7 +1,11 @@ import 'package:example/main.dart'; import 'package:flutter/material.dart'; import 'package:flutter_custom_calendar/flutter_custom_calendar.dart'; +import 'package:provider/provider.dart'; +/** + * 默认风格+单选 + */ class DefaultStylePage extends StatefulWidget { DefaultStylePage({Key key, this.title}) : super(key: key); @@ -12,29 +16,32 @@ class DefaultStylePage extends StatefulWidget { } class _DefaultStylePageState extends State { - String text; + ValueNotifier text; + ValueNotifier selectText; CalendarController controller; @override void initState() { - text = "${DateTime.now().year}年${DateTime.now().month}月"; - controller = new CalendarController( minYear: 2019, minYearMonth: 8, maxYear: 2019, maxYearMonth: 9); controller.addMonthChangeListener( (year, month) { - setState(() { - text = "$year年$month月"; - }); + text.value = "$year年$month月"; }, ); controller.addOnCalendarSelectListener((dateModel) { //刷新选择的时间 - setState(() {}); + selectText.value = + "单选模式\n选中的时间:\n${controller.getSingleSelectCalendar()}"; }); + + text = new ValueNotifier("${DateTime.now().year}年${DateTime.now().month}月"); + + selectText = new ValueNotifier( + "单选模式\n选中的时间:\n${controller.getSingleSelectCalendar()}"); } @override @@ -55,7 +62,11 @@ class _DefaultStylePageState extends State { // controller.moveToPreviousMonth(); controller.previousPage(); }), - new Text(text), + ValueListenableBuilder( + valueListenable: text, + builder: (context, value, child) { + return new Text(text.value); + }), new IconButton( icon: Icon(Icons.navigate_next), onPressed: () { @@ -67,8 +78,11 @@ class _DefaultStylePageState extends State { CalendarViewWidget( calendarController: controller, ), - new Text( - "单选模式\n选中的时间:\n${controller.getSingleSelectCalendar().toString()}"), + ValueListenableBuilder( + valueListenable: selectText, + builder: (context, value, child) { + return new Text(selectText.value); + }), ], ), ), diff --git a/example/lib/multi_select_style_page.dart b/example/lib/multi_select_style_page.dart index 8545024..10582f8 100644 --- a/example/lib/multi_select_style_page.dart +++ b/example/lib/multi_select_style_page.dart @@ -1,6 +1,9 @@ import 'package:flutter/material.dart'; import 'package:flutter_custom_calendar/flutter_custom_calendar.dart'; +/** + * 自定义风格+多选 + */ class MultiSelectStylePage extends StatefulWidget { MultiSelectStylePage({Key key, this.title}) : super(key: key); @@ -11,14 +14,13 @@ class MultiSelectStylePage extends StatefulWidget { } class _MultiSelectStylePageState extends State { - String text; + ValueNotifier text; + ValueNotifier selectText; CalendarController controller; @override void initState() { - text = "${DateTime.now().year}年${DateTime.now().month}月"; - controller = new CalendarController( selectMode: Constants.MODE_MULTI_SELECT, maxMultiSelectCount: 5, @@ -34,23 +36,20 @@ class _MultiSelectStylePageState extends State { controller.addMonthChangeListener( (year, month) { - setState(() { - text = "$year年$month月"; - }); + text.value = "$year年$month月"; }, ); controller.addOnCalendarSelectListener((dateModel) { //刷新选择的时间 - setState(() {}); + selectText.value = + "单选模式\n选中的时间:\n${controller.getSingleSelectCalendar()}"; }); - controller.addOnMultiSelectOutOfSizeListener(() { - print("超出限制个数"); - }); - controller.addOnMultiSelectOutOfRangeListener(() { - print("超出范围限制"); - }); + text = new ValueNotifier("${DateTime.now().year}年${DateTime.now().month}月"); + + selectText = new ValueNotifier( + "单选模式\n选中的时间:\n${controller.getSingleSelectCalendar()}"); } @override @@ -70,7 +69,11 @@ class _MultiSelectStylePageState extends State { onPressed: () { controller.moveToPreviousMonth(); }), - new Text(text), + ValueListenableBuilder( + valueListenable: text, + builder: (context, value, child) { + return new Text(text.value); + }), new IconButton( icon: Icon(Icons.navigate_next), onPressed: () { @@ -81,8 +84,11 @@ class _MultiSelectStylePageState extends State { CalendarViewWidget( calendarController: controller, ), -// new Text( -// "多选模式\n选中的时间:\n${controller.getMultiSelectCalendar().toString()}"), + ValueListenableBuilder( + valueListenable: selectText, + builder: (context, value, child) { + return new Text(selectText.value); + }), ], ), ), diff --git a/example/lib/progress_style_page.dart b/example/lib/progress_style_page.dart index 8716d1a..39a5cf3 100644 --- a/example/lib/progress_style_page.dart +++ b/example/lib/progress_style_page.dart @@ -2,6 +2,9 @@ import 'package:flutter/material.dart'; import 'package:flutter_custom_calendar/flutter_custom_calendar.dart'; import 'dart:math'; +/** + * 进度条风格+单选 + */ class ProgressStylePage extends StatefulWidget { ProgressStylePage({Key key, this.title}) : super(key: key); @@ -12,18 +15,23 @@ class ProgressStylePage extends StatefulWidget { } class _ProgressStylePageState extends State { - String text; + ValueNotifier text; + ValueNotifier selectText; CalendarController controller; @override void initState() { - text = "${DateTime.now().year}年${DateTime.now().month}月"; - DateTime now = DateTime.now(); DateTime temp = DateTime(now.year, now.month, now.day); Map progressMap = { + DateModel.fromDateTime(temp.add(Duration(days: -1))): 0, + DateModel.fromDateTime(temp.add(Duration(days: -2))): 20, + DateModel.fromDateTime(temp.add(Duration(days: -3))): 40, + DateModel.fromDateTime(temp.add(Duration(days: -4))): 60, + DateModel.fromDateTime(temp.add(Duration(days: -5))): 80, + DateModel.fromDateTime(temp.add(Duration(days: -6))): 100, DateModel.fromDateTime(temp.add(Duration(days: 1))): 0, DateModel.fromDateTime(temp.add(Duration(days: 2))): 20, DateModel.fromDateTime(temp.add(Duration(days: 3))): 40, @@ -43,23 +51,20 @@ class _ProgressStylePageState extends State { controller.addMonthChangeListener( (year, month) { - setState(() { - text = "$year年$month月"; - }); + text.value = "$year年$month月"; }, ); controller.addOnCalendarSelectListener((dateModel) { //刷新选择的时间 - setState(() {}); + selectText.value = + "单选模式\n选中的时间:\n${controller.getSingleSelectCalendar()}"; }); - controller.addOnMultiSelectOutOfSizeListener(() { - print("超出限制个数"); - }); - controller.addOnMultiSelectOutOfRangeListener(() { - print("超出范围限制"); - }); + text = new ValueNotifier("${DateTime.now().year}年${DateTime.now().month}月"); + + selectText = new ValueNotifier( + "单选模式\n选中的时间:\n${controller.getSingleSelectCalendar()}"); } @override @@ -79,7 +84,11 @@ class _ProgressStylePageState extends State { onPressed: () { controller.moveToPreviousMonth(); }), - new Text(text), + ValueListenableBuilder( + valueListenable: text, + builder: (context, value, child) { + return new Text(text.value); + }), new IconButton( icon: Icon(Icons.navigate_next), onPressed: () { @@ -90,8 +99,11 @@ class _ProgressStylePageState extends State { CalendarViewWidget( calendarController: controller, ), -// new Text( -// "单选模式\n选中的时间:\n${controller.getSingleSelectCalendar().toString()}"), + ValueListenableBuilder( + valueListenable: selectText, + builder: (context, value, child) { + return new Text(selectText.value); + }), ], ), ), diff --git a/lib/model/date_model.dart b/lib/model/date_model.dart index 70d0f11..61c6bb9 100644 --- a/lib/model/date_model.dart +++ b/lib/model/date_model.dart @@ -1,3 +1,4 @@ +import 'package:flutter_custom_calendar/utils/date_util.dart'; import 'package:flutter_custom_calendar/utils/lunar_util.dart'; /** @@ -8,20 +9,38 @@ class DateModel { int month; int day = 1; - int lunarYear; - int lunarMonth; - int lunarDay; + List lunar = List(3); - String lunarString; //农历字符串 - String solarTerm; //24节气 - String gregorianFestival; //公历节日 - String traditionFestival; //传统农历节日 + //农历字符串 + String get lunarString { + if (solarTerm.isNotEmpty) { + return solarTerm; + } else if (gregorianFestival.isNotEmpty) { + return gregorianFestival; + } else if (traditionFestival.isNotEmpty) { + return traditionFestival; + } else { + return LunarUtil.numToChinese(lunar[1], lunar[2], lunar[3]); + } + } + + //24节气 + String get solarTerm => LunarUtil.getSolarTerm(year, month, day); + + //公历节日 + String get gregorianFestival { + String result = LunarUtil.gregorianFestival(month, day); + if (result?.isNotEmpty == true) { + return result; + } + return LunarUtil.getSpecialFestival(year, month, day); + } + +//传统农历节日 + String get traditionFestival => + LunarUtil.getTraditionFestival(year, month, day); - bool isCurrentMonth;//是否是当前月份 - bool isCurrentDay; //是否是今天 - bool isLeapYear; //是否是闰年 - bool isWeekend; //是否是周末 -// int leapMonth; //是否是闰月 + bool isCurrentMonth; //是否是当前月份 Object extraData; //自定义的额外数据 @@ -29,6 +48,20 @@ class DateModel { bool isSelected; //是否被选中,用来实现一些标记或者选择功能 bool isCanClick = true; //todo:是否可点击:设置范围外的日历不可点击,或者可以通过自定义拦截点击事件来设置true或者false + //是否是周末 + bool get isWeekend => DateUtil.isWeekend(getDateTime()); + + //是否是闰年 + bool get isLeapYear => DateUtil.isLeapYear(year); + + //是否是今天 + bool get isCurrentDay => DateUtil.isCurrentDay(year, month, day); + + int get lunarYear => lunar[0]; + + int get lunarMonth => lunar[1]; + + int get lunarDay => lunar[2]; @override String toString() { @@ -46,7 +79,12 @@ class DateModel { ..year = dateTime.year ..month = dateTime.month ..day = dateTime.day; - LunarUtil.setupLunarCalendar(dateModel); + List lunar = + LunarUtil.solarToLunar(dateModel.year, dateModel.month, dateModel.day); + dateModel.lunar = lunar; + +// 将数据的初始化放到各个get方法里面进行操作,类似懒加载,不然很浪费 +// LunarUtil.setupLunarCalendar(dateModel); return dateModel; } diff --git a/lib/utils/lunar_util.dart b/lib/utils/lunar_util.dart index 18c9a2c..23b0755 100644 --- a/lib/utils/lunar_util.dart +++ b/lib/utils/lunar_util.dart @@ -762,44 +762,44 @@ class LunarUtil { int month = dateModel.month; int day = dateModel.day; - dateModel.isWeekend = DateUtil.isWeekend(new DateTime(year, month, day)); - dateModel.isLeapYear = DateUtil.isLeapYear(year); - dateModel.isCurrentDay = DateUtil.isCurrentDay(year, month, day); +// dateModel.isWeekend = DateUtil.isWeekend(new DateTime(year, month, day)); +// dateModel.isLeapYear = DateUtil.isLeapYear(year); +// dateModel.isCurrentDay = DateUtil.isCurrentDay(year, month, day); List lunar = LunarUtil.solarToLunar(2020, 2, day); - dateModel.lunarYear = (lunar[0]); - dateModel.lunarMonth = (lunar[1]); - dateModel.lunarDay = (lunar[2]); +// dateModel.lunarYear = (lunar[0]); +// dateModel.lunarMonth = (lunar[1]); +// dateModel.lunarDay = (lunar[2]); // if (lunar[3] == 1) { // //如果是闰月 // dateModel.leapMonth = lunar[1]; // } //24节气 - String solarTerm = getSolarTerm(year, month, day); - dateModel.solarTerm=solarTerm; - //公历节日 - String gregorian = gregorianFestival(month, day); - dateModel.gregorianFestival=gregorian; - //传统农历节日 - String festival = getTraditionFestival(lunar[0], lunar[1], lunar[2]); - dateModel.traditionFestival=festival; +// String solarTerm = getSolarTerm(year, month, day); +// dateModel.solarTerm=solarTerm; +// //公历节日 +// String gregorian = gregorianFestival(month, day); +// dateModel.gregorianFestival=gregorian; +// //传统农历节日 +// String festival = getTraditionFestival(lunar[0], lunar[1], lunar[2]); +// dateModel.traditionFestival=festival; //农历格式的日期 String lunarText = numToChinese(lunar[1], lunar[2], lunar[3]); - if (gregorian.isEmpty) { - gregorian = getSpecialFestival(year, month, day); - } - if (solarTerm.isNotEmpty) { - dateModel.lunarString = solarTerm; - } else if (gregorian.isNotEmpty) { - dateModel.lunarString = gregorian; - } else if (festival.isNotEmpty) { - dateModel.lunarString = festival; - } else { - dateModel.lunarString = lunarText; - } +// if (gregorian.isEmpty) { +// gregorian = getSpecialFestival(year, month, day); +// } +// if (solarTerm.isNotEmpty) { +// dateModel.lunarString = solarTerm; +// } else if (gregorian.isNotEmpty) { +// dateModel.lunarString = gregorian; +// } else if (festival.isNotEmpty) { +// dateModel.lunarString = festival; +// } else { +// dateModel.lunarString = lunarText; +// } } /** diff --git a/lib/widget/month_view.dart b/lib/widget/month_view.dart index a6b25a7..69960d6 100644 --- a/lib/widget/month_view.dart +++ b/lib/widget/month_view.dart @@ -57,6 +57,7 @@ class _MonthViewState extends State { @override Widget build(BuildContext context) { + LogUtil.log(TAG: this.runtimeType, message: "_MonthViewState build"); itemHeight = MediaQuery.of(context).size.width / 7; totalHeight = itemHeight * lineCount + mainSpacing * (lineCount - 1); @@ -68,10 +69,7 @@ class _MonthViewState extends State { builder: (context, calendarProvider, child) { CalendarConfiguration configuration = calendarProvider.calendarConfiguration; - LogUtil.log( - TAG: this.runtimeType, - message: - "build calendarProvider.selectDateModel:${calendarProvider.selectDateModel}"); + return new GridView.builder( physics: NeverScrollableScrollPhysics(), gridDelegate: new SliverGridDelegateWithFixedCrossAxisCount( @@ -95,6 +93,8 @@ class _MonthViewState extends State { } return GestureDetector( + //点击整个item都会触发事件 + behavior: HitTestBehavior.opaque, onTap: () { LogUtil.log( TAG: this.runtimeType, @@ -119,16 +119,15 @@ class _MonthViewState extends State { return; } + configuration.calendarSelect(dateModel); + if (calendarProvider.selectedDateList.contains(dateModel)) { + calendarProvider.selectedDateList.remove(dateModel); + } else { + calendarProvider.selectedDateList.add(dateModel); + } + //多选也可以弄这些单选的代码 calendarProvider.selectDateModel = dateModel; - configuration.calendarSelect(dateModel); - setState(() { - if (calendarProvider.selectedDateList.contains(dateModel)) { - calendarProvider.selectedDateList.remove(dateModel); - } else { - calendarProvider.selectedDateList.add(dateModel); - } - }); } else { calendarProvider.selectDateModel = dateModel; configuration.calendarSelect(dateModel);