popup_layer.dart 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. import 'dart:math';
  2. import 'package:calendar_view/calendar_page/month_calendar/more_schedule_popup.dart';
  3. import 'package:calendar_view/calendar_page/month_calendar/schedule_popup.dart';
  4. import 'package:calendar_view/popup_layer/popup_layer_controller.dart';
  5. import 'package:flutter/material.dart';
  6. import 'package:get/get.dart';
  7. class PopupLayer extends StatefulWidget {
  8. const PopupLayer({super.key});
  9. @override
  10. State<PopupLayer> createState() => _PopupLayerState();
  11. }
  12. class _PopupLayerState extends State<PopupLayer> {
  13. PopupLayerController popupLayerController = Get.find<PopupLayerController>();
  14. bool _isShowMoreSchedulePopup = false;
  15. bool _isNeedCloseMoreSchedulePopup = false;
  16. bool _isShowScheduleDetailPopup = false;
  17. bool _isNeedCloseScheduleDetailPopup = false;
  18. GlobalKey moreSchedulePopupKey = GlobalKey();
  19. GlobalKey scheduleDetailPopupKey = GlobalKey();
  20. /// 日程详情弹出框宽度
  21. static const double _scheduleDetailPopupWidth = 380;
  22. /// 完成日程列表弹出框最小推荐宽度
  23. static const double _moreSchedulePopupMinWidth = 220;
  24. /// 固定的头部高度
  25. static const double _popupLayerMarginTop = 70;
  26. /// 固定的左侧宽度
  27. static const double _popupLayerMarginLeft = 240;
  28. /// 弹出框装饰器
  29. static final BoxDecoration _popupBoxDecoration = BoxDecoration(
  30. color: Colors.white,
  31. borderRadius: BorderRadius.circular(8),
  32. border: Border.all(color: Colors.black12, width: 1),
  33. boxShadow: const [
  34. BoxShadow(
  35. color: Colors.black12,
  36. offset: Offset(3, 3),
  37. blurRadius: 25,
  38. ),
  39. ],
  40. );
  41. @override
  42. void initState() {
  43. super.initState();
  44. popupLayerController.onPopupMoreSchedule.addListener(_onPopupMoreSchedule);
  45. popupLayerController.onGlobalClick.addListener(_onGlobalClick);
  46. popupLayerController.onPopupScheduleDatail
  47. .addListener(_onPopupScheduleDatail);
  48. }
  49. @override
  50. void dispose() {
  51. super.dispose();
  52. popupLayerController.onPopupMoreSchedule
  53. .removeListener(_onPopupMoreSchedule);
  54. popupLayerController.onGlobalClick.removeListener(_onGlobalClick);
  55. popupLayerController.onPopupScheduleDatail
  56. .removeListener(_onPopupScheduleDatail);
  57. }
  58. /// 收到显示更多日程的事件
  59. void _onPopupMoreSchedule(e, GlobalKey key) {
  60. setState(() {
  61. _isShowMoreSchedulePopup = true;
  62. moreSchedulePopupKey = key;
  63. });
  64. }
  65. /// 收到显示日程详情的事件
  66. void _onPopupScheduleDatail(e, GlobalKey key) {
  67. setState(() {
  68. _isShowScheduleDetailPopup = true;
  69. scheduleDetailPopupKey = key;
  70. });
  71. }
  72. /// 收到全局点击事件
  73. /// 全局按下时设置 _needClosePopup 为 true
  74. /// 弹层组件收到 onPointerUp 时设置 _needClosePopup 为 false
  75. /// 全局抬起时判断 _needClosePopup 是否为 true,为 true 则关闭弹层
  76. void _onGlobalClick(e, PointerEvent event) {
  77. if (event is PointerDownEvent) {
  78. _isNeedCloseMoreSchedulePopup = true;
  79. _isNeedCloseScheduleDetailPopup = true;
  80. } else if (event is PointerUpEvent) {
  81. if (_isNeedCloseMoreSchedulePopup) {
  82. setState(() {
  83. _isShowMoreSchedulePopup = false;
  84. });
  85. }
  86. if (_isNeedCloseScheduleDetailPopup) {
  87. setState(() {
  88. _isShowScheduleDetailPopup = false;
  89. });
  90. popupLayerController.onCloseSchedulePopup.emit(this, null);
  91. }
  92. }
  93. }
  94. @override
  95. Widget build(BuildContext context) {
  96. return Stack(
  97. children: [
  98. Offstage(
  99. offstage: !_isShowMoreSchedulePopup,
  100. child: Listener(
  101. behavior: HitTestBehavior.deferToChild,
  102. onPointerUp: (event) {
  103. _isNeedCloseMoreSchedulePopup = false;
  104. },
  105. child: _buildMoreSchedulePopup(moreSchedulePopupKey),
  106. ),
  107. ),
  108. Offstage(
  109. offstage: !_isShowScheduleDetailPopup,
  110. child: Listener(
  111. behavior: HitTestBehavior.deferToChild,
  112. onPointerUp: (event) {
  113. setState(() {
  114. _isNeedCloseScheduleDetailPopup = false;
  115. });
  116. },
  117. child: _buildScheduleDetailPopup(scheduleDetailPopupKey),
  118. ),
  119. ),
  120. ],
  121. );
  122. }
  123. /// 只做容器而无需负责内容
  124. /// [trigger] 触发器的 key
  125. Widget _buildMoreSchedulePopup(GlobalKey trigger) {
  126. final Size triggerSize = getWidgetSize(trigger);
  127. final Offset triggerOffset = getWidgetOffset(trigger);
  128. return Flow(
  129. delegate: AutoAlignFlowDelegate(triggerOffset),
  130. children: [
  131. Container(
  132. decoration: _popupBoxDecoration,
  133. width: max(triggerSize.width, _moreSchedulePopupMinWidth),
  134. child: MoreSchedulePopup(
  135. scheduleData: popupLayerController.currMoreScheduleData,
  136. ),
  137. ),
  138. ],
  139. );
  140. }
  141. /// 只做容器而无需负责内容
  142. /// [trigger] 触发器的 key
  143. Widget _buildScheduleDetailPopup(GlobalKey trigger) {
  144. final Size triggerSize = getWidgetSize(trigger);
  145. final Offset triggerOffset = getWidgetOffset(trigger);
  146. return Flow(
  147. delegate: ScheduleAutoAlignFlowDelegate(triggerOffset, triggerSize),
  148. children: [
  149. Container(
  150. padding: const EdgeInsets.symmetric(horizontal: 10),
  151. width: _scheduleDetailPopupWidth,
  152. child: Container(
  153. decoration: _popupBoxDecoration,
  154. child: SchedulePopup(
  155. scheduleData: popupLayerController.currScheduleData,
  156. onClose: () {
  157. setState(() {
  158. _isShowScheduleDetailPopup = false;
  159. });
  160. popupLayerController.onCloseSchedulePopup.emit(this, null);
  161. },
  162. ),
  163. ),
  164. ),
  165. ],
  166. );
  167. }
  168. /// 通过组件的 key 获取对应的组件的大小信息
  169. Size getWidgetSize(GlobalKey key) {
  170. try {
  171. final RenderBox renderBox =
  172. key.currentContext?.findRenderObject() as RenderBox;
  173. return renderBox.size;
  174. } catch (e) {
  175. print('获取大小信息失败: $e');
  176. return Size.zero;
  177. }
  178. }
  179. /// 通过组件的 key 获取对应的组件在弹出层的位置
  180. Offset getWidgetOffset(GlobalKey key) {
  181. try {
  182. final RenderBox renderBox =
  183. key.currentContext?.findRenderObject() as RenderBox;
  184. return renderBox
  185. .localToGlobal(Offset.zero)
  186. .translate(-_popupLayerMarginLeft, -_popupLayerMarginTop);
  187. } catch (e) {
  188. print('获取位置信息失败: $e');
  189. return Offset.zero;
  190. }
  191. }
  192. }
  193. /// 自动对齐委托
  194. class AutoAlignFlowDelegate extends FlowDelegate {
  195. final Offset triggerOffset;
  196. AutoAlignFlowDelegate(this.triggerOffset);
  197. @override
  198. void paintChildren(FlowPaintingContext context) {
  199. final Size containerSize = context.size;
  200. for (int i = 0; i < context.childCount; i++) {
  201. final Size childSize = context.getChildSize(i) ?? Size.zero;
  202. double x = triggerOffset.dx;
  203. double y = triggerOffset.dy;
  204. if (containerSize.height - y < childSize.height) {
  205. y = containerSize.height - childSize.height;
  206. }
  207. if (containerSize.width - x < childSize.width) {
  208. x = containerSize.width - childSize.width;
  209. }
  210. context.paintChild(i, transform: Matrix4.translationValues(x, y, 0));
  211. }
  212. }
  213. @override
  214. bool shouldRepaint(FlowDelegate oldDelegate) {
  215. return oldDelegate != this;
  216. }
  217. }
  218. /// 日程详情框自动对齐委托
  219. class ScheduleAutoAlignFlowDelegate extends FlowDelegate {
  220. final Offset triggerOffset;
  221. final Size triggerSize;
  222. ScheduleAutoAlignFlowDelegate(this.triggerOffset, this.triggerSize);
  223. @override
  224. void paintChildren(FlowPaintingContext context) {
  225. final Size containerSize = context.size;
  226. for (int i = 0; i < context.childCount; i++) {
  227. final Size childSize = context.getChildSize(i) ?? Size.zero;
  228. double x = triggerOffset.dx;
  229. double y = triggerOffset.dy;
  230. if (containerSize.height - y < childSize.height) {
  231. y = containerSize.height - childSize.height;
  232. }
  233. if (containerSize.width - x - triggerSize.width < childSize.width) {
  234. x = x - childSize.width;
  235. if (x < 0) {
  236. x = 0;
  237. y += triggerSize.height + 10;
  238. if (containerSize.height - y < childSize.height) {
  239. y = containerSize.height - childSize.height;
  240. }
  241. }
  242. } else {
  243. x = x + triggerSize.width;
  244. }
  245. context.paintChild(i, transform: Matrix4.translationValues(x, y, 0));
  246. }
  247. }
  248. @override
  249. bool shouldRepaint(ScheduleAutoAlignFlowDelegate oldDelegate) {
  250. return oldDelegate != this;
  251. }
  252. }