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

@ -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<CustomStylePage> {
String text;
ValueNotifier<String> text;
ValueNotifier<String> 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<CustomStylePage> {
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<CustomStylePage> {
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<CustomStylePage> {
CalendarViewWidget(
calendarController: controller,
),
new Text(
"自定义创建Item\n选中的时间:\n${controller.getSingleSelectCalendar().toString()}"),
ValueListenableBuilder(
valueListenable: selectText,
builder: (context, value, child) {
return new Text(selectText.value);
}),
],
),
),

@ -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<DefaultStylePage> {
String text;
ValueNotifier<String> text;
ValueNotifier<String> 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<DefaultStylePage> {
// 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<DefaultStylePage> {
CalendarViewWidget(
calendarController: controller,
),
new Text(
"单选模式\n选中的时间:\n${controller.getSingleSelectCalendar().toString()}"),
ValueListenableBuilder(
valueListenable: selectText,
builder: (context, value, child) {
return new Text(selectText.value);
}),
],
),
),

@ -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<MultiSelectStylePage> {
String text;
ValueNotifier<String> text;
ValueNotifier<String> 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<MultiSelectStylePage> {
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<MultiSelectStylePage> {
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<MultiSelectStylePage> {
CalendarViewWidget(
calendarController: controller,
),
// new Text(
// "多选模式\n选中的时间:\n${controller.getMultiSelectCalendar().toString()}"),
ValueListenableBuilder(
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 '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<ProgressStylePage> {
String text;
ValueNotifier<String> text;
ValueNotifier<String> 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<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: 2))): 20,
DateModel.fromDateTime(temp.add(Duration(days: 3))): 40,
@ -43,23 +51,20 @@ class _ProgressStylePageState extends State<ProgressStylePage> {
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<ProgressStylePage> {
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<ProgressStylePage> {
CalendarViewWidget(
calendarController: controller,
),
// new Text(
// "单选模式\n选中的时间:\n${controller.getSingleSelectCalendar().toString()}"),
ValueListenableBuilder(
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';
/**
@ -8,20 +9,38 @@ class DateModel {
int month;
int day = 1;
int lunarYear;
int lunarMonth;
int lunarDay;
List<int> 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; //
Object extraData; //
@ -29,6 +48,20 @@ class DateModel {
bool isSelected; //
bool isCanClick =
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
String toString() {
@ -46,7 +79,12 @@ class DateModel {
..year = dateTime.year
..month = dateTime.month
..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;
}

@ -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<int> 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;
// }
}
/**

@ -57,6 +57,7 @@ class _MonthViewState extends State<MonthView> {
@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<MonthView> {
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<MonthView> {
}
return GestureDetector(
//item
behavior: HitTestBehavior.opaque,
onTap: () {
LogUtil.log(
TAG: this.runtimeType,
@ -119,16 +119,15 @@ class _MonthViewState extends State<MonthView> {
return;
}
//
calendarProvider.selectDateModel = dateModel;
configuration.calendarSelect(dateModel);
setState(() {
if (calendarProvider.selectedDateList.contains(dateModel)) {
calendarProvider.selectedDateList.remove(dateModel);
} else {
calendarProvider.selectedDateList.add(dateModel);
}
});
//
calendarProvider.selectDateModel = dateModel;
} else {
calendarProvider.selectDateModel = dateModel;
configuration.calendarSelect(dateModel);

Loading…
Cancel
Save