qhd-prevention-flutter/lib/pages/home/workSet_page.dart

293 lines
8.7 KiB
Dart

import 'package:flutter/material.dart';
import 'package:qhd_prevention/pages/home/work_alert.dart';
import 'package:qhd_prevention/pages/my_appbar.dart';
import 'package:qhd_prevention/tools/tools.dart';
import 'package:table_calendar/table_calendar.dart';
import 'package:intl/intl.dart';
import '../../tools/h_colors.dart';
class WorkSetPage extends StatefulWidget {
const WorkSetPage({super.key});
@override
State<WorkSetPage> createState() => _WorkSetPageState();
}
class _WorkSetPageState extends State<WorkSetPage> {
DateTime _focusedDay = DateTime.now();
DateTime _selectedDay = DateTime.now();
void _goToToday() {
setState(() {
_focusedDay = DateTime.now();
_selectedDay = DateTime.now();
selectDate();
});
}
void _goToPrevMonth() {
setState(() {
_focusedDay = DateTime(_focusedDay.year, _focusedDay.month - 1);
});
}
void _goToNextMonth() {
setState(() {
_focusedDay = DateTime(_focusedDay.year, _focusedDay.month + 1);
});
}
void selectDate() {
print("======$_selectedDay");
showWorkAlert(
context: context,
alertTitle: "标题:",
inputHint: "请输入",
contentHint: "请输入",
onConfirm: (title, content) {
// 处理确定后的逻辑
print("标题: $title");
print("内容: $content");
},
onCancel: () {
// 处理取消逻辑
print("用户取消了操作");
},
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: h_backGroundColor(),
appBar: MyAppbar(title: "工作安排"),
body: Column(
children: [
Padding(
padding: const EdgeInsets.symmetric(vertical: 1),
child: _calendarHeader(),
),
Container(
padding: EdgeInsets.only(top: 10),
color: Colors.white,
child: TableCalendar(
locale: 'zh_CN', // 👈 设置为中文
firstDay: DateTime(2000),
lastDay: DateTime(2100),
focusedDay: _focusedDay,
selectedDayPredicate: (day) => isSameDay(_selectedDay, day),
onDaySelected: (selectedDay, focusedDay) {
setState(() {
_selectedDay = selectedDay;
_focusedDay = focusedDay;
selectDate();
});
},
daysOfWeekStyle: DaysOfWeekStyle(
dowTextFormatter: (date, locale) {
// date.weekday: 1=Mon ... 7=Sun
const weekdays = ['', '', '', '', '', '', ''];
return weekdays[date.weekday % 7]; // 0=Sun
},
weekdayStyle: const TextStyle(
fontWeight: FontWeight.bold,
color: Colors.black87,
),
weekendStyle: const TextStyle(
fontWeight: FontWeight.bold,
color: Colors.black45,
),
),
calendarFormat: CalendarFormat.month,
startingDayOfWeek: StartingDayOfWeek.sunday,
headerVisible: false,
enabledDayPredicate: (day) {
// 禁用非当前月份日期
return day.month == _focusedDay.month;
},
calendarStyle: const CalendarStyle(
outsideTextStyle: TextStyle(color: Colors.grey),
outsideDecoration: BoxDecoration(
color: Colors.transparent,
shape: BoxShape.rectangle,
),
todayDecoration: BoxDecoration(
color: Colors.transparent, // 不再使用默认 today 样式
),
selectedDecoration: BoxDecoration(
color: Colors.blue,
shape: BoxShape.circle,
),
),
calendarBuilders: CalendarBuilders(
defaultBuilder: (context, day, focusedDay) {
return _buildDayCell(day);
},
outsideBuilder: (context, day, focusedDay) {
return _buildDayCell(day, isOutside: true);
},
todayBuilder: (context, day, focusedDay) {
return _buildDayCell(day);
},
selectedBuilder: (context, day, focusedDay) {
return _buildDayCell(day, isSelected: true);
},
),
),
),
Container(color: h_backGroundColor(), height: 20),
_workTipWidget(),
],
),
);
}
/// 本日工作题型
Widget _workTipWidget() {
return Container(
color: Colors.white,
padding: EdgeInsets.all(12),
child: Column(
spacing: 20,
children: [
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text("本日工作题型", style: TextStyle(fontSize: 16)),
Spacer(),
],
),
Column(
spacing: 5,
children: [
_itemCell("需进行2项隐患排查", true),
_itemCell("需进行2项隐患排查", false),
],
),
],
),
);
}
Widget _itemCell(final String title, bool isFinish) {
return Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Row(
spacing: 5,
children: [
Icon(Icons.circle, size: 10, color: Colors.blue),
Text(title, style: TextStyle(color: Colors.black45),),
],
),
Text(isFinish?"已完成":"未完成", style: TextStyle(color: isFinish?Colors.green:Colors.red)),
],
);
}
///日历头
Widget _calendarHeader() {
return Container(
color: Colors.white,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
// 占位
const Spacer(),
const Spacer(),
// 中间内容:← 年月 →
Row(
mainAxisSize: MainAxisSize.min,
children: [
IconButton(
icon: const Icon(Icons.chevron_left),
onPressed: _goToPrevMonth,
padding: EdgeInsets.zero,
constraints: const BoxConstraints(),
),
const SizedBox(width: 4),
Text(
DateFormat('yyyy年MM月').format(_focusedDay),
style: const TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
),
const SizedBox(width: 4),
IconButton(
icon: const Icon(Icons.chevron_right),
onPressed: _goToNextMonth,
padding: EdgeInsets.zero,
constraints: const BoxConstraints(),
),
],
),
const Spacer(),
Container(
height: 24,
decoration: BoxDecoration(
color: h_backGroundColor(),
borderRadius: const BorderRadius.only(
topLeft: Radius.circular(12),
bottomLeft: Radius.circular(12),
),
),
child: TextButton(
onPressed: _goToToday,
style: TextButton.styleFrom(
padding: const EdgeInsets.symmetric(horizontal: 7),
minimumSize: Size.zero,
tapTargetSize: MaterialTapTargetSize.shrinkWrap,
),
child: const Text("回到今天", style: TextStyle(fontSize: 11)),
),
),
],
),
);
}
Widget _buildDayCell(
DateTime day, {
bool isSelected = false,
bool isOutside = false,
}) {
final bool isToday = isSameDay(day, DateTime.now());
final bool selected = isSameDay(day, _selectedDay);
Color textColor;
if (selected) {
textColor = Colors.white;
} else if (isOutside) {
textColor = Colors.grey;
} else {
textColor = Colors.black87;
}
return Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Container(
width: 35,
height: 35,
alignment: Alignment.center,
decoration: BoxDecoration(
color: selected ? Colors.blue : Colors.transparent,
shape: BoxShape.circle,
),
child: Text(
'${day.day}',
style: TextStyle(color: textColor, fontWeight: FontWeight.w500),
),
),
if (isToday)
Text(
"今天",
style: TextStyle(
fontSize: 12,
color: selected ? Colors.white : Colors.blue,
),
),
],
);
}
}