Skip to content

Commit c7ad856

Browse files
committed
add constraint define
1 parent fe965e8 commit c7ad856

File tree

1 file changed

+79
-20
lines changed

1 file changed

+79
-20
lines changed

lib/src/constraint_layout.dart

Lines changed: 79 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import 'package:flutter/rendering.dart';
1212
1313
class ConstraintLayout extends MultiChildRenderObjectWidget {
1414
/// Constraints can be separated from widgets
15-
final List<Constraint> childConstraints;
15+
final List<ConstraintDefine>? childConstraints;
1616

1717
final bool debugShowGuideline;
1818
final bool debugShowClickArea;
@@ -25,7 +25,7 @@ class ConstraintLayout extends MultiChildRenderObjectWidget {
2525

2626
ConstraintLayout({
2727
Key? key,
28-
this.childConstraints = const [],
28+
this.childConstraints,
2929
required List<Widget> children,
3030
this.debugShowGuideline = false,
3131
this.debugShowClickArea = false,
@@ -44,7 +44,7 @@ class ConstraintLayout extends MultiChildRenderObjectWidget {
4444
RenderObject createRenderObject(BuildContext context) {
4545
assert(_debugEnsureNotEmptyString('debugName', debugName));
4646
return _ConstraintRenderBox()
47-
.._childConstraints = childConstraints
47+
..childConstraints = childConstraints
4848
.._debugShowGuideline = debugShowGuideline
4949
.._debugShowClickArea = debugShowClickArea
5050
.._debugPrintConstraints = debugPrintConstraints
@@ -433,9 +433,23 @@ class _Align {
433433

434434
typedef OnLayoutCallback = void Function(RenderObject renderObject, Rect rect);
435435

436-
class Constraint {
436+
class ConstraintDefine {
437437
final ConstraintId? id;
438438

439+
ConstraintDefine(this.id);
440+
441+
@override
442+
bool operator ==(Object other) =>
443+
identical(this, other) ||
444+
other is ConstraintDefine &&
445+
runtimeType == other.runtimeType &&
446+
id == other.id;
447+
448+
@override
449+
int get hashCode => id.hashCode;
450+
}
451+
452+
class Constraint extends ConstraintDefine {
439453
/// 'wrap_content'、'match_parent'、'match_constraint'、'48, etc'
440454
/// 'match_parent' will be converted to the base constraints
441455
final double width;
@@ -538,7 +552,7 @@ class Constraint {
538552
final bool? ratioBaseOnWidth;
539553

540554
Constraint({
541-
this.id,
555+
ConstraintId? id,
542556
this.width = wrapContent,
543557
this.height = wrapContent,
544558
@_baseConstraint this.left,
@@ -581,14 +595,14 @@ class Constraint {
581595
this.maxHeight = matchParent,
582596
this.widthHeightRatio,
583597
this.ratioBaseOnWidth,
584-
});
598+
}) : super(id);
585599

586600
@override
587601
bool operator ==(Object other) =>
588602
identical(this, other) ||
589-
other is Constraint &&
603+
super == other &&
604+
other is Constraint &&
590605
runtimeType == other.runtimeType &&
591-
id == other.id &&
592606
width == other.width &&
593607
height == other.height &&
594608
clickPadding == other.clickPadding &&
@@ -633,7 +647,7 @@ class Constraint {
633647

634648
@override
635649
int get hashCode =>
636-
id.hashCode ^
650+
super.hashCode ^
637651
width.hashCode ^
638652
height.hashCode ^
639653
clickPadding.hashCode ^
@@ -1161,14 +1175,17 @@ class UnConstrained extends ParentDataWidget<_ConstraintBoxData> {
11611175
@override
11621176
void applyParentData(RenderObject renderObject) {
11631177
assert(renderObject.parent is _ConstraintRenderBox);
1164-
List<Constraint> childConstraints =
1178+
List<ConstraintDefine>? childConstraints =
11651179
(renderObject.parent as _ConstraintRenderBox)._childConstraints;
1166-
Iterable<Constraint> constraintIterable =
1167-
childConstraints.where((element) => element.id == id);
1180+
assert(childConstraints != null,
1181+
'Can not find Constraint for child with id $id.');
1182+
Iterable<ConstraintDefine> constraintIterable =
1183+
childConstraints!.where((element) => element.id == id);
11681184
assert(constraintIterable.isNotEmpty,
11691185
'Can not find Constraint for child with id $id.');
11701186
assert(constraintIterable.length == 1, 'Duplicate id in childConstraints.');
1171-
Constraint constraint = constraintIterable.first;
1187+
assert(constraintIterable.first is Constraint);
1188+
Constraint constraint = constraintIterable.first as Constraint;
11721189
assert(constraint.validate());
11731190
constraint.applyTo(renderObject);
11741191
}
@@ -1183,7 +1200,7 @@ class _ConstraintRenderBox extends RenderBox
11831200
with
11841201
ContainerRenderObjectMixin<RenderBox, _ConstraintBoxData>,
11851202
RenderBoxContainerDefaultsMixin<RenderBox, _ConstraintBoxData> {
1186-
late List<Constraint> _childConstraints;
1203+
List<ConstraintDefine>? _childConstraints;
11871204
late bool _debugShowGuideline;
11881205
late bool _debugShowClickArea;
11891206
late bool _debugPrintConstraints;
@@ -1207,15 +1224,23 @@ class _ConstraintRenderBox extends RenderBox
12071224
Queue<int> layoutTimeUsage = Queue();
12081225
Queue<int> paintTimeUsage = Queue();
12091226

1210-
set childConstraints(List<Constraint> value) {
1227+
set childConstraints(List<ConstraintDefine>? value) {
12111228
bool isSameList = true;
1212-
if (_childConstraints.length != value.length) {
1229+
if (_childConstraints == null && value == null) {
1230+
// do nothing
1231+
} else if (_childConstraints == null) {
1232+
isSameList = false;
1233+
} else if (value == null) {
12131234
isSameList = false;
12141235
} else {
1215-
for (int i = 0; i < _childConstraints.length; i++) {
1216-
if (_childConstraints[i] != value[i]) {
1217-
isSameList = false;
1218-
break;
1236+
if (_childConstraints!.length != value.length) {
1237+
isSameList = false;
1238+
} else {
1239+
for (int i = 0; i < _childConstraints!.length; i++) {
1240+
if (_childConstraints![i] != value[i]) {
1241+
isSameList = false;
1242+
break;
1243+
}
12191244
}
12201245
}
12211246
}
@@ -2791,6 +2816,40 @@ class _HelperBox extends RenderBox {
27912816
}
27922817
}
27932818

2819+
class GuidelineDefine extends ConstraintDefine {
2820+
final double? guidelineBegin;
2821+
final double? guidelineEnd;
2822+
final double? guidelinePercent;
2823+
final bool horizontal;
2824+
2825+
GuidelineDefine({
2826+
required ConstraintId id,
2827+
this.guidelineBegin,
2828+
this.guidelineEnd,
2829+
this.guidelinePercent,
2830+
this.horizontal = false,
2831+
}) : super(id);
2832+
2833+
@override
2834+
bool operator ==(Object other) =>
2835+
identical(this, other) ||
2836+
super == other &&
2837+
other is GuidelineDefine &&
2838+
runtimeType == other.runtimeType &&
2839+
guidelineBegin == other.guidelineBegin &&
2840+
guidelineEnd == other.guidelineEnd &&
2841+
guidelinePercent == other.guidelinePercent &&
2842+
horizontal == other.horizontal;
2843+
2844+
@override
2845+
int get hashCode =>
2846+
super.hashCode ^
2847+
guidelineBegin.hashCode ^
2848+
guidelineEnd.hashCode ^
2849+
guidelinePercent.hashCode ^
2850+
horizontal.hashCode;
2851+
}
2852+
27942853
class Guideline extends LeafRenderObjectWidget {
27952854
final ConstraintId id;
27962855
final double? guidelineBegin;

0 commit comments

Comments
 (0)