123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156 |
- import 'package:calendar_view/utils/calendar_util.dart';
- import 'package:calendar_view/utils/chinese_calendar_utils.dart';
- import 'package:calendar_view/calendar_view/month_calendar/schedule_list.dart';
- import 'package:flutter/material.dart';
- /// 月历的每一格日期
- class MonthDayItem extends StatefulWidget {
- const MonthDayItem(
- {super.key,
- required this.dayData,
- // required this.onSelect,
- required this.maxScheduleLines});
- final DayStructure dayData;
- // final ValueChanged<int> onSelect;
- final int maxScheduleLines;
- /// 最大能容纳的日程条数
- @override
- State<MonthDayItem> createState() => _MyWidgetState();
- }
- class _MyWidgetState extends State<MonthDayItem> {
- late DayStructure _dayData;
- late BoxDecoration _dateContainerDecoration;
- BoxDecoration _gridContainerDecoration = const BoxDecoration(
- color: Colors.white,
- );
- late TextStyle _dayTextStyle;
- late TextStyle _lunarDayTextStyle;
- /// TODO[Gavin]: i18n
- get displayStr => _dayData.isToday ? '今' : _dayData.date.day.toString();
- @override
- void initState() {
- super.initState();
- _dayData = widget.dayData;
- _initStyle();
- }
- @override
- void didUpdateWidget(MonthDayItem oldWidget) {
- super.didUpdateWidget(oldWidget);
- _dayData = widget.dayData;
- _initStyle();
- }
- /// 初始化样式
- /// 存在三种背景样式,选中,未选中,“今日”未选中
- /// 存在四种文字样式,选中,未选中,非当月,“今日”未选中
- void _initStyle() {
- if (_dayData.isSelected) {
- _dayTextStyle = TextStyle(
- height: _dayData.isToday ? null : 1,
- fontSize: 14,
- color: Colors.white,
- );
- _dateContainerDecoration = BoxDecoration(
- color: Colors.blue,
- borderRadius: BorderRadius.circular(15),
- );
- _gridContainerDecoration = const BoxDecoration(
- color: Color.fromARGB(255, 226, 241, 254),
- );
- _lunarDayTextStyle = _dayTextStyle.copyWith(
- fontSize: 11,
- color: Colors.blue,
- );
- } else {
- if (_dayData.isToday) {
- _dayTextStyle = const TextStyle(
- height: null,
- fontSize: 14,
- color: Colors.blue,
- );
- _dateContainerDecoration = BoxDecoration(
- color: const Color.fromARGB(255, 224, 241, 255),
- borderRadius: BorderRadius.circular(15),
- );
- _gridContainerDecoration = const BoxDecoration(
- color: Color.fromARGB(255, 248, 248, 248),
- );
- _lunarDayTextStyle = _dayTextStyle.copyWith(
- color: Colors.black87,
- fontSize: 11,
- );
- } else {
- _dayTextStyle = TextStyle(
- height: _dayData.isToday ? null : 1,
- fontSize: 14,
- color: _dayData.isCurrentMonth ? Colors.black87 : Colors.black26,
- );
- _dateContainerDecoration = const BoxDecoration(
- color: Colors.transparent,
- );
- _gridContainerDecoration = const BoxDecoration(
- color: Colors.white,
- );
- _lunarDayTextStyle = _dayTextStyle.copyWith(
- fontSize: 11,
- );
- }
- }
- }
- @override
- Widget build(BuildContext context) {
- return Container(
- decoration: _gridContainerDecoration,
- child: Column(
- children: [
- Row(
- mainAxisAlignment: MainAxisAlignment.center,
- children: [
- Container(
- decoration: _dateContainerDecoration,
- margin: const EdgeInsets.symmetric(vertical: 3),
- height: 24,
- width: 24,
- child: Center(
- child: Text(displayStr, style: _dayTextStyle),
- ),
- ),
- Container(
- // decoration: _dateContainerDecoration,
- margin: const EdgeInsets.all(3),
- height: 24,
- child: Center(
- child: Text(_getLunarDay(), style: _lunarDayTextStyle),
- ),
- ),
- ],
- ),
- _buildScheduleContainer()
- ],
- ),
- );
- }
- /// 构建日程容器
- Widget _buildScheduleContainer() {
- return Expanded(
- // ignore: sized_box_for_whitespace
- child: Container(
- width: double.infinity,
- child: ScheduleList(maxLines: widget.maxScheduleLines),
- ),
- );
- }
- /// 获取节气和农历日
- String _getLunarDay() {
- DateTime date = _dayData.date;
- CalendarInfo lunarDate = CalendarUtils.getInfo(date);
- return lunarDate.term ?? lunarDate.lunarDayName ?? '';
- }
- }
|