Flutter仿钉钉考勤日历

DatePickerDialog(
initialDate: DateTime.now(),
firstDate: DateTime(2020),
lastDate: DateTime(2030),
onDateChanged: onDateChanged,
// 0:无状态,1:正常考勤 2:异常考情,迟到,早退,
// 若不满一个月,日历会自动用0补满一个月
checking: [
0,
0,
1,
2,
],
),

DatePickerDialog是在存在与Flutter的material包中,Flutter自带的日历是以dialog形式存在的,本文将dialog改成StatefulWidget直接在页面中,将多余东西去掉,直接在material/calendar_date_picker.dart中_DayPicker上进行修改。 2. 修改日历中日期样式:

Widget dayWidget = Container(
margin: EdgeInsets.all(4.0),
decoration: decoration,
alignment: Alignment.center,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text(localizations.formatDecimal(day),
style: TextStyle(
fontSize: 14.0,
color: dayColor,
fontWeight: FontWeight.bold)),
Visibility(
visible: checking[day - 1] == 1 || checking[day - 1] == 2,
child: Container(
height: 6.0,
width: 6.0,
decoration: BoxDecoration(
shape: BoxShape.circle,
color: isSelectedDay ? Colors.white :
(checking[day - 1] == 1 ? Color(0xFF1376EE): Color(0xFFFF8A21)),
),
),
),
],
),
);

Visibility原来没有,是修改加上去的,主要是显示当天打卡状态,若打卡正常则在日期显示下方显示蓝色小点,若有异常则显示橙色的点,若没有状态就不显示,checking则是使用DatePickerDialog传入的,由于日历从1开始,数组是从索引0开始的,所以使用checking[day - 1]才能准确获取某一日的打卡状态,day 则是日历中某一月中所有日期。

2.设置星期标题 修改后:

List _dayHeaders() {
final List result = [];
final List weekdays = [“日”, “一”, “二”, “三”, “四”, “五”, “六”];
for (int i = 1; true; i = (i + 1) % 7) {
final String weekday = weekdays[i];
result.add(ExcludeSemantics(
child: Center(
child: Text(weekday,
style: TextStyle(fontSize: 14.0, color: Color(0xFF999999)))),
));
if (i == (1 - 1) % 7) break;
}
return result;
}

原文:

List _dayHeaders(TextStyle? headerStyle, MaterialLocalizations localizations) {
final List result = [];
for (int i = localizations.firstDayOfWeekIndex; true; i = (i + 1) % 7) {
final String weekday = localizations.narrowWeekdays[i];
result.add(ExcludeSemantics(
child: Center(child: Text(weekday, style: headerStyle)),
));
if (i == (localizations.firstDayOfWeekIndex - 1) % 7)
break;
}
return result;
}

localizations.firstDayOfWeekIndex返回值为0或者1,若返回0,则星期日为每周的第一天;若返回1,则星期一为每周的第一天。本文中没有从localizations.firstDayOfWeekIndex获取,直接赋值为1,则每周从星期一开始。

3.补全每个月空白日期:

  • 获取指定月份有多少天

static int getDaysInMonth(int year, int month) {
if(month < 1){
year = year - 1;
month = month + 12;
}

if(month > 12){
year = year + 1;
month = month - 12;
}
if (month == DateTime.february) {
final bool isLeapYear = (year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0);
return isLeapYear ? 29 : 28;
}
const List daysInMonth = [31, -1, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
return daysInMonth[month - 1];
}

  • 获取指定月份1日的偏移量,即每个月第一天是星期几,若1号是星期3,因为日历每周是从第一天开始的,所以第一周的星期的星期一,星期二为空白,需要补全上个月的倒数后两天,所以还需要获取上个月的最后两天是哪两天。

// 获取日期偏移
static int firstDayOffsets(int year, int month) {
final int weekdayFromMonday = DateTime(year, month).weekday - 1;
int firstDayOfWeekIndex = 1;
firstDayOfWeekIndex = (firstDayOfWeekIndex - 1) % 7;
return (weekdayFromMonday - firstDayOfWeekIndex) % 7;
}

// 补全开始日期
int day = -dayOffset;
while (day < daysInMonth) {
day++;
if (day < 1) {
dayItems.add(Container(
margin: EdgeInsets.all(4.0),
alignment: Alignment.center,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
// daysInPreMonth上个月的天数-本月日期偏移则可把本月开始缺失的日期补全
Text(localizations.formatDecimal(daysInPreMonth - day.abs()),
style: TextStyle(
fontSize: 14.0,
color: Color(0xFF888888),
fontWeight: FontWeight.bold)),
Visibility(
visible: false,
child: Container(
height: 6.0,
width: 6.0,
decoration: BoxDecoration(
shape: BoxShape.circle,
color: Color(0xFFFF8A21),
),
),
),
],
),
));
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数初中级Android工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则近万的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。

img

img

img

img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点,真正体系化!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!

如果你觉得这些内容对你有帮助,可以扫码获取!!(备注:Android)

最后

如果你看到了这里,觉得文章写得不错就给个赞呗?如果你觉得那里值得改进的,请给我留言。一定会认真查询,修正不足。谢谢。

欢迎大家一起交流讨论啊~

《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!

不足。谢谢。

[外链图片转存中…(img-7mneYzly-1713315000224)]

欢迎大家一起交流讨论啊~

《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!

  • 29
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值