123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136 |
- import 'package:calendar_view/utils/calendar_util.dart';
- import 'package:flutter/material.dart';
- import 'package:flutter/services.dart';
- /// 小日历的每一格日期
- class MiniDayItem extends StatefulWidget {
- const MiniDayItem({super.key, required this.dayData, required this.onSelect});
- final DayStructure dayData;
- final ValueChanged<int> onSelect;
- @override
- State<MiniDayItem> createState() => _MyWidgetState();
- }
- class _MyWidgetState extends State<MiniDayItem> {
- late DayStructure _dayData;
- late BoxDecoration containerDecoration;
- late TextStyle dayTextStyle;
- /// TODO[Gavin]: i18n
- get displayStr => _dayData.isToday ? '今' : _dayData.date.day.toString();
- @override
- void initState() {
- super.initState();
- _dayData = widget.dayData;
- _initStyle();
- }
- @override
- void didUpdateWidget(MiniDayItem oldWidget) {
- super.didUpdateWidget(oldWidget);
- _dayData = widget.dayData;
- _initStyle();
- }
- /// 初始化样式
- /// 存在三种背景样式,选中,未选中,“今日”未选中
- /// 存在四种文字样式,选中,未选中,非当月,“今日”未选中
- void _initStyle() {
- if (_dayData.isSelected) {
- dayTextStyle = TextStyle(
- height: _dayData.isToday ? null : 1,
- fontSize: 12,
- color: Colors.white,
- );
- containerDecoration = BoxDecoration(
- color: Colors.blue,
- borderRadius: BorderRadius.circular(15),
- );
- } else {
- if (_dayData.isToday) {
- dayTextStyle = const TextStyle(
- height: null,
- fontSize: 12,
- color: Colors.blue,
- );
- containerDecoration = BoxDecoration(
- color: const Color.fromARGB(255, 224, 241, 255),
- borderRadius: BorderRadius.circular(15),
- );
- } else {
- dayTextStyle = TextStyle(
- height: _dayData.isToday ? null : 1,
- fontSize: 12,
- color: _dayData.isCurrentMonth ? Colors.black87 : Colors.black26,
- );
- containerDecoration = const BoxDecoration(
- color: Colors.transparent,
- );
- }
- }
- }
- @override
- Widget build(BuildContext context) {
- return GestureDetector(
- onTap: () {
- widget.onSelect.call(_dayData.index);
- },
- child: MouseRegion(
- cursor: SystemMouseCursors.click,
- onEnter: _handleMouthEnter,
- onExit: _handleMouthExit,
- child: Column(
- children: [
- Container(
- decoration: containerDecoration,
- height: 25,
- width: 25,
- child: Center(
- child: Text(displayStr, style: dayTextStyle),
- ),
- ),
- const SizedBox(
- height: 2,
- ),
- _buildScheduleIndicator()
- ],
- ),
- ),
- );
- }
- /// 构建日程指示器
- Widget _buildScheduleIndicator() {
- return Container(
- height: 4,
- width: 4,
- decoration: _dayData.hasSchedule
- ? BoxDecoration(
- color: const Color.fromARGB(255, 218, 219, 220),
- borderRadius: BorderRadius.circular(2),
- )
- : null,
- );
- }
- /// 鼠标移入时,如果未选中,背景色设置为灰色
- void _handleMouthEnter(PointerEnterEvent event) {
- if (!_dayData.isSelected) {
- setState(() {
- containerDecoration = BoxDecoration(
- color: const Color.fromARGB(255, 234, 236, 237),
- borderRadius: BorderRadius.circular(15),
- );
- });
- }
- }
- /// 鼠标移出时,如果未选中,背景色设置为透明
- void _handleMouthExit(PointerExitEvent event) {
- if (!_dayData.isSelected) {
- _initStyle();
- setState(() {});
- }
- }
- }
|