You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

138 lines
3.6 KiB

import 'package:flutter_custom_calendar/utils/date_util.dart';
6 years ago
import 'package:flutter_custom_calendar/utils/lunar_util.dart';
/**
*
*/
class DateModel {
3 years ago
int? year;
int? month;
6 years ago
int day = 1;
3 years ago
List<int> lunar = [];
6 years ago
// List<int> get lunar {
// if (lunar?.isNotEmpty == false) {
// return lunar;
// }
//// return LunarUtil.solarToLunar(year, month, day);
// }
//农历字符串
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节气
3 years ago
String get solarTerm => LunarUtil.getSolarTerm(year, month!, day);
//公历节日
String get gregorianFestival {
3 years ago
String result = LunarUtil.gregorianFestival(month!, day);
3 years ago
if (result.isNotEmpty == true) {
return result;
}
3 years ago
return LunarUtil.getSpecialFestival(year, month!, day);
}
//传统农历节日
String get traditionFestival =>
LunarUtil.getTraditionFestival(lunarYear, lunarMonth, lunarDay);
6 years ago
3 years ago
bool? isCurrentMonth; //是否是当前月份
6 years ago
3 years ago
Object? extraData; //自定义的额外数据
6 years ago
bool isInRange = false; //是否在范围内,比如可以实现在某个范围外,设置置灰的功能
3 years ago
bool? isSelected; //是否被选中,用来实现一些标记或者选择功能
bool isCanClick =
true; //todo:是否可点击设置范围外的日历不可点击或者可以通过自定义拦截点击事件来设置true或者false
//是否是周末
bool get isWeekend => DateUtil.isWeekend(getDateTime());
//是否是闰年
3 years ago
bool get isLeapYear => DateUtil.isLeapYear(year!);
//是否是今天
bool get isCurrentDay => DateUtil.isCurrentDay(year, month, day);
3 years ago
int? get lunarYear => lunar[0];
3 years ago
int? get lunarMonth => lunar[1];
3 years ago
int? get lunarDay => lunar[2];
6 years ago
@override
String toString() {
return 'DateModel{year: $year, month: $month, day: $day}';
} //如果是闰月,则返回闰月
//转化成DateTime格式
DateTime getDateTime() {
3 years ago
return new DateTime(year!, month!, day);
6 years ago
}
//根据DateTime创建对应的model并初始化农历和传统节日等信息
static DateModel fromDateTime(DateTime dateTime) {
DateModel dateModel = new DateModel()
..year = dateTime.year
..month = dateTime.month
..day = dateTime.day;
3 years ago
List<int> lunar =
3 years ago
LunarUtil.solarToLunar(dateModel.year!, dateModel.month!, dateModel.day);
dateModel.lunar = lunar;
// 将数据的初始化放到各个get方法里面进行操作类似懒加载,不然很浪费
// LunarUtil.setupLunarCalendar(dateModel);
6 years ago
return dateModel;
}
@override
bool operator ==(Object other) =>
identical(this, other) ||
other is DateModel &&
runtimeType == other.runtimeType &&
year == other.year &&
month == other.month &&
day == other.day;
@override
int get hashCode => year.hashCode ^ month.hashCode ^ day.hashCode;
//是否是同一天
bool isSameWith(DateModel dateModel) {
return year == dateModel.year &&
month == dateModel.month &&
day == dateModel.day;
}
//是否在某天之后
bool isAfter(DateModel dateModel) {
return this.getDateTime().isAfter(dateModel.getDateTime());
}
//是否在某天之前
bool isBefore(DateModel dateModel) {
return this.getDateTime().isBefore(dateModel.getDateTime());
}
6 years ago
}
5 years ago
class X {
var _y;
get y => null == _y ? initY() : _y;
initY() {
//do some computation
_y = "result";
}
}