Skip to content

Commit 7f513ec

Browse files
SaadArdatiBirjuVachhani
authored andcommitted
🔧 Refactor all resize functions into resize handler classes.
1 parent f40d751 commit 7f513ec

File tree

7 files changed

+1059
-1077
lines changed

7 files changed

+1059
-1077
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
part of 'resize_handler.dart';
2+
3+
/// Handles resizing for [ResizeMode.freeform].
4+
final class FreeformResizeHandler extends ResizeHandler {
5+
/// A default constructor for [FreeformResizeHandler].
6+
const FreeformResizeHandler();
7+
8+
@override
9+
(Box rect, Box largest, bool hasValidFlip) resize({
10+
required Box initialRect,
11+
required Box explodedRect,
12+
required Box clampingRect,
13+
required HandlePosition handle,
14+
required Constraints constraints,
15+
required Flip flip,
16+
}) {
17+
final flippedHandle = handle.flip(flip);
18+
Box effectiveInitialRect = flipBox(initialRect, flip, handle);
19+
20+
Box newRect = Box.fromLTRB(
21+
max(explodedRect.left, clampingRect.left),
22+
max(explodedRect.top, clampingRect.top),
23+
min(explodedRect.right, clampingRect.right),
24+
min(explodedRect.bottom, clampingRect.bottom),
25+
);
26+
27+
bool isValid = true;
28+
if (!constraints.isUnconstrained) {
29+
final constrainedWidth =
30+
newRect.width.clamp(constraints.minWidth, constraints.maxWidth);
31+
final constrainedHeight =
32+
newRect.height.clamp(constraints.minHeight, constraints.maxHeight);
33+
34+
newRect = Box.fromHandle(
35+
flippedHandle.anchor(effectiveInitialRect),
36+
flippedHandle,
37+
constrainedWidth,
38+
constrainedHeight,
39+
);
40+
41+
isValid = isValidBox(newRect, constraints, clampingRect);
42+
if (!isValid) {
43+
newRect = Box.fromHandle(
44+
handle.anchor(initialRect),
45+
handle,
46+
!handle.isSide || handle.isHorizontal
47+
? constraints.minWidth
48+
: constrainedWidth,
49+
!handle.isSide || handle.isVertical
50+
? constraints.minHeight
51+
: constrainedHeight,
52+
);
53+
}
54+
}
55+
56+
// Not used but calculating it for returning correct largest box.
57+
final Box area = getAvailableAreaForHandle(
58+
rect: isValid ? effectiveInitialRect : initialRect,
59+
handle: isValid ? flippedHandle : handle,
60+
clampingRect: clampingRect,
61+
);
62+
63+
return (newRect, area, isValid);
64+
}
65+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
library resize_handlers;
2+
3+
import 'dart:math';
4+
5+
import '../../box_transform.dart';
6+
7+
part 'freeform_resizing.dart';
8+
part 'symmetric_resizing.dart';
9+
part 'symmetric_scale_resizing.dart';
10+
part 'scale_resizing.dart';
11+
12+
/// An abstract class the provides a common interface for all resize modes.
13+
sealed class ResizeHandler {
14+
/// A default constructor for [ResizeHandler].
15+
const ResizeHandler();
16+
17+
/// Resizes the given [explodedRect] to fit within the [clampingRect].
18+
///
19+
/// Specifying the [handle] will determine how the [explodedRect] will be
20+
/// resized.
21+
///
22+
/// The [initialRect] helps determine the initial state of the rectangle.
23+
///
24+
/// The [clampingRect] is the box that the [explodedRect] is not allowed
25+
/// to go outside of when dragging or resizing.
26+
///
27+
/// The [constraints] is the constraints that the [explodedRect] is not
28+
/// allowed to shrink or grow beyond.
29+
(Box, Box, bool) resize({
30+
required Box initialRect,
31+
required Box explodedRect,
32+
required Box clampingRect,
33+
required HandlePosition handle,
34+
required Constraints constraints,
35+
required Flip flip,
36+
});
37+
}

0 commit comments

Comments
 (0)