schedule_item.dart 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. import 'package:calendar_view/utils/calendar_util.dart';
  2. import 'package:flutter/material.dart';
  3. class SchedualItem extends StatefulWidget {
  4. const SchedualItem(
  5. {super.key, required this.scheduleData, required this.itemHeight});
  6. final Schedule scheduleData;
  7. final double itemHeight;
  8. @override
  9. State<SchedualItem> createState() => _SchedualItemState();
  10. }
  11. class _SchedualItemState extends State<SchedualItem> {
  12. ScheduleType get scheduleType => widget.scheduleData.type;
  13. Schedule get schedule => widget.scheduleData;
  14. bool _isSelected = false; // 是否选中状态
  15. ////TODO:[Gavin] 监听一个popupController的关闭事件,关闭时将选中状态置为false
  16. @override
  17. Widget build(BuildContext context) {
  18. return Material(
  19. color: Colors.transparent,
  20. child: InkWell(
  21. customBorder: RoundedRectangleBorder(
  22. borderRadius: BorderRadius.circular(5),
  23. ),
  24. onTap: () {
  25. setState(() {
  26. _isSelected = !_isSelected;
  27. });
  28. ScaffoldMessenger.of(context).showSnackBar(
  29. SnackBar(
  30. backgroundColor: scheduleType.color,
  31. duration: const Duration(milliseconds: 500),
  32. content: Text('点击了${scheduleType.typeName}'),
  33. ),
  34. );
  35. },
  36. /// 接入双击后会导致单击反应变慢,暂时不接入
  37. // onDoubleTap: () {
  38. // ScaffoldMessenger.of(context).showSnackBar(
  39. // SnackBar(
  40. // backgroundColor: scheduleType.color,
  41. // duration: const Duration(milliseconds: 500),
  42. // content: Text('双击了${scheduleType.typeName} 等待接入打开详情'),
  43. // ),
  44. // );
  45. // },
  46. hoverColor: const Color.fromARGB(255, 227, 228, 228),
  47. child: Container(
  48. height: widget.itemHeight,
  49. padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 0),
  50. decoration: BoxDecoration(
  51. color: _isSelected ? scheduleType.color : Colors.transparent,
  52. borderRadius: BorderRadius.circular(5),
  53. ),
  54. child: Row(
  55. children: [
  56. Container(
  57. width: 6,
  58. height: 6,
  59. decoration: BoxDecoration(
  60. color: scheduleType.color,
  61. borderRadius: BorderRadius.circular(5),
  62. ),
  63. ),
  64. const SizedBox(width: 5),
  65. Text(
  66. _getStartTime(schedule.startTime),
  67. style: TextStyle(
  68. fontSize: 12,
  69. color: _isSelected
  70. ? Colors.white
  71. : const Color.fromARGB(167, 0, 0, 0),
  72. ),
  73. overflow: TextOverflow.ellipsis,
  74. ),
  75. Expanded(
  76. child: Text(
  77. breakWord(schedule.title),
  78. style: TextStyle(
  79. fontSize: 12,
  80. color: _isSelected ? Colors.white : Colors.black),
  81. overflow: TextOverflow.ellipsis,
  82. ),
  83. ),
  84. ],
  85. ),
  86. ),
  87. ),
  88. );
  89. }
  90. static String breakWord(String text) {
  91. if (text.isEmpty) {
  92. return text;
  93. }
  94. String breakWord = ' ';
  95. for (var element in text.runes) {
  96. breakWord += String.fromCharCode(element);
  97. breakWord += '\u200B';
  98. }
  99. return breakWord;
  100. }
  101. static String _getStartTime(DateTime? startTime) {
  102. if (startTime == null) {
  103. // TODO: i18n
  104. return '全天';
  105. }
  106. return '${startTime.hour}:${startTime.minute}';
  107. }
  108. }