526 lines
17 KiB
Dart
Raw Permalink Normal View History

2023-07-13 14:13:44 +08:00
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:star_lock/main/lockDetail/checkingIn/checkingInDetail/checkingInDetail_state.dart';
2023-07-13 14:13:44 +08:00
import '../../../../app_settings/app_colors.dart';
import '../../../../tools/titleAppBar.dart';
import 'checkingInDetail_logic.dart';
2023-07-13 14:13:44 +08:00
class CheckingInDetailPage extends StatefulWidget {
2023-07-15 15:11:28 +08:00
const CheckingInDetailPage({Key? key}) : super(key: key);
2023-07-13 14:13:44 +08:00
@override
_CheckingInDetailPageState createState() => _CheckingInDetailPageState();
}
class _CheckingInDetailPageState extends State<CheckingInDetailPage> {
final CheckingInDetailLogic logic = Get.put(CheckingInDetailLogic());
final CheckingInDetailState state = Get.find<CheckingInDetailLogic>().state;
2023-07-13 14:13:44 +08:00
int _year = DateTime.now().year;
int _month = DateTime.now().month;
int _day = DateTime.now().day;
final List<CalendarModel> _datas = <CalendarModel>[];
final List<CalendarModel> _listDatas = <CalendarModel>[];
2023-07-13 14:13:44 +08:00
@override
void initState() {
super.initState();
2023-07-13 14:13:44 +08:00
//设置默认当前月日期
_setDatas(year: _year, month: _month);
logic.getCheckInDetailData((){
//设置模拟数据,日历月事件,可根据接口返回的结果
_loadAttendanceMonthRecord('$_year-$_month');
});
2023-07-13 14:13:44 +08:00
//设置模拟数据,日历月事件,可根据接口返回的结果
// _loadAttendanceMonthRecord("$_year-$_month");
2023-07-13 14:13:44 +08:00
//日历日事件
// _loadAttendanceDayRecord("$_year-$_month-$_day");
2023-07-13 14:13:44 +08:00
}
//加载月历事件,请求接口
_loadAttendanceMonthRecord(String dateTime) async {
// 显示的上个月的天数
final int placeholderDays = _getPlaceholderDays(year: _year, month: _month);
// 当月多少天
final int currentMonthDays = _getCurrentMonthDays(year: _year, month: _month);
2023-07-13 14:13:44 +08:00
setState(() {
// 因为_datas这个月上个月都包含的都有 遍历把本月的赋值
2023-07-13 14:13:44 +08:00
for (int i = 0; i < _datas.length; i++) {
// 因为i从0开始 所以i>=上个月的天数 且小于上个月跟本月天数之和
if((i >= placeholderDays) && (i < (placeholderDays + currentMonthDays))){
_datas[i].workType = state.monthListData[i-placeholderDays].colorType.toString();
2023-07-13 14:13:44 +08:00
}
}
});
}
//加载日事件,请求接口
_loadAttendanceDayRecord(String dateTime) async {
2023-07-13 14:13:44 +08:00
//可根据接口返回的内容在日历下面打卡信息或者其余内容
state.checkDate.value = DateTime.parse(dateTime).millisecondsSinceEpoch;
logic.getCheckInDetailData((){
//设置模拟数据,日历月事件,可根据接口返回的结果
_loadAttendanceMonthRecord('$_year-$_month');
});
2023-07-13 14:13:44 +08:00
}
2023-07-13 14:13:44 +08:00
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: TitleAppBar(
barTitle: state.staffName.value,
haveBack: true,
backgroundColor: AppColors.mainColor),
2023-07-13 14:13:44 +08:00
body: SingleChildScrollView(
child: Column(
children: <Widget>[
Container(
// margin: EdgeInsets.all(20.sp),
decoration: BoxDecoration(
//设置颜色
color: Colors.white,
borderRadius: BorderRadius.all(Radius.circular(12.r)),
//设置四周边框
),
child: Column(
children: <Widget>[
_yearHeader(),
SizedBox(height: 10.h),
_weekHeader(),
_everyDay(),
_bottomStatisticsWidget()
],
),
),
2023-07-13 14:13:44 +08:00
//下面可以添加日事件中的信息,例如打卡信息
Container(),
],
)));
2023-07-13 14:13:44 +08:00
}
//头部年
Widget _yearHeader() {
return Container(
height: 30,
margin: const EdgeInsets.only(top: 10),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
2023-07-13 14:13:44 +08:00
GestureDetector(
onTap: _lastMonth,
child: Image(width: 50.w, height: 30.w, image: const AssetImage('images/icon_left_black.png'),),
2023-07-13 14:13:44 +08:00
),
SizedBox(width: 60.w,),
Text('$_year-$_month', style: TextStyle(fontSize: 28.sp, color: Colors.black, fontWeight: FontWeight.w500)),
SizedBox(width: 60.w),
2023-07-13 14:13:44 +08:00
GestureDetector(
onTap: _nextMonth,
child: Image(width: 50.w, height: 30.w, image: const AssetImage('images/icon_right_black.png')),
2023-07-13 14:13:44 +08:00
),
],
),
);
}
//中部周
Widget _weekHeader() {
final List<String> array = <String>[
2024-07-26 09:21:22 +08:00
'简写周一'.tr,
'简写周二'.tr,
'简写周三'.tr,
'简写周四'.tr,
'简写周五'.tr,
'简写周六'.tr,
'简写周日'.tr
];
return SizedBox(
2023-07-13 14:13:44 +08:00
height: 50.h,
child: GridView.builder(
padding: const EdgeInsets.only(left: 10, right: 10),
itemCount: array.length,
shrinkWrap: true,
physics: const NeverScrollableScrollPhysics(),
gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
//横轴元素个数
2023-07-13 14:13:44 +08:00
crossAxisCount: 7,
//纵轴间距
// mainAxisSpacing: ScreenUtil().setHeight(10),
// 横轴间距
// crossAxisSpacing: ScreenUtil().setWidth(15),
//子组件宽高长度比例
childAspectRatio: 2),
itemBuilder: (BuildContext context, int index) {
2023-07-13 14:13:44 +08:00
return Container(
alignment: Alignment.center,
child: Text(
array[index],
style: TextStyle(
color:
// index == 5 || index == 6
// ? const Color(0xFFC4C8D0) :
const Color(0xFF3C3E43),
fontSize: 26.sp),
2023-07-13 14:13:44 +08:00
));
},
),
);
}
//底部日
Widget _everyDay() {
return GridView.builder(
padding: EdgeInsets.only(left: 10.sp, top: 10.sp, right: 10.sp),
itemCount: _getRowsForMonthYear(year: _year, month: _month) * 7,
shrinkWrap: true,
physics: const NeverScrollableScrollPhysics(),
gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
//横轴元素个数
crossAxisCount: 7,
// //纵轴间距
// mainAxisSpacing: ScreenUtil().setHeight(10),
// // 横轴间距
// crossAxisSpacing: ScreenUtil().setWidth(10),
//子组件宽高长度比例
childAspectRatio: 1),
itemBuilder: (BuildContext context, int index) {
Color backColor = Colors.white;
// Color textColor = const Color(0xFFFFFFFF);
if(_datas[index].workType == '1'){
// 迟到
backColor = const Color(0xFFE83523);
}else if( _datas[index].workType == '2'){
// 早退
backColor = const Color(0xFFEDB459);
}else if( _datas[index].workType == '3'){
// 未打卡
backColor = const Color(0xFF666666);
}
return GestureDetector(
onTap: () {
// setState(() {
// if (_datas[index].month == _month) {
// //判断点击的是否是当月日期,只对当前月点击的日期做处理
// for (int i = 0; i < _datas.length; i++) {
// if (i == index) {
// //切换至选中的日期
// _day = _datas[i].day!;
// _datas[i].isSelect = true;
//
// //加载选中的日期事件
// _loadAttendanceDayRecord(
// "${_datas[i].year}-${_datas[i].month}-${_datas[i].day}");
// } else {
// _datas[i].isSelect = false;
// }
// }
// } else {
// //不是当月的不做处理
// // _datas[index].is_select=false;
// }
// });
},
child: Column(
children: <Widget>[
Container(
width: 40.w,
height: 40.w,
//设置底部背景
decoration: BoxDecoration(
color: backColor,
shape: BoxShape.circle,
),
child: Center(
child: Text(
//不是当前月不显示值
_datas[index].month == _month
? _datas[index].day.toString()
: '',
textAlign: TextAlign.center,
//设置选中字体颜色 休息的颜色都是黑色 其余的都是白色因为有背景色
style: (int.parse(_datas[index].workType!) > 0)
? TextStyle(
fontSize: 24.sp, color: const Color(0xFFFFFFFF))
: TextStyle(
fontSize: 24.sp,
color: Colors.black),
2023-07-13 14:13:44 +08:00
),
),
2023-07-13 14:13:44 +08:00
),
const SizedBox(height: 5),
//设置底部小圆点,非当前月的不显示,设置为透明,其余的根据状态判断显示
// _datas[index].month == _month &&
// _datas[index].workType != "" &&
// _datas[index].workType != "0"
// ? Container(
// height: 6.0,
// width: 6.0,
// decoration: BoxDecoration(
// shape: BoxShape.circle,
// color: _datas[index].workType == "1"
// ? const Color(0xFFF48835)
// : const Color(0xFF2C91F6)),
// )
// : Container(),
],
),
);
},
2023-07-13 14:13:44 +08:00
);
}
Widget _bottomStatisticsWidget() {
2023-07-13 14:13:44 +08:00
return Column(
children: <Widget>[
2023-07-13 14:13:44 +08:00
Row(
children: <Widget>[
2023-07-13 14:13:44 +08:00
Container(
width: 1.sw,
// height: 40.h,
padding: EdgeInsets.only(left: 50.w, top: 5.h, bottom: 5.h),
2023-07-13 14:13:44 +08:00
color: Colors.grey,
child: Text(
'月统计'.tr,
style: TextStyle(
color: Colors.white,
fontSize: 26.sp,
fontWeight: FontWeight.w500),
),
2023-07-13 14:13:44 +08:00
),
],
),
_bottomStatisticsItemWidget(const Color(0xFFE83523), '迟到'.tr, state.lateTimes.value),
_bottomStatisticsItemWidget(const Color(0xFFEDB459), '早退'.tr, state.earlyTimes.value),
_bottomStatisticsItemWidget(const Color(0xFF666666), '未打卡'.tr, state.noPunchTimes.value),
2023-07-13 14:13:44 +08:00
],
);
}
Widget _bottomStatisticsItemWidget(
Color color,
String leftTitel,
String rightTitle,
) {
2023-07-13 14:13:44 +08:00
return Column(
children: <Widget>[
2023-07-13 14:13:44 +08:00
Container(
// height: 70.h,
padding:
EdgeInsets.only(left: 20.w, right: 10.w, top: 20.w, bottom: 20.w),
2023-07-13 14:13:44 +08:00
child: Row(
children: <Widget>[
SizedBox(width: 20.w),
2023-07-13 14:13:44 +08:00
Container(
width: 30.w,
height: 30.w,
2023-07-13 14:13:44 +08:00
decoration: BoxDecoration(
color: color,
borderRadius: BorderRadius.all(Radius.circular(30.h))),
2023-07-13 14:13:44 +08:00
),
SizedBox(width: 20.w),
Expanded(
child: Text(leftTitel,
style: TextStyle(
fontSize: 24.sp, fontWeight: FontWeight.w500))),
SizedBox(width: 20.w),
Text(rightTitle,
textAlign: TextAlign.end,
style:
TextStyle(fontSize: 24.sp, fontWeight: FontWeight.w500)),
SizedBox(width: 10.w),
2023-07-13 14:13:44 +08:00
],
),
),
Container(
height: 0.5.h,
color: Colors.grey,
)
2023-07-13 14:13:44 +08:00
],
);
}
// 获取行数
2023-07-15 15:11:28 +08:00
int _getRowsForMonthYear({int? year, int? month}) {
// 当前月天数
final int currentMonthDays = _getCurrentMonthDays(year: year, month: month);
// 这个月1号前面有几天
final int placeholderDays = _getPlaceholderDays(year: year, month: month);
2023-07-13 14:13:44 +08:00
int rows = (currentMonthDays + placeholderDays) ~/ 7;
final int remainder = (currentMonthDays + placeholderDays) % 7;
2023-07-13 14:13:44 +08:00
if (remainder > 0) {
rows = rows + 1;
}
return rows;
}
// 得到这个月的第一天是星期几
2023-07-15 15:11:28 +08:00
int _getPlaceholderDays({int? year, int? month}) {
return DateTime(year!, month!).weekday - 1 % 7;
2023-07-13 14:13:44 +08:00
}
// 获取当前月份天数
2023-07-15 15:11:28 +08:00
int _getCurrentMonthDays({int? year, int? month}) {
2023-07-13 14:13:44 +08:00
if (month == 2) {
//判断2月份是闰年月还是平年
2023-07-15 15:11:28 +08:00
if (((year! % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) {
2023-07-13 14:13:44 +08:00
return 29;
} else {
return 28;
}
} else if (month == 1 ||
month == 3 ||
month == 5 ||
month == 7 ||
month == 8 ||
month == 10 ||
month == 12) {
return 31;
} else {
return 30;
}
}
/// 获取展示信息
2023-07-15 15:11:28 +08:00
_setDatas({int? year, int? month}) {
2023-07-13 14:13:44 +08:00
/// 上个月占位
int? lastYear = year;
int lastMonth = month! - 1;
2023-07-13 14:13:44 +08:00
if (month == 1) {
2023-07-15 15:11:28 +08:00
lastYear = (year! - 1)!;
2023-07-13 14:13:44 +08:00
lastMonth = 12;
}
final int placeholderDays = _getPlaceholderDays(year: year, month: month);
final int lastMonthDays = _getCurrentMonthDays(year: lastYear, month: lastMonth);
final int firstDay = lastMonthDays - placeholderDays;
for (int i = 0; i < placeholderDays; i++) {
2023-07-13 14:13:44 +08:00
_datas.add(CalendarModel(
year: lastYear,
month: lastMonth,
day: firstDay + i + 1,
isSelect: false,
workType: '0'));
2023-07-13 14:13:44 +08:00
}
/// 本月显示
final int currentMonthDays = _getCurrentMonthDays(year: year, month: month);
for (int i = 0; i < currentMonthDays; i++) {
2023-07-13 14:13:44 +08:00
if (i == _day - 1) {
_datas.add(CalendarModel(
year: year,
month: month,
day: i + 1,
isSelect: true,
workType: '0'));
2023-07-13 14:13:44 +08:00
} else {
_datas.add(CalendarModel(
year: year,
month: month,
day: i + 1,
isSelect: false,
workType: '0'));
2023-07-13 14:13:44 +08:00
}
}
/// 下个月占位
int? nextYear = year;
int nextMonth = month + 1;
2023-07-13 14:13:44 +08:00
if (month == 12) {
nextYear = year! + 1;
2023-07-13 14:13:44 +08:00
nextMonth = 1;
}
final int nextPlaceholderDays =
_getPlaceholderDays(year: nextYear, month: nextMonth);
for (int i = 0; i < 7 - nextPlaceholderDays; i++) {
2023-07-13 14:13:44 +08:00
_datas.add(CalendarModel(
year: nextYear,
month: nextMonth,
day: i + 1,
isSelect: false,
workType: '0'));
2023-07-13 14:13:44 +08:00
}
}
// 上月
_lastMonth() {
setState(() {
if (_month == 1) {
_year = _year - 1;
_month = 12;
} else {
_month = _month - 1;
}
_day = 1; //查看上一个月时,默认选中的为第一天
_datas.clear();
_setDatas(year: _year, month: _month);
//更新月历事件
// _loadAttendanceMonthRecord("$_year-$_month");
2023-07-13 14:13:44 +08:00
//更新日事件
_loadAttendanceDayRecord("$_year-${_month.toString().padLeft(2,'0')}-${_day.toString().padLeft(2,'0')}");
2023-07-13 14:13:44 +08:00
});
}
// 下月
_nextMonth() {
if (_month == 12) {
//当前月是12月下一个月就是下一年
if (DateTime.now().year >= _year + 1) {
//判断下一年是否大于当前年
2023-07-13 14:13:44 +08:00
_setNextMonthData();
}
} else {
//当前月不是12月还处于当前年
if (DateTime.now().month >= _month + 1) {
2023-07-13 14:13:44 +08:00
//判断下一个月是否超过当前月,超过当前月不做操作
_setNextMonthData();
}
}
}
//设置下个月的数据
_setNextMonthData() {
2023-07-13 14:13:44 +08:00
setState(() {
if (_month == 12) {
_year = _year + 1;
_month = 1;
} else {
_month = _month + 1;
}
if (_month == DateTime.now().month) {
//如果下个月时当前月,默认选中当天
_day = DateTime.now().day;
} else {
//如果不是当前月,默认选中第一天
_day = 1;
}
_datas.clear();
_setDatas(year: _year, month: _month);
//更新月历事件
_loadAttendanceMonthRecord('$_year-$_month');
2023-07-13 14:13:44 +08:00
//更新日事件
// _loadAttendanceDayRecord("$_year-$_month-$_day");
_loadAttendanceDayRecord("$_year-${_month.toString().padLeft(2,'0')}-${_day.toString().padLeft(2,'0')}");
2023-07-13 14:13:44 +08:00
});
}
}
//日历bean
class CalendarModel {
CalendarModel(
{this.year, this.month, this.day, this.isSelect, this.workType});
2023-07-15 15:11:28 +08:00
int? year;
int? month;
int? day;
String? workType = ''; //日期事件0休息1迟到2早退3未打卡
2023-07-15 15:11:28 +08:00
bool? isSelect = false;
}