reset_dimension.dart
4.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import 'package:flutter/rendering.dart';
import 'package:flutter/widgets.dart';
import '../utils/render_box_layout.dart';
import '../utils/render_box_offset.dart';
class ResetDimension extends SingleChildRenderObjectWidget {
final double? height;
final double? depth;
final double? width;
final CrossAxisAlignment horizontalAlignment;
const ResetDimension({
Key? key,
this.height,
this.depth,
this.width,
this.horizontalAlignment = CrossAxisAlignment.center,
required Widget child,
}) : super(key: key, child: child);
@override
RenderResetDimension createRenderObject(BuildContext context) =>
RenderResetDimension(
layoutHeight: height,
layoutWidth: width,
layoutDepth: depth,
horizontalAlignment: horizontalAlignment,
);
@override
void updateRenderObject(
BuildContext context, RenderResetDimension renderObject) =>
renderObject
..layoutHeight = height
..layoutDepth = depth
..layoutWidth = width
..horizontalAlignment = horizontalAlignment;
}
class RenderResetDimension extends RenderShiftedBox {
RenderResetDimension({
RenderBox? child,
double? layoutHeight,
double? layoutDepth,
double? layoutWidth,
CrossAxisAlignment horizontalAlignment = CrossAxisAlignment.center,
}) : _layoutHeight = layoutHeight,
_layoutDepth = layoutDepth,
_layoutWidth = layoutWidth,
_horizontalAlignment = horizontalAlignment,
super(child);
double? get layoutHeight => _layoutHeight;
double? _layoutHeight;
set layoutHeight(double? value) {
if (_layoutHeight != value) {
_layoutHeight = value;
markNeedsLayout();
}
}
double? get layoutDepth => _layoutDepth;
double? _layoutDepth;
set layoutDepth(double? value) {
if (_layoutDepth != value) {
_layoutDepth = value;
markNeedsLayout();
}
}
double? get layoutWidth => _layoutWidth;
double? _layoutWidth;
set layoutWidth(double? value) {
if (_layoutWidth != value) {
_layoutWidth = value;
markNeedsLayout();
}
}
CrossAxisAlignment get horizontalAlignment => _horizontalAlignment;
CrossAxisAlignment _horizontalAlignment;
set horizontalAlignment(CrossAxisAlignment value) {
if (_horizontalAlignment != value) {
_horizontalAlignment = value;
markNeedsLayout();
}
}
@override
double computeMinIntrinsicWidth(double height) =>
layoutWidth ?? super.computeMinIntrinsicWidth(height);
@override
double computeMaxIntrinsicWidth(double height) =>
layoutWidth ?? super.computeMaxIntrinsicWidth(height);
@override
double computeMinIntrinsicHeight(double width) {
if (layoutHeight == null && layoutDepth == null) {
return super.computeMinIntrinsicHeight(width);
}
if (layoutHeight != null && layoutDepth != null) {
return layoutHeight! + layoutDepth!;
}
return 0;
}
@override
double computeMaxIntrinsicHeight(double width) {
if (layoutHeight == null && layoutDepth == null) {
return super.computeMaxIntrinsicHeight(width);
}
if (layoutHeight != null && layoutDepth != null) {
return layoutHeight! + layoutDepth!;
}
return 0;
}
@override
double? computeDistanceToActualBaseline(TextBaseline baseline) =>
layoutHeight ?? super.computeDistanceToActualBaseline(baseline);
@override
Size computeDryLayout(BoxConstraints constraints) =>
_computeLayout(constraints);
@override
void performLayout() {
size = _computeLayout(constraints, dry: false);
}
Size _computeLayout(
BoxConstraints constraints, {
bool dry = true,
}) {
final child = this.child!;
final childSize = child.getLayoutSize(constraints, dry: dry);
final childHeight =
dry ? 0.0 : child.getDistanceToBaseline(TextBaseline.alphabetic)!;
final childDepth = childSize.height - childHeight;
final childWidth = childSize.width;
final height = layoutHeight ?? childHeight;
final depth = layoutDepth ?? childDepth;
final width = layoutWidth ?? childWidth;
var dx = 0.0;
switch (horizontalAlignment) {
case CrossAxisAlignment.start:
case CrossAxisAlignment.stretch:
case CrossAxisAlignment.baseline:
break;
case CrossAxisAlignment.end:
dx = width - childWidth;
break;
case CrossAxisAlignment.center:
default:
dx = (width - childWidth) / 2;
break;
}
if (!dry) {
child.offset = Offset(dx, height - childHeight);
}
return Size(width, height + depth);
}
}