Skip to content

Commit cab5adb

Browse files
committed
♻️ refactor transformable_box.dart file
1 parent 174bbce commit cab5adb

File tree

7 files changed

+322
-292
lines changed

7 files changed

+322
-292
lines changed

packages/flutter_box_transform/lib/flutter_box_transform.dart

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@ library flutter_box_transform;
22

33
export 'package:box_transform/box_transform.dart';
44

5-
export 'src/extensions.dart' hide DimensionExt;
6-
export 'src/handle.dart';
5+
export 'src/extensions.dart';
6+
export 'src/handles.dart';
77
export 'src/transformable_box.dart';
88
export 'src/transformable_box_controller.dart';
9+
export 'src/typedefs.dart';
910
export 'src/ui_box_transform.dart';
1011
export 'src/ui_result.dart';
Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
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+
}

packages/flutter_box_transform/lib/src/handle.dart renamed to packages/flutter_box_transform/lib/src/handles.dart

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import 'dart:math';
22

3+
import 'package:box_transform/box_transform.dart';
34
import 'package:flutter/material.dart';
45

5-
import '../flutter_box_transform.dart';
6-
76
/// Default width of the border of the handles.
87
const kDefaultHandleBorderWidth = 1.5;
98

0 commit comments

Comments
 (0)