123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274 |
- import 'dart:math';
- import 'package:calendar_view/calendar_page/month_calendar/more_schedule_popup.dart';
- import 'package:calendar_view/calendar_page/month_calendar/schedule_popup.dart';
- import 'package:calendar_view/popup_layer/popup_layer_controller.dart';
- import 'package:flutter/material.dart';
- import 'package:get/get.dart';
- class PopupLayer extends StatefulWidget {
- const PopupLayer({super.key});
- @override
- State<PopupLayer> createState() => _PopupLayerState();
- }
- class _PopupLayerState extends State<PopupLayer> {
- PopupLayerController popupLayerController = Get.find<PopupLayerController>();
- bool _isShowMoreSchedulePopup = false;
- bool _isNeedCloseMoreSchedulePopup = false;
- bool _isShowScheduleDetailPopup = false;
- bool _isNeedCloseScheduleDetailPopup = false;
- GlobalKey moreSchedulePopupKey = GlobalKey();
- GlobalKey scheduleDetailPopupKey = GlobalKey();
-
- static const double _scheduleDetailPopupWidth = 380;
-
- static const double _moreSchedulePopupMinWidth = 220;
-
- static const double _popupLayerMarginTop = 70;
-
- static const double _popupLayerMarginLeft = 240;
-
- static final BoxDecoration _popupBoxDecoration = BoxDecoration(
- color: Colors.white,
- borderRadius: BorderRadius.circular(8),
- border: Border.all(color: Colors.black12, width: 1),
- boxShadow: const [
- BoxShadow(
- color: Colors.black12,
- offset: Offset(3, 3),
- blurRadius: 25,
- ),
- ],
- );
- @override
- void initState() {
- super.initState();
- popupLayerController.onPopupMoreSchedule.addListener(_onPopupMoreSchedule);
- popupLayerController.onGlobalClick.addListener(_onGlobalClick);
- popupLayerController.onPopupScheduleDatail
- .addListener(_onPopupScheduleDatail);
- }
- @override
- void dispose() {
- super.dispose();
- popupLayerController.onPopupMoreSchedule
- .removeListener(_onPopupMoreSchedule);
- popupLayerController.onGlobalClick.removeListener(_onGlobalClick);
- popupLayerController.onPopupScheduleDatail
- .removeListener(_onPopupScheduleDatail);
- }
-
- void _onPopupMoreSchedule(e, GlobalKey key) {
- setState(() {
- _isShowMoreSchedulePopup = true;
- moreSchedulePopupKey = key;
- });
- }
-
- void _onPopupScheduleDatail(e, GlobalKey key) {
- setState(() {
- _isShowScheduleDetailPopup = true;
- scheduleDetailPopupKey = key;
- });
- }
-
-
-
-
- void _onGlobalClick(e, PointerEvent event) {
- if (event is PointerDownEvent) {
- _isNeedCloseMoreSchedulePopup = true;
- _isNeedCloseScheduleDetailPopup = true;
- } else if (event is PointerUpEvent) {
- if (_isNeedCloseMoreSchedulePopup) {
- setState(() {
- _isShowMoreSchedulePopup = false;
- });
- }
- if (_isNeedCloseScheduleDetailPopup) {
- setState(() {
- _isShowScheduleDetailPopup = false;
- });
- popupLayerController.onCloseSchedulePopup.emit(this, null);
- }
- }
- }
- @override
- Widget build(BuildContext context) {
- return Stack(
- children: [
- Offstage(
- offstage: !_isShowMoreSchedulePopup,
- child: Listener(
- behavior: HitTestBehavior.deferToChild,
- onPointerUp: (event) {
- _isNeedCloseMoreSchedulePopup = false;
- },
- child: _buildMoreSchedulePopup(moreSchedulePopupKey),
- ),
- ),
- Offstage(
- offstage: !_isShowScheduleDetailPopup,
- child: Listener(
- behavior: HitTestBehavior.deferToChild,
- onPointerUp: (event) {
- setState(() {
- _isNeedCloseScheduleDetailPopup = false;
- });
- },
- child: _buildScheduleDetailPopup(scheduleDetailPopupKey),
- ),
- ),
- ],
- );
- }
-
-
- Widget _buildMoreSchedulePopup(GlobalKey trigger) {
- final Size triggerSize = getWidgetSize(trigger);
- final Offset triggerOffset = getWidgetOffset(trigger);
- return Flow(
- delegate: AutoAlignFlowDelegate(triggerOffset),
- children: [
- Container(
- decoration: _popupBoxDecoration,
- width: max(triggerSize.width, _moreSchedulePopupMinWidth),
- child: MoreSchedulePopup(
- scheduleData: popupLayerController.currMoreScheduleData,
- ),
- ),
- ],
- );
- }
-
-
- Widget _buildScheduleDetailPopup(GlobalKey trigger) {
- final Size triggerSize = getWidgetSize(trigger);
- final Offset triggerOffset = getWidgetOffset(trigger);
- return Flow(
- delegate: ScheduleAutoAlignFlowDelegate(triggerOffset, triggerSize),
- children: [
- Container(
- padding: const EdgeInsets.symmetric(horizontal: 10),
- width: _scheduleDetailPopupWidth,
- child: Container(
- decoration: _popupBoxDecoration,
- child: SchedulePopup(
- scheduleData: popupLayerController.currScheduleData,
- onClose: () {
- setState(() {
- _isShowScheduleDetailPopup = false;
- });
- popupLayerController.onCloseSchedulePopup.emit(this, null);
- },
- ),
- ),
- ),
- ],
- );
- }
-
- Size getWidgetSize(GlobalKey key) {
- try {
- final RenderBox renderBox =
- key.currentContext?.findRenderObject() as RenderBox;
- return renderBox.size;
- } catch (e) {
- print('获取大小信息失败: $e');
- return Size.zero;
- }
- }
-
- Offset getWidgetOffset(GlobalKey key) {
- try {
- final RenderBox renderBox =
- key.currentContext?.findRenderObject() as RenderBox;
- return renderBox
- .localToGlobal(Offset.zero)
- .translate(-_popupLayerMarginLeft, -_popupLayerMarginTop);
- } catch (e) {
- print('获取位置信息失败: $e');
- return Offset.zero;
- }
- }
- }
- class AutoAlignFlowDelegate extends FlowDelegate {
- final Offset triggerOffset;
- AutoAlignFlowDelegate(this.triggerOffset);
- @override
- void paintChildren(FlowPaintingContext context) {
- final Size containerSize = context.size;
- for (int i = 0; i < context.childCount; i++) {
- final Size childSize = context.getChildSize(i) ?? Size.zero;
- double x = triggerOffset.dx;
- double y = triggerOffset.dy;
- if (containerSize.height - y < childSize.height) {
- y = containerSize.height - childSize.height;
- }
- if (containerSize.width - x < childSize.width) {
- x = containerSize.width - childSize.width;
- }
- context.paintChild(i, transform: Matrix4.translationValues(x, y, 0));
- }
- }
- @override
- bool shouldRepaint(FlowDelegate oldDelegate) {
- return oldDelegate != this;
- }
- }
- class ScheduleAutoAlignFlowDelegate extends FlowDelegate {
- final Offset triggerOffset;
- final Size triggerSize;
- ScheduleAutoAlignFlowDelegate(this.triggerOffset, this.triggerSize);
- @override
- void paintChildren(FlowPaintingContext context) {
- final Size containerSize = context.size;
- for (int i = 0; i < context.childCount; i++) {
- final Size childSize = context.getChildSize(i) ?? Size.zero;
- double x = triggerOffset.dx;
- double y = triggerOffset.dy;
- if (containerSize.height - y < childSize.height) {
- y = containerSize.height - childSize.height;
- }
- if (containerSize.width - x - triggerSize.width < childSize.width) {
- x = x - childSize.width;
- if (x < 0) {
- x = 0;
- y += triggerSize.height + 10;
- if (containerSize.height - y < childSize.height) {
- y = containerSize.height - childSize.height;
- }
- }
- } else {
- x = x + triggerSize.width;
- }
- context.paintChild(i, transform: Matrix4.translationValues(x, y, 0));
- }
- }
- @override
- bool shouldRepaint(ScheduleAutoAlignFlowDelegate oldDelegate) {
- return oldDelegate != this;
- }
- }
|