schedule_item.dart 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. import 'package:calendar_view/popup_layer/popup_layer_controller.dart';
  2. import 'package:calendar_view/utils/calendar_util.dart';
  3. import 'package:calendar_view/utils/event_type.dart';
  4. import 'package:flutter/material.dart';
  5. import 'package:get/get.dart';
  6. class SchedualItem extends StatefulWidget {
  7. const SchedualItem({
  8. super.key,
  9. required this.scheduleData,
  10. required this.itemHeight,
  11. this.onTapShowSchedule,
  12. });
  13. final Schedule scheduleData;
  14. final double itemHeight;
  15. final ValueCallback? onTapShowSchedule;
  16. @override
  17. State<SchedualItem> createState() => _SchedualItemState();
  18. }
  19. class _SchedualItemState extends State<SchedualItem> {
  20. ScheduleType get scheduleType => widget.scheduleData.type;
  21. Schedule get schedule => widget.scheduleData;
  22. bool _isSelected = false; // 是否选中状态
  23. GlobalKey _scheduleListKey = GlobalKey();
  24. final PopupLayerController _popupLayerController =
  25. Get.find<PopupLayerController>();
  26. @override
  27. void initState() {
  28. super.initState();
  29. _popupLayerController.onCloseSchedulePopup.addListener(_onGlobalClick);
  30. }
  31. @override
  32. void dispose() {
  33. super.dispose();
  34. _popupLayerController.onCloseSchedulePopup.removeListener(_onGlobalClick);
  35. }
  36. void _onGlobalClick(e, bool isClose) {
  37. if (!mounted) return;
  38. print(isClose);
  39. setState(() {
  40. _isSelected = isClose;
  41. });
  42. }
  43. ////TODO:[Gavin] 监听一个popupController的关闭事件,关闭时将选中状态置为false
  44. @override
  45. Widget build(BuildContext context) {
  46. return Material(
  47. color: Colors.transparent,
  48. child: InkWell(
  49. key: _scheduleListKey,
  50. customBorder: RoundedRectangleBorder(
  51. borderRadius: BorderRadius.circular(5),
  52. ),
  53. onTap: () {
  54. setState(() {
  55. _isSelected = !_isSelected;
  56. });
  57. widget.onTapShowSchedule!(_scheduleListKey);
  58. },
  59. hoverColor: const Color.fromARGB(255, 227, 228, 228),
  60. child: Container(
  61. height: widget.itemHeight,
  62. padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 0),
  63. decoration: BoxDecoration(
  64. color: _isSelected ? scheduleType.color : Colors.transparent,
  65. borderRadius: BorderRadius.circular(5),
  66. ),
  67. child: Row(
  68. children: [
  69. Container(
  70. width: 6,
  71. height: 6,
  72. decoration: BoxDecoration(
  73. color: scheduleType.color,
  74. borderRadius: BorderRadius.circular(5),
  75. ),
  76. ),
  77. const SizedBox(width: 5),
  78. Text(
  79. _getStartTime(schedule.startTime),
  80. style: TextStyle(
  81. fontSize: 12,
  82. color: _isSelected
  83. ? Colors.white
  84. : const Color.fromARGB(167, 0, 0, 0),
  85. ),
  86. overflow: TextOverflow.ellipsis,
  87. ),
  88. Expanded(
  89. child: Text(
  90. breakWord(schedule.title),
  91. style: TextStyle(
  92. fontSize: 12,
  93. color: _isSelected ? Colors.white : Colors.black),
  94. overflow: TextOverflow.ellipsis,
  95. ),
  96. ),
  97. ],
  98. ),
  99. ),
  100. ),
  101. );
  102. }
  103. static String breakWord(String text) {
  104. if (text.isEmpty) {
  105. return text;
  106. }
  107. String breakWord = ' ';
  108. for (var element in text.runes) {
  109. breakWord += String.fromCharCode(element);
  110. breakWord += '\u200B';
  111. }
  112. return breakWord;
  113. }
  114. static String _getStartTime(DateTime? startTime) {
  115. if (startTime == null) {
  116. // TODO: i18n
  117. return '全天';
  118. }
  119. return '${startTime.hour}:${startTime.minute}';
  120. }
  121. }