line.dart
19.1 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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
//ignore_for_file: lines_longer_than_80_chars
import 'dart:math' as math;
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter/widgets.dart';
import '../constants.dart';
import '../utils/render_box_offset.dart';
import '../utils/render_box_layout.dart';
class LineParentData extends ContainerBoxParentData<RenderBox> {
// The first canBreakBefore has no effect
bool canBreakBefore = false;
BoxConstraints Function(double height, double depth)? customCrossSize;
double trailingMargin = 0.0;
bool alignerOrSpacer = false;
@override
String toString() =>
'${super.toString()}; canBreakBefore = $canBreakBefore; customSize = ${customCrossSize != null}; trailingMargin = $trailingMargin; alignerOrSpacer = $alignerOrSpacer';
}
class LineElement extends ParentDataWidget<LineParentData> {
final bool canBreakBefore;
final BoxConstraints Function(double height, double depth)? customCrossSize;
final double trailingMargin;
final bool alignerOrSpacer;
const LineElement({
Key? key,
this.canBreakBefore = false,
this.customCrossSize,
this.trailingMargin = 0.0,
this.alignerOrSpacer = false,
required Widget child,
}) : super(key: key, child: child);
@override
void applyParentData(RenderObject renderObject) {
assert(renderObject.parentData is LineParentData);
final parentData = renderObject.parentData as LineParentData;
var needsLayout = false;
if (parentData.canBreakBefore != canBreakBefore) {
parentData.canBreakBefore = canBreakBefore;
needsLayout = true;
}
if (parentData.customCrossSize != customCrossSize) {
parentData.customCrossSize = customCrossSize;
needsLayout = true;
}
if (parentData.trailingMargin != trailingMargin) {
parentData.trailingMargin = trailingMargin;
needsLayout = true;
}
if (parentData.alignerOrSpacer != alignerOrSpacer) {
parentData.alignerOrSpacer = alignerOrSpacer;
needsLayout = true;
}
if (needsLayout) {
final targetParent = renderObject.parent;
if (targetParent is RenderObject) targetParent.markNeedsLayout();
}
}
@override
void debugFillProperties(DiagnosticPropertiesBuilder properties) {
super.debugFillProperties(properties);
properties.add(FlagProperty('canBreakBefore',
value: canBreakBefore, ifTrue: 'allow breaking before'));
properties.add(FlagProperty('customSize',
value: customCrossSize != null, ifTrue: 'using relative size'));
properties.add(DoubleProperty('trailingMargin', trailingMargin));
properties.add(FlagProperty('alignerOrSpacer',
value: alignerOrSpacer, ifTrue: 'is a alignment symbol'));
}
@override
Type get debugTypicalAncestorWidgetClass => Line;
}
/// Line provides abilities for line breaks, delim-sizing and background color indicator.
class Line extends MultiChildRenderObjectWidget {
Line({
Key? key,
this.crossAxisAlignment = CrossAxisAlignment.baseline,
this.minDepth = 0.0,
this.minHeight = 0.0,
this.textBaseline = TextBaseline.alphabetic,
this.textDirection,
List<Widget> children = const [],
}) : super(key: key, children: children);
final CrossAxisAlignment crossAxisAlignment;
final double minDepth;
final double minHeight;
final TextBaseline textBaseline;
final TextDirection? textDirection;
bool get _needTextDirection => true;
@protected
TextDirection? getEffectiveTextDirection(BuildContext context) =>
textDirection ?? (_needTextDirection ? Directionality.of(context) : null);
@override
RenderLine createRenderObject(BuildContext context) => RenderLine(
crossAxisAlignment: crossAxisAlignment,
minDepth: minDepth,
minHeight: minHeight,
textBaseline: textBaseline,
textDirection: getEffectiveTextDirection(context),
);
@override
void updateRenderObject(BuildContext context, RenderLine renderObject) =>
renderObject
..crossAxisAlignment = crossAxisAlignment
..minDepth = minDepth
..minHeight = minHeight
..textBaseline = textBaseline
..textDirection = getEffectiveTextDirection(context);
@override
void debugFillProperties(DiagnosticPropertiesBuilder properties) {
super.debugFillProperties(properties);
properties.add(EnumProperty<TextBaseline>('textBaseline', textBaseline,
defaultValue: null));
properties.add(EnumProperty<CrossAxisAlignment>(
'crossAxisAlignment', crossAxisAlignment));
properties.add(EnumProperty<TextDirection>('textDirection', textDirection,
defaultValue: null));
}
}
// RenderLine
class RenderLine extends RenderBox
with
ContainerRenderObjectMixin<RenderBox, LineParentData>,
RenderBoxContainerDefaultsMixin<RenderBox, LineParentData>,
DebugOverflowIndicatorMixin {
RenderLine({
List<RenderBox>? children,
CrossAxisAlignment crossAxisAlignment = CrossAxisAlignment.baseline,
double minDepth = 0,
double minHeight = 0,
TextBaseline textBaseline = TextBaseline.alphabetic,
TextDirection? textDirection = TextDirection.ltr,
}) : _crossAxisAlignment = crossAxisAlignment,
_minDepth = minDepth,
_minHeight = minHeight,
_textBaseline = textBaseline,
_textDirection = textDirection {
addAll(children);
}
CrossAxisAlignment get crossAxisAlignment => _crossAxisAlignment;
CrossAxisAlignment _crossAxisAlignment;
set crossAxisAlignment(CrossAxisAlignment value) {
if (_crossAxisAlignment != value) {
_crossAxisAlignment = value;
markNeedsLayout();
}
}
double get minDepth => _minDepth;
double _minDepth;
set minDepth(double value) {
if (_minDepth != value) {
_minDepth = value;
markNeedsLayout();
}
}
double get minHeight => _minHeight;
double _minHeight;
set minHeight(double value) {
if (_minHeight != value) {
_minHeight = value;
markNeedsLayout();
}
}
TextBaseline get textBaseline => _textBaseline;
TextBaseline _textBaseline;
set textBaseline(TextBaseline value) {
if (_textBaseline != value) {
_textBaseline = value;
markNeedsLayout();
}
}
TextDirection? get textDirection => _textDirection;
TextDirection? _textDirection;
set textDirection(TextDirection? value) {
if (_textDirection != value) {
_textDirection = value;
markNeedsLayout();
}
}
bool get _debugHasNecessaryDirections {
assert(textDirection != null,
'Horizontal $runtimeType has a null textDirection, so the alignment cannot be resolved.');
return true;
}
double? _overflow;
bool get _hasOverflow => _overflow! > precisionErrorTolerance;
@override
void setupParentData(RenderBox child) {
if (child.parentData is! LineParentData) {
child.parentData = LineParentData();
}
}
double _getIntrinsicSize({
required Axis sizingDirection,
required double
extent, // the extent in the direction that isn't the sizing direction
required double Function(RenderBox child, double extent)
childSize, // a method to find the size in the sizing direction
}) {
if (sizingDirection == Axis.horizontal) {
// INTRINSIC MAIN SIZE
// Intrinsic main size is the smallest size the flex container can take
// while maintaining the min/max-content contributions of its flex items.
var inflexibleSpace = 0.0;
var child = firstChild;
while (child != null) {
inflexibleSpace += childSize(child, extent);
final childParentData = child.parentData as LineParentData;
child = childParentData.nextSibling;
}
return inflexibleSpace;
} else {
// INTRINSIC CROSS SIZE
// Intrinsic cross size is the max of the intrinsic cross sizes of the
// children, after the flexible children are fit into the available space,
// with the children sized using their max intrinsic dimensions.
var maxCrossSize = 0.0;
var child = firstChild;
while (child != null) {
final childMainSize = child.getMaxIntrinsicWidth(double.infinity);
final crossSize = childSize(child, childMainSize);
maxCrossSize = math.max(maxCrossSize, crossSize);
final childParentData = child.parentData as LineParentData;
child = childParentData.nextSibling;
}
return maxCrossSize;
}
}
@override
double computeMinIntrinsicWidth(double height) => _getIntrinsicSize(
sizingDirection: Axis.horizontal,
extent: height,
childSize: (RenderBox child, double extent) =>
child.getMinIntrinsicWidth(extent),
);
@override
double computeMaxIntrinsicWidth(double height) => _getIntrinsicSize(
sizingDirection: Axis.horizontal,
extent: height,
childSize: (RenderBox child, double extent) =>
child.getMaxIntrinsicWidth(extent),
);
@override
double computeMinIntrinsicHeight(double width) => _getIntrinsicSize(
sizingDirection: Axis.vertical,
extent: width,
childSize: (RenderBox child, double extent) =>
child.getMinIntrinsicHeight(extent),
);
@override
double computeMaxIntrinsicHeight(double width) => _getIntrinsicSize(
sizingDirection: Axis.vertical,
extent: width,
childSize: (RenderBox child, double extent) =>
child.getMaxIntrinsicHeight(extent),
);
double maxHeightAboveBaseline = 0.0;
double maxHeightAboveEndBaseline = 0.0;
@override
double computeDistanceToActualBaseline(TextBaseline baseline) {
assert(!debugNeedsLayout);
return maxHeightAboveBaseline;
}
@protected
late List<double> caretOffsets;
List<double>? alignColWidth;
@override
Size computeDryLayout(BoxConstraints constraints) =>
_computeLayout(constraints);
@override
void performLayout() {
size = _computeLayout(constraints, dry: false);
}
Size _computeLayout(
BoxConstraints constraints, {
bool dry = true,
}) {
assert(_debugHasNecessaryDirections);
// First pass, layout fixed-sized children to calculate height and depth
var maxHeightAboveBaseline = 0.0;
var maxDepthBelowBaseline = 0.0;
var child = firstChild;
final relativeChildren = <RenderBox>[];
final alignerAndSpacers = <RenderBox>[];
final sizeMap = <RenderBox, Size>{};
while (child != null) {
final childParentData = child.parentData as LineParentData;
if (childParentData.customCrossSize != null) {
relativeChildren.add(child);
} else if (childParentData.alignerOrSpacer) {
alignerAndSpacers.add(child);
} else {
final childSize = child.getLayoutSize(infiniteConstraint, dry: dry);
sizeMap[child] = childSize;
final distance = dry ? 0.0 : child.getDistanceToBaseline(textBaseline)!;
maxHeightAboveBaseline = math.max(maxHeightAboveBaseline, distance);
maxDepthBelowBaseline =
math.max(maxDepthBelowBaseline, childSize.height - distance);
}
assert(child.parentData == childParentData);
child = childParentData.nextSibling;
}
// Second pass, layout custom-sized children
for (final child in relativeChildren) {
final childParentData = child.parentData as LineParentData;
assert(childParentData.customCrossSize != null);
final childConstraints = childParentData.customCrossSize!(
maxHeightAboveBaseline, maxDepthBelowBaseline);
final childSize = child.getLayoutSize(childConstraints, dry: dry);
sizeMap[child] = childSize;
final distance = dry ? 0.0 : child.getDistanceToBaseline(textBaseline)!;
maxHeightAboveBaseline = math.max(maxHeightAboveBaseline, distance);
maxDepthBelowBaseline =
math.max(maxDepthBelowBaseline, childSize.height - distance);
}
// Apply mininmum size constraint
maxHeightAboveBaseline = math.max(maxHeightAboveBaseline, minHeight);
maxDepthBelowBaseline = math.max(maxDepthBelowBaseline, minDepth);
// Third pass. Calculate column width separate by aligners and spacers.
//
// Also determine offset for each children in the meantime, as if there are
// no aligning instructions. If there are indeed none, this will be the
// final pass.
child = firstChild;
var mainPos = 0.0;
var lastColPosition = mainPos;
final colWidths = <double>[];
var caretOffsets = [mainPos];
while (child != null) {
final childParentData = child.parentData as LineParentData;
var childSize = sizeMap[child] ?? Size.zero;
if (childParentData.alignerOrSpacer) {
final childConstraints = BoxConstraints.tightFor(width: 0.0);
childSize = child.getLayoutSize(childConstraints, dry: dry);
colWidths.add(mainPos - lastColPosition);
lastColPosition = mainPos;
}
if (!dry) {
childParentData.offset =
Offset(mainPos, maxHeightAboveBaseline - child.layoutHeight);
}
mainPos += childSize.width + childParentData.trailingMargin;
caretOffsets.add(mainPos);
child = childParentData.nextSibling;
}
colWidths.add(mainPos - lastColPosition);
var size = constraints.constrain(
Size(mainPos, maxHeightAboveBaseline + maxDepthBelowBaseline));
if (!dry) {
this.caretOffsets = caretOffsets;
this._overflow = mainPos - size.width;
this.maxHeightAboveBaseline = maxHeightAboveBaseline;
} else {
return size;
}
// If we have no aligners or spacers, no need to do the fourth pass.
if (alignerAndSpacers.isEmpty) return size;
// If we are have no aligning instructions, no need to do the fourth pass.
if (this.alignColWidth == null) {
// Report column width
this.alignColWidth = colWidths;
return size;
}
// If the code reaches here, means we have aligners/spacers and the
// aligning instructions.
// First report first column width.
final alignColWidth = List.of(this.alignColWidth!, growable: false)
..[0] = colWidths.first;
this.alignColWidth = alignColWidth;
// We will determine the width of the spacers using aligning instructions
///
/// Aligner Spacer Aligner
/// | | |
/// x | f o o b a | r | z z z
/// | |-------| |
/// y y | f | o o b a r |
/// | |-------| |
/// Index: 0 1 2
/// Col: 0 1 2
///
var aligner = true;
var index = 0;
for (final alignerOrSpacer in alignerAndSpacers) {
if (aligner) {
alignerOrSpacer.layout(BoxConstraints.tightFor(width: 0.0),
parentUsesSize: true);
} else {
alignerOrSpacer.layout(
BoxConstraints.tightFor(
width: alignColWidth[index] +
(index + 1 < alignColWidth.length - 1
? alignColWidth[index + 1]
: 0) -
colWidths[index] -
(index + 1 < colWidths.length - 1 ? colWidths[index + 1] : 0),
),
parentUsesSize: true,
);
}
aligner = !aligner;
index++;
}
// Fourth pass, determine position for each children
child = firstChild;
mainPos = 0.0;
this.caretOffsets
..clear()
..add(mainPos);
while (child != null) {
final childParentData = child.parentData as LineParentData;
childParentData.offset =
Offset(mainPos, maxHeightAboveBaseline - child.layoutHeight);
mainPos += child.size.width + childParentData.trailingMargin;
this.caretOffsets.add(mainPos);
child = childParentData.nextSibling;
}
size = constraints.constrain(
Size(mainPos, maxHeightAboveBaseline + maxDepthBelowBaseline));
this._overflow = mainPos - size.width;
return size;
}
@override
bool hitTestChildren(BoxHitTestResult result, {required Offset position}) =>
defaultHitTestChildren(result, position: position);
// List<Rect> get rects {
// final constraints = this.constraints;
// if (constraints is BreakableBoxConstraints) {
// var i = 0;
// var crossPos = 0.0;
// final res = <Rect>[];
// for (final size in size.lineSizes) {
// final mainPos = i == 0
// ? 0.0
// : constraints.maxWidthFirstLine - constraints.maxWidthBodyLines;
// res.add(Rect.fromLTWH(mainPos, crossPos, size.width, size.height));
// crossPos += size.height;
// i++;
// }
// return res;
// } else {
// return [Rect.fromLTWH(0, 0, size.width, size.height)];
// }
// }
@override
void paint(PaintingContext context, Offset offset) {
if (!_hasOverflow) {
defaultPaint(context, offset);
return;
}
if (size.isEmpty) return;
context.pushClipRect(
needsCompositing, offset, Offset.zero & size, defaultPaint);
assert(() {
// Only set this if it's null to save work. It gets reset to null if the
// _direction changes.
final debugOverflowHints = <DiagnosticsNode>[
ErrorDescription(
'The edge of the $runtimeType that is overflowing has been marked '
'in the rendering with a yellow and black striped pattern. This is '
'usually caused by the contents being too big for the $runtimeType.',
),
ErrorHint(
'Consider applying a flex factor (e.g. using an Expanded widget) to '
'force the children of the $runtimeType to fit within the available '
'space instead of being sized to their natural size.',
),
ErrorHint(
'This is considered an error condition because it indicates that there '
'is content that cannot be seen. If the content is legitimately bigger '
'than the available space, consider clipping it with a ClipRect widget '
'before putting it in the flex, or using a scrollable container rather '
'than a Flex, like a ListView.',
),
];
// Simulate a child rect that overflows by the right amount. This child
// rect is never used for drawing, just for determining the overflow
// location and amount.
Rect overflowChildRect;
overflowChildRect = Rect.fromLTWH(0.0, 0.0, size.width + _overflow!, 0.0);
paintOverflowIndicator(
context, offset, Offset.zero & size, overflowChildRect,
overflowHints: debugOverflowHints);
return true;
}());
}
@override
Rect? describeApproximatePaintClip(RenderObject child) =>
_hasOverflow ? Offset.zero & size : null;
@override
String toStringShort() {
var header = super.toStringShort();
if (_overflow != null && _hasOverflow) header += ' OVERFLOWING';
return header;
}
@override
void debugFillProperties(DiagnosticPropertiesBuilder properties) {
super.debugFillProperties(properties);
properties.add(EnumProperty<CrossAxisAlignment>(
'crossAxisAlignment', crossAxisAlignment));
properties.add(EnumProperty<TextDirection>('textDirection', textDirection,
defaultValue: null));
properties.add(EnumProperty<TextBaseline>('textBaseline', textBaseline,
defaultValue: null));
// properties.add(DoubleProperty('baselineOffset', baselineOffset));
}
}