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

引入provider状态管理,避免深层嵌套传递信息
周视图和月视图,联动
增加日志输出类LogUtil,方便查看调试
增加example例子
develop
xiaodong 5 years ago
parent 025be98263
commit 93009e9324

@ -16,7 +16,8 @@ class CustomSignPage extends StatefulWidget {
} }
class _CustomSignPageState extends State<CustomSignPage> { class _CustomSignPageState extends State<CustomSignPage> {
String text; ValueNotifier<String> text;
ValueNotifier<String> selectText;
CalendarController controller; CalendarController controller;
@ -42,8 +43,6 @@ class _CustomSignPageState extends State<CustomSignPage> {
@override @override
void initState() { void initState() {
text = "${DateTime.now().year}${DateTime.now().month}";
controller = new CalendarController( controller = new CalendarController(
weekBarItemWidgetBuilder: () { weekBarItemWidgetBuilder: () {
return CustomStyleWeekBarItem(); return CustomStyleWeekBarItem();
@ -55,16 +54,20 @@ class _CustomSignPageState extends State<CustomSignPage> {
controller.addMonthChangeListener( controller.addMonthChangeListener(
(year, month) { (year, month) {
setState(() { text.value = "$year$month";
text = "$year$month";
});
}, },
); );
controller.addOnCalendarSelectListener((dateModel) { 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 @override
@ -84,7 +87,11 @@ class _CustomSignPageState extends State<CustomSignPage> {
onPressed: () { onPressed: () {
controller.moveToPreviousMonth(); controller.moveToPreviousMonth();
}), }),
new Text(text), ValueListenableBuilder(
valueListenable: text,
builder: (context, value, child) {
return new Text(text.value);
}),
new IconButton( new IconButton(
icon: Icon(Icons.navigate_next), icon: Icon(Icons.navigate_next),
onPressed: () { onPressed: () {
@ -95,8 +102,11 @@ class _CustomSignPageState extends State<CustomSignPage> {
CalendarViewWidget( CalendarViewWidget(
calendarController: controller, calendarController: controller,
), ),
new Text( ValueListenableBuilder(
"自定义创建Item\n选中的时间:\n${controller.getSingleSelectCalendar().toString()}"), valueListenable: selectText,
builder: (context, value, child) {
return new Text(selectText.value);
}),
], ],
), ),
), ),

@ -1,7 +1,9 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_custom_calendar/flutter_custom_calendar.dart'; import 'package:flutter_custom_calendar/flutter_custom_calendar.dart';
/**
* +
*/
class CustomStylePage extends StatefulWidget { class CustomStylePage extends StatefulWidget {
CustomStylePage({Key key, this.title}) : super(key: key); CustomStylePage({Key key, this.title}) : super(key: key);
@ -12,14 +14,13 @@ class CustomStylePage extends StatefulWidget {
} }
class _CustomStylePageState extends State<CustomStylePage> { class _CustomStylePageState extends State<CustomStylePage> {
String text; ValueNotifier<String> text;
ValueNotifier<String> selectText;
CalendarController controller; CalendarController controller;
@override @override
void initState() { void initState() {
text = "${DateTime.now().year}${DateTime.now().month}";
controller = new CalendarController(weekBarItemWidgetBuilder: () { controller = new CalendarController(weekBarItemWidgetBuilder: () {
return CustomStyleWeekBarItem(); return CustomStyleWeekBarItem();
}, dayWidgetBuilder: (dateModel) { }, dayWidgetBuilder: (dateModel) {
@ -28,16 +29,20 @@ class _CustomStylePageState extends State<CustomStylePage> {
controller.addMonthChangeListener( controller.addMonthChangeListener(
(year, month) { (year, month) {
setState(() { text.value = "$year$month";
text = "$year$month";
});
}, },
); );
controller.addOnCalendarSelectListener((dateModel) { 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 @override
@ -57,7 +62,11 @@ class _CustomStylePageState extends State<CustomStylePage> {
onPressed: () { onPressed: () {
controller.moveToPreviousMonth(); controller.moveToPreviousMonth();
}), }),
new Text(text), ValueListenableBuilder(
valueListenable: text,
builder: (context, value, child) {
return new Text(text.value);
}),
new IconButton( new IconButton(
icon: Icon(Icons.navigate_next), icon: Icon(Icons.navigate_next),
onPressed: () { onPressed: () {
@ -68,8 +77,11 @@ class _CustomStylePageState extends State<CustomStylePage> {
CalendarViewWidget( CalendarViewWidget(
calendarController: controller, calendarController: controller,
), ),
new Text( ValueListenableBuilder(
"自定义创建Item\n选中的时间:\n${controller.getSingleSelectCalendar().toString()}"), valueListenable: selectText,
builder: (context, value, child) {
return new Text(selectText.value);
}),
], ],
), ),
), ),

@ -1,7 +1,11 @@
import 'package:example/main.dart'; import 'package:example/main.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_custom_calendar/flutter_custom_calendar.dart'; import 'package:flutter_custom_calendar/flutter_custom_calendar.dart';
import 'package:provider/provider.dart';
/**
* +
*/
class DefaultStylePage extends StatefulWidget { class DefaultStylePage extends StatefulWidget {
DefaultStylePage({Key key, this.title}) : super(key: key); DefaultStylePage({Key key, this.title}) : super(key: key);
@ -12,29 +16,32 @@ class DefaultStylePage extends StatefulWidget {
} }
class _DefaultStylePageState extends State<DefaultStylePage> { class _DefaultStylePageState extends State<DefaultStylePage> {
String text; ValueNotifier<String> text;
ValueNotifier<String> selectText;
CalendarController controller; CalendarController controller;
@override @override
void initState() { void initState() {
text = "${DateTime.now().year}${DateTime.now().month}";
controller = new CalendarController( controller = new CalendarController(
minYear: 2019, minYearMonth: 8, maxYear: 2019, maxYearMonth: 9); minYear: 2019, minYearMonth: 8, maxYear: 2019, maxYearMonth: 9);
controller.addMonthChangeListener( controller.addMonthChangeListener(
(year, month) { (year, month) {
setState(() { text.value = "$year$month";
text = "$year$month";
});
}, },
); );
controller.addOnCalendarSelectListener((dateModel) { 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 @override
@ -55,7 +62,11 @@ class _DefaultStylePageState extends State<DefaultStylePage> {
// controller.moveToPreviousMonth(); // controller.moveToPreviousMonth();
controller.previousPage(); controller.previousPage();
}), }),
new Text(text), ValueListenableBuilder(
valueListenable: text,
builder: (context, value, child) {
return new Text(text.value);
}),
new IconButton( new IconButton(
icon: Icon(Icons.navigate_next), icon: Icon(Icons.navigate_next),
onPressed: () { onPressed: () {
@ -67,8 +78,11 @@ class _DefaultStylePageState extends State<DefaultStylePage> {
CalendarViewWidget( CalendarViewWidget(
calendarController: controller, calendarController: controller,
), ),
new Text( ValueListenableBuilder(
"单选模式\n选中的时间:\n${controller.getSingleSelectCalendar().toString()}"), valueListenable: selectText,
builder: (context, value, child) {
return new Text(selectText.value);
}),
], ],
), ),
), ),

@ -1,6 +1,9 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter_custom_calendar/flutter_custom_calendar.dart'; import 'package:flutter_custom_calendar/flutter_custom_calendar.dart';
/**
* +
*/
class MultiSelectStylePage extends StatefulWidget { class MultiSelectStylePage extends StatefulWidget {
MultiSelectStylePage({Key key, this.title}) : super(key: key); MultiSelectStylePage({Key key, this.title}) : super(key: key);
@ -11,14 +14,13 @@ class MultiSelectStylePage extends StatefulWidget {
} }
class _MultiSelectStylePageState extends State<MultiSelectStylePage> { class _MultiSelectStylePageState extends State<MultiSelectStylePage> {
String text; ValueNotifier<String> text;
ValueNotifier<String> selectText;
CalendarController controller; CalendarController controller;
@override @override
void initState() { void initState() {
text = "${DateTime.now().year}${DateTime.now().month}";
controller = new CalendarController( controller = new CalendarController(
selectMode: Constants.MODE_MULTI_SELECT, selectMode: Constants.MODE_MULTI_SELECT,
maxMultiSelectCount: 5, maxMultiSelectCount: 5,
@ -34,23 +36,20 @@ class _MultiSelectStylePageState extends State<MultiSelectStylePage> {
controller.addMonthChangeListener( controller.addMonthChangeListener(
(year, month) { (year, month) {
setState(() { text.value = "$year$month";
text = "$year$month";
});
}, },
); );
controller.addOnCalendarSelectListener((dateModel) { controller.addOnCalendarSelectListener((dateModel) {
// //
setState(() {}); selectText.value =
"单选模式\n选中的时间:\n${controller.getSingleSelectCalendar()}";
}); });
controller.addOnMultiSelectOutOfSizeListener(() { text = new ValueNotifier("${DateTime.now().year}${DateTime.now().month}");
print("超出限制个数");
}); selectText = new ValueNotifier(
controller.addOnMultiSelectOutOfRangeListener(() { "单选模式\n选中的时间:\n${controller.getSingleSelectCalendar()}");
print("超出范围限制");
});
} }
@override @override
@ -70,7 +69,11 @@ class _MultiSelectStylePageState extends State<MultiSelectStylePage> {
onPressed: () { onPressed: () {
controller.moveToPreviousMonth(); controller.moveToPreviousMonth();
}), }),
new Text(text), ValueListenableBuilder(
valueListenable: text,
builder: (context, value, child) {
return new Text(text.value);
}),
new IconButton( new IconButton(
icon: Icon(Icons.navigate_next), icon: Icon(Icons.navigate_next),
onPressed: () { onPressed: () {
@ -81,8 +84,11 @@ class _MultiSelectStylePageState extends State<MultiSelectStylePage> {
CalendarViewWidget( CalendarViewWidget(
calendarController: controller, calendarController: controller,
), ),
// new Text( ValueListenableBuilder(
// "多选模式\n选中的时间:\n${controller.getMultiSelectCalendar().toString()}"), valueListenable: selectText,
builder: (context, value, child) {
return new Text(selectText.value);
}),
], ],
), ),
), ),

@ -2,6 +2,9 @@ import 'package:flutter/material.dart';
import 'package:flutter_custom_calendar/flutter_custom_calendar.dart'; import 'package:flutter_custom_calendar/flutter_custom_calendar.dart';
import 'dart:math'; import 'dart:math';
/**
* +
*/
class ProgressStylePage extends StatefulWidget { class ProgressStylePage extends StatefulWidget {
ProgressStylePage({Key key, this.title}) : super(key: key); ProgressStylePage({Key key, this.title}) : super(key: key);
@ -12,18 +15,23 @@ class ProgressStylePage extends StatefulWidget {
} }
class _ProgressStylePageState extends State<ProgressStylePage> { class _ProgressStylePageState extends State<ProgressStylePage> {
String text; ValueNotifier<String> text;
ValueNotifier<String> selectText;
CalendarController controller; CalendarController controller;
@override @override
void initState() { void initState() {
text = "${DateTime.now().year}${DateTime.now().month}";
DateTime now = DateTime.now(); DateTime now = DateTime.now();
DateTime temp = DateTime(now.year, now.month, now.day); DateTime temp = DateTime(now.year, now.month, now.day);
Map<DateModel, int> progressMap = { Map<DateModel, int> 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: 1))): 0,
DateModel.fromDateTime(temp.add(Duration(days: 2))): 20, DateModel.fromDateTime(temp.add(Duration(days: 2))): 20,
DateModel.fromDateTime(temp.add(Duration(days: 3))): 40, DateModel.fromDateTime(temp.add(Duration(days: 3))): 40,
@ -43,23 +51,20 @@ class _ProgressStylePageState extends State<ProgressStylePage> {
controller.addMonthChangeListener( controller.addMonthChangeListener(
(year, month) { (year, month) {
setState(() { text.value = "$year$month";
text = "$year$month";
});
}, },
); );
controller.addOnCalendarSelectListener((dateModel) { controller.addOnCalendarSelectListener((dateModel) {
// //
setState(() {}); selectText.value =
"单选模式\n选中的时间:\n${controller.getSingleSelectCalendar()}";
}); });
controller.addOnMultiSelectOutOfSizeListener(() { text = new ValueNotifier("${DateTime.now().year}${DateTime.now().month}");
print("超出限制个数");
}); selectText = new ValueNotifier(
controller.addOnMultiSelectOutOfRangeListener(() { "单选模式\n选中的时间:\n${controller.getSingleSelectCalendar()}");
print("超出范围限制");
});
} }
@override @override
@ -79,7 +84,11 @@ class _ProgressStylePageState extends State<ProgressStylePage> {
onPressed: () { onPressed: () {
controller.moveToPreviousMonth(); controller.moveToPreviousMonth();
}), }),
new Text(text), ValueListenableBuilder(
valueListenable: text,
builder: (context, value, child) {
return new Text(text.value);
}),
new IconButton( new IconButton(
icon: Icon(Icons.navigate_next), icon: Icon(Icons.navigate_next),
onPressed: () { onPressed: () {
@ -90,8 +99,11 @@ class _ProgressStylePageState extends State<ProgressStylePage> {
CalendarViewWidget( CalendarViewWidget(
calendarController: controller, calendarController: controller,
), ),
// new Text( ValueListenableBuilder(
// "单选模式\n选中的时间:\n${controller.getSingleSelectCalendar().toString()}"), valueListenable: selectText,
builder: (context, value, child) {
return new Text(selectText.value);
}),
], ],
), ),
), ),

@ -1,3 +1,4 @@
import 'package:flutter_custom_calendar/utils/date_util.dart';
import 'package:flutter_custom_calendar/utils/lunar_util.dart'; import 'package:flutter_custom_calendar/utils/lunar_util.dart';
/** /**
@ -8,20 +9,38 @@ class DateModel {
int month; int month;
int day = 1; int day = 1;
int lunarYear; List<int> lunar = List(3);
int lunarMonth;
int lunarDay;
String lunarString; // //
String solarTerm; //24 String get lunarString {
String gregorianFestival; // if (solarTerm.isNotEmpty) {
String traditionFestival; // 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 isCurrentMonth; //
bool isCurrentDay; //
bool isLeapYear; //
bool isWeekend; //
// int leapMonth; //
Object extraData; // Object extraData; //
@ -29,6 +48,20 @@ class DateModel {
bool isSelected; // bool isSelected; //
bool isCanClick = bool isCanClick =
true; //todo:truefalse true; //todo:truefalse
//
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 @override
String toString() { String toString() {
@ -46,7 +79,12 @@ class DateModel {
..year = dateTime.year ..year = dateTime.year
..month = dateTime.month ..month = dateTime.month
..day = dateTime.day; ..day = dateTime.day;
LunarUtil.setupLunarCalendar(dateModel); List<int> lunar =
LunarUtil.solarToLunar(dateModel.year, dateModel.month, dateModel.day);
dateModel.lunar = lunar;
// get,
// LunarUtil.setupLunarCalendar(dateModel);
return dateModel; return dateModel;
} }

@ -762,44 +762,44 @@ class LunarUtil {
int month = dateModel.month; int month = dateModel.month;
int day = dateModel.day; int day = dateModel.day;
dateModel.isWeekend = DateUtil.isWeekend(new DateTime(year, month, day)); // dateModel.isWeekend = DateUtil.isWeekend(new DateTime(year, month, day));
dateModel.isLeapYear = DateUtil.isLeapYear(year); // dateModel.isLeapYear = DateUtil.isLeapYear(year);
dateModel.isCurrentDay = DateUtil.isCurrentDay(year, month, day); // dateModel.isCurrentDay = DateUtil.isCurrentDay(year, month, day);
List<int> lunar = LunarUtil.solarToLunar(2020, 2, day); List<int> lunar = LunarUtil.solarToLunar(2020, 2, day);
dateModel.lunarYear = (lunar[0]); // dateModel.lunarYear = (lunar[0]);
dateModel.lunarMonth = (lunar[1]); // dateModel.lunarMonth = (lunar[1]);
dateModel.lunarDay = (lunar[2]); // dateModel.lunarDay = (lunar[2]);
// if (lunar[3] == 1) { // if (lunar[3] == 1) {
// // // //
// dateModel.leapMonth = lunar[1]; // dateModel.leapMonth = lunar[1];
// } // }
//24 //24
String solarTerm = getSolarTerm(year, month, day); // String solarTerm = getSolarTerm(year, month, day);
dateModel.solarTerm=solarTerm; // dateModel.solarTerm=solarTerm;
// // //
String gregorian = gregorianFestival(month, day); // String gregorian = gregorianFestival(month, day);
dateModel.gregorianFestival=gregorian; // dateModel.gregorianFestival=gregorian;
// // //
String festival = getTraditionFestival(lunar[0], lunar[1], lunar[2]); // String festival = getTraditionFestival(lunar[0], lunar[1], lunar[2]);
dateModel.traditionFestival=festival; // dateModel.traditionFestival=festival;
// //
String lunarText = numToChinese(lunar[1], lunar[2], lunar[3]); String lunarText = numToChinese(lunar[1], lunar[2], lunar[3]);
if (gregorian.isEmpty) { // if (gregorian.isEmpty) {
gregorian = getSpecialFestival(year, month, day); // gregorian = getSpecialFestival(year, month, day);
} // }
if (solarTerm.isNotEmpty) { // if (solarTerm.isNotEmpty) {
dateModel.lunarString = solarTerm; // dateModel.lunarString = solarTerm;
} else if (gregorian.isNotEmpty) { // } else if (gregorian.isNotEmpty) {
dateModel.lunarString = gregorian; // dateModel.lunarString = gregorian;
} else if (festival.isNotEmpty) { // } else if (festival.isNotEmpty) {
dateModel.lunarString = festival; // dateModel.lunarString = festival;
} else { // } else {
dateModel.lunarString = lunarText; // dateModel.lunarString = lunarText;
} // }
} }
/** /**

@ -57,6 +57,7 @@ class _MonthViewState extends State<MonthView> {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
LogUtil.log(TAG: this.runtimeType, message: "_MonthViewState build");
itemHeight = MediaQuery.of(context).size.width / 7; itemHeight = MediaQuery.of(context).size.width / 7;
totalHeight = itemHeight * lineCount + mainSpacing * (lineCount - 1); totalHeight = itemHeight * lineCount + mainSpacing * (lineCount - 1);
@ -68,10 +69,7 @@ class _MonthViewState extends State<MonthView> {
builder: (context, calendarProvider, child) { builder: (context, calendarProvider, child) {
CalendarConfiguration configuration = CalendarConfiguration configuration =
calendarProvider.calendarConfiguration; calendarProvider.calendarConfiguration;
LogUtil.log(
TAG: this.runtimeType,
message:
"build calendarProvider.selectDateModel:${calendarProvider.selectDateModel}");
return new GridView.builder( return new GridView.builder(
physics: NeverScrollableScrollPhysics(), physics: NeverScrollableScrollPhysics(),
gridDelegate: new SliverGridDelegateWithFixedCrossAxisCount( gridDelegate: new SliverGridDelegateWithFixedCrossAxisCount(
@ -95,6 +93,8 @@ class _MonthViewState extends State<MonthView> {
} }
return GestureDetector( return GestureDetector(
//item
behavior: HitTestBehavior.opaque,
onTap: () { onTap: () {
LogUtil.log( LogUtil.log(
TAG: this.runtimeType, TAG: this.runtimeType,
@ -119,16 +119,15 @@ class _MonthViewState extends State<MonthView> {
return; return;
} }
//
calendarProvider.selectDateModel = dateModel;
configuration.calendarSelect(dateModel); configuration.calendarSelect(dateModel);
setState(() {
if (calendarProvider.selectedDateList.contains(dateModel)) { if (calendarProvider.selectedDateList.contains(dateModel)) {
calendarProvider.selectedDateList.remove(dateModel); calendarProvider.selectedDateList.remove(dateModel);
} else { } else {
calendarProvider.selectedDateList.add(dateModel); calendarProvider.selectedDateList.add(dateModel);
} }
});
//
calendarProvider.selectDateModel = dateModel;
} else { } else {
calendarProvider.selectDateModel = dateModel; calendarProvider.selectDateModel = dateModel;
configuration.calendarSelect(dateModel); configuration.calendarSelect(dateModel);

Loading…
Cancel
Save