|
| 1 | +import 'package:box_transform/box_transform.dart'; |
| 2 | +import 'package:flutter/material.dart'; |
| 3 | + |
| 4 | +import 'typedefs.dart'; |
| 5 | + |
| 6 | +/// Creates a new corner handle widget, with its appropriate gesture splash |
| 7 | +/// zone. |
| 8 | +@protected |
| 9 | +class CornerHandleWidget extends StatelessWidget { |
| 10 | + /// The position of the handle. |
| 11 | + final HandlePosition handlePosition; |
| 12 | + |
| 13 | + /// The builder that is used to build the handle widget. |
| 14 | + final HandleBuilder builder; |
| 15 | + |
| 16 | + /// The size of the handle's gesture response area. |
| 17 | + final double handleTapSize; |
| 18 | + |
| 19 | + /// Called when the handle dragging starts. |
| 20 | + final GestureDragStartCallback? onPanStart; |
| 21 | + |
| 22 | + /// Called when the handle dragging is updated. |
| 23 | + final GestureDragUpdateCallback? onPanUpdate; |
| 24 | + |
| 25 | + /// Called when the handle dragging ends. |
| 26 | + final GestureDragEndCallback? onPanEnd; |
| 27 | + |
| 28 | + /// Called when the handle dragging is canceled. |
| 29 | + final GestureDragCancelCallback? onPanCancel; |
| 30 | + |
| 31 | + /// Whether the handle is resizable. |
| 32 | + final bool enabled; |
| 33 | + |
| 34 | + /// Creates a new handle widget. |
| 35 | + CornerHandleWidget({ |
| 36 | + super.key, |
| 37 | + required this.handlePosition, |
| 38 | + required this.handleTapSize, |
| 39 | + required this.builder, |
| 40 | + this.onPanStart, |
| 41 | + this.onPanUpdate, |
| 42 | + this.onPanEnd, |
| 43 | + this.onPanCancel, |
| 44 | + required this.enabled, |
| 45 | + }) : assert(handlePosition.isDiagonal, 'A corner handle must be diagonal.'); |
| 46 | + |
| 47 | + @override |
| 48 | + Widget build(BuildContext context) { |
| 49 | + Widget child = builder(context, handlePosition); |
| 50 | + if (enabled) { |
| 51 | + child = GestureDetector( |
| 52 | + behavior: HitTestBehavior.opaque, |
| 53 | + onPanStart: onPanStart, |
| 54 | + onPanUpdate: onPanUpdate, |
| 55 | + onPanEnd: onPanEnd, |
| 56 | + onPanCancel: onPanCancel, |
| 57 | + child: MouseRegion( |
| 58 | + cursor: getCursorForHandle(handlePosition), |
| 59 | + child: child, |
| 60 | + ), |
| 61 | + ); |
| 62 | + } |
| 63 | + |
| 64 | + return Positioned( |
| 65 | + left: handlePosition.influencesLeft ? 0 : null, |
| 66 | + right: handlePosition.influencesRight ? 0 : null, |
| 67 | + top: handlePosition.influencesTop ? 0 : null, |
| 68 | + bottom: handlePosition.influencesBottom ? 0 : null, |
| 69 | + width: handleTapSize, |
| 70 | + height: handleTapSize, |
| 71 | + child: child, |
| 72 | + ); |
| 73 | + } |
| 74 | + |
| 75 | + /// Returns the cursor for the given handle position. |
| 76 | + MouseCursor getCursorForHandle(HandlePosition handle) { |
| 77 | + switch (handle) { |
| 78 | + case HandlePosition.topLeft: |
| 79 | + case HandlePosition.bottomRight: |
| 80 | + return SystemMouseCursors.resizeUpLeftDownRight; |
| 81 | + case HandlePosition.topRight: |
| 82 | + case HandlePosition.bottomLeft: |
| 83 | + return SystemMouseCursors.resizeUpRightDownLeft; |
| 84 | + default: |
| 85 | + throw Exception('Invalid handle position.'); |
| 86 | + } |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +/// Creates a new cardinal handle widget, with its appropriate gesture splash |
| 91 | +/// zone. |
| 92 | +@protected |
| 93 | +class SideHandleWidget extends StatelessWidget { |
| 94 | + /// The position of the handle. |
| 95 | + final HandlePosition handlePosition; |
| 96 | + |
| 97 | + /// The builder that is used to build the handle widget. |
| 98 | + final HandleBuilder builder; |
| 99 | + |
| 100 | + /// The thickness of the handle that is used for gesture detection. |
| 101 | + final double handleTapSize; |
| 102 | + |
| 103 | + /// Called when the handle dragging starts. |
| 104 | + final GestureDragStartCallback? onPanStart; |
| 105 | + |
| 106 | + /// Called when the handle dragging is updated. |
| 107 | + final GestureDragUpdateCallback? onPanUpdate; |
| 108 | + |
| 109 | + /// Called when the handle dragging ends. |
| 110 | + final GestureDragEndCallback? onPanEnd; |
| 111 | + |
| 112 | + /// Called when the handle dragging is canceled. |
| 113 | + final GestureDragCancelCallback? onPanCancel; |
| 114 | + |
| 115 | + /// Whether the handle is resizable. |
| 116 | + final bool enabled; |
| 117 | + |
| 118 | + /// Creates a new handle widget. |
| 119 | + SideHandleWidget({ |
| 120 | + super.key, |
| 121 | + required this.handlePosition, |
| 122 | + required this.handleTapSize, |
| 123 | + required this.builder, |
| 124 | + this.onPanStart, |
| 125 | + this.onPanUpdate, |
| 126 | + this.onPanEnd, |
| 127 | + this.onPanCancel, |
| 128 | + required this.enabled, |
| 129 | + }) : assert(handlePosition.isSide, 'A cardinal handle must be cardinal.'); |
| 130 | + |
| 131 | + @override |
| 132 | + Widget build(BuildContext context) { |
| 133 | + Widget child = builder(context, handlePosition); |
| 134 | + if (enabled) { |
| 135 | + child = GestureDetector( |
| 136 | + behavior: HitTestBehavior.opaque, |
| 137 | + onPanStart: onPanStart, |
| 138 | + onPanUpdate: onPanUpdate, |
| 139 | + onPanEnd: onPanEnd, |
| 140 | + onPanCancel: onPanCancel, |
| 141 | + child: MouseRegion( |
| 142 | + cursor: getCursorForHandle(handlePosition), |
| 143 | + child: child, |
| 144 | + ), |
| 145 | + ); |
| 146 | + } |
| 147 | + |
| 148 | + return Positioned( |
| 149 | + left: handlePosition.isVertical |
| 150 | + ? handleTapSize |
| 151 | + : handlePosition.influencesLeft |
| 152 | + ? 0 |
| 153 | + : null, |
| 154 | + right: handlePosition.isVertical |
| 155 | + ? handleTapSize |
| 156 | + : handlePosition.influencesRight |
| 157 | + ? 0 |
| 158 | + : null, |
| 159 | + top: handlePosition.isHorizontal |
| 160 | + ? handleTapSize |
| 161 | + : handlePosition.influencesTop |
| 162 | + ? 0 |
| 163 | + : null, |
| 164 | + bottom: handlePosition.isHorizontal |
| 165 | + ? handleTapSize |
| 166 | + : handlePosition.influencesBottom |
| 167 | + ? 0 |
| 168 | + : null, |
| 169 | + width: handlePosition.isHorizontal ? handleTapSize : null, |
| 170 | + height: handlePosition.isVertical ? handleTapSize : null, |
| 171 | + child: child, |
| 172 | + ); |
| 173 | + } |
| 174 | + |
| 175 | + /// Returns the cursor for the given handle position. |
| 176 | + MouseCursor getCursorForHandle(HandlePosition handle) { |
| 177 | + switch (handle) { |
| 178 | + case HandlePosition.left: |
| 179 | + case HandlePosition.right: |
| 180 | + return SystemMouseCursors.resizeLeftRight; |
| 181 | + case HandlePosition.top: |
| 182 | + case HandlePosition.bottom: |
| 183 | + return SystemMouseCursors.resizeUpDown; |
| 184 | + default: |
| 185 | + throw Exception('Invalid handle position.'); |
| 186 | + } |
| 187 | + } |
| 188 | +} |
0 commit comments