schedule_item.dart 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. hoverColor: const Color.fromARGB(255, 227, 228, 228),
  37. child: Container(
  38. height: widget.itemHeight,
  39. padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 0),
  40. decoration: BoxDecoration(
  41. color: _isSelected ? scheduleType.color : Colors.transparent,
  42. borderRadius: BorderRadius.circular(5),
  43. ),
  44. child: Row(
  45. children: [
  46. Container(
  47. width: 6,
  48. height: 6,
  49. decoration: BoxDecoration(
  50. color: scheduleType.color,
  51. borderRadius: BorderRadius.circular(5),
  52. ),
  53. ),
  54. const SizedBox(width: 5),
  55. Text(
  56. _getStartTime(schedule.startTime),
  57. style: TextStyle(
  58. fontSize: 12,
  59. color: _isSelected
  60. ? Colors.white
  61. : const Color.fromARGB(167, 0, 0, 0),
  62. ),
  63. overflow: TextOverflow.ellipsis,
  64. ),
  65. Expanded(
  66. child: Text(
  67. breakWord(schedule.title),
  68. style: TextStyle(
  69. fontSize: 12,
  70. color: _isSelected ? Colors.white : Colors.black),
  71. overflow: TextOverflow.ellipsis,
  72. ),
  73. ),
  74. ],
  75. ),
  76. ),
  77. ),
  78. );
  79. }
  80. static String breakWord(String text) {
  81. if (text.isEmpty) {
  82. return text;
  83. }
  84. String breakWord = ' ';
  85. for (var element in text.runes) {
  86. breakWord += String.fromCharCode(element);
  87. breakWord += '\u200B';
  88. }
  89. return breakWord;
  90. }
  91. static String _getStartTime(DateTime? startTime) {
  92. if (startTime == null) {
  93. // TODO: i18n
  94. return '全天';
  95. }
  96. return '${startTime.hour}:${startTime.minute}';
  97. }
  98. }