Skip to content

Commit fba78d0

Browse files
author
fbchen
committed
add redundant dependency detection
1 parent 56645f5 commit fba78d0

File tree

1 file changed

+72
-32
lines changed

1 file changed

+72
-32
lines changed

lib/constrait_layout/constraint_layout.dart

Lines changed: 72 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,9 @@ class ConstraintLayout extends MultiChildRenderObjectWidget {
6666

6767
@override
6868
void updateRenderObject(
69-
BuildContext context, covariant RenderObject renderObject) {
69+
BuildContext context,
70+
covariant RenderObject renderObject,
71+
) {
7072
(renderObject as _ConstraintRenderBox)
7173
..debugShowGuideline = debugShowGuideline
7274
..debugShowPreview = debugShowPreview
@@ -81,17 +83,17 @@ class ConstraintLayout extends MultiChildRenderObjectWidget {
8183

8284
class CL {
8385
// size constraint
84-
static const double matchConstraint = 0;
85-
static const double matchParent = -1;
86-
static const double wrapContent = -2;
86+
static const double matchConstraint = -3.1415926;
87+
static const double matchParent = -2.7182818;
88+
static const double wrapContent = -0.6180339;
8789

8890
// visibility
8991
static const CLVisibility visible = CLVisibility.visible;
9092
static const CLVisibility gone = CLVisibility.gone;
9193
static const CLVisibility invisible = CLVisibility.invisible;
9294

9395
// anchor
94-
static const String parent = 'parent';
96+
static const String parent = 'ZG9uJ3QgZnVjayBtb3VudGFpbiE=';
9597
}
9698

9799
enum CLVisibility {
@@ -179,30 +181,30 @@ class Constrained extends ParentDataWidget<_ConstraintBoxData> {
179181
// TODO consider flow
180182
// group is pointless
181183

182-
const Constrained(
183-
{Key? key,
184-
required Widget child,
185-
required this.width,
186-
required this.height,
187-
this.id,
188-
this.leftToLeft,
189-
this.leftToRight,
190-
this.rightToLeft,
191-
this.rightToRight,
192-
this.topToTop,
193-
this.topToBottom,
194-
this.bottomToTop,
195-
this.bottomToBottom,
196-
this.clickPadding,
197-
this.visibility,
198-
this.margin,
199-
this.goneMargin,
200-
this.center,
201-
this.centerHorizontal,
202-
this.centerVertical,
203-
this.horizontalBias,
204-
this.verticalBias})
205-
: super(key: key, child: child);
184+
const Constrained({
185+
Key? key,
186+
required Widget child,
187+
required this.width,
188+
required this.height,
189+
this.id,
190+
this.leftToLeft,
191+
this.leftToRight,
192+
this.rightToLeft,
193+
this.rightToRight,
194+
this.topToTop,
195+
this.topToBottom,
196+
this.bottomToTop,
197+
this.bottomToBottom,
198+
this.clickPadding,
199+
this.visibility,
200+
this.margin,
201+
this.goneMargin,
202+
this.center,
203+
this.centerHorizontal,
204+
this.centerVertical,
205+
this.horizontalBias,
206+
this.verticalBias,
207+
}) : super(key: key, child: child);
206208

207209
bool checkSize(double size) {
208210
if (size == CL.matchParent ||
@@ -592,6 +594,19 @@ class _ConstraintRenderBox extends RenderBox
592594
}
593595
}
594596

597+
void _debugEnsureNullDependency(
598+
_NodeDependency node,
599+
_NodeDependency? dependency,
600+
String direction,
601+
) {
602+
if (_debugCheckDependencies == true && kDebugMode) {
603+
if (dependency != null) {
604+
debugPrint(
605+
'Warning: The child element with id ${node.nodeId} has a duplicate $direction dependency.');
606+
}
607+
}
608+
}
609+
595610
bool _isInternalBox(RenderBox renderBox) {
596611
if (renderBox is _GuidelineRenderBox) {
597612
return true;
@@ -602,7 +617,10 @@ class _ConstraintRenderBox extends RenderBox
602617
}
603618

604619
// child and id will not be null at the same time
605-
_NodeDependency _getNodeDependencyForChild(RenderBox? child, String? id) {
620+
_NodeDependency _getNodeDependencyForChild(
621+
RenderBox? child,
622+
String? id,
623+
) {
606624
_NodeDependency? node;
607625
if (child != null) {
608626
node = _nodeDependencies[child];
@@ -654,41 +672,57 @@ class _ConstraintRenderBox extends RenderBox
654672
currentNode.visibility = childParentData.visibility;
655673

656674
if (childParentData.leftToLeft != null) {
675+
_debugEnsureNullDependency(
676+
currentNode, currentNode.leftDependency, 'left');
657677
currentNode.leftDependency =
658678
_getNodeDependencyForChild(null, childParentData.leftToLeft);
659679
currentNode.leftDependencyType = 0;
660680
}
661681
if (childParentData.leftToRight != null) {
682+
_debugEnsureNullDependency(
683+
currentNode, currentNode.leftDependency, 'left');
662684
currentNode.leftDependency =
663685
_getNodeDependencyForChild(null, childParentData.leftToRight);
664686
currentNode.leftDependencyType = 1;
665687
}
666688
if (childParentData.rightToLeft != null) {
689+
_debugEnsureNullDependency(
690+
currentNode, currentNode.rightDependency, 'right');
667691
currentNode.rightDependency =
668692
_getNodeDependencyForChild(null, childParentData.rightToLeft);
669693
currentNode.rightDependencyType = 0;
670694
}
671695
if (childParentData.rightToRight != null) {
696+
_debugEnsureNullDependency(
697+
currentNode, currentNode.rightDependency, 'right');
672698
currentNode.rightDependency =
673699
_getNodeDependencyForChild(null, childParentData.rightToRight);
674700
currentNode.rightDependencyType = 1;
675701
}
676702
if (childParentData.topToTop != null) {
703+
_debugEnsureNullDependency(
704+
currentNode, currentNode.topDependency, 'top');
677705
currentNode.topDependency =
678706
_getNodeDependencyForChild(null, childParentData.topToTop);
679707
currentNode.topDependencyType = 0;
680708
}
681709
if (childParentData.topToBottom != null) {
710+
_debugEnsureNullDependency(
711+
currentNode, currentNode.topDependency, 'top');
682712
currentNode.topDependency =
683713
_getNodeDependencyForChild(null, childParentData.topToBottom);
684714
currentNode.topDependencyType = 1;
685715
}
686716
if (childParentData.bottomToTop != null) {
717+
_debugEnsureNullDependency(
718+
currentNode, currentNode.bottomDependency, 'bottom');
687719
currentNode.bottomDependency =
688720
_getNodeDependencyForChild(null, childParentData.bottomToTop);
689721
currentNode.bottomDependencyType = 0;
690722
}
691723
if (childParentData.bottomToBottom != null) {
724+
_debugEnsureNullDependency(
725+
currentNode, currentNode.bottomDependency, 'bottom');
692726
currentNode.bottomDependency =
693727
_getNodeDependencyForChild(null, childParentData.bottomToBottom);
694728
currentNode.bottomDependencyType = 1;
@@ -1030,7 +1064,10 @@ class _ConstraintRenderBox extends RenderBox
10301064
}
10311065

10321066
@override
1033-
bool hitTestChildren(BoxHitTestResult result, {required Offset position}) {
1067+
bool hitTestChildren(
1068+
BoxHitTestResult result, {
1069+
required Offset position,
1070+
}) {
10341071
RenderBox? child = lastChild;
10351072
while (child != null) {
10361073
_ConstraintBoxData childParentData =
@@ -1074,7 +1111,10 @@ class _ConstraintRenderBox extends RenderBox
10741111
}
10751112

10761113
@override
1077-
void paint(PaintingContext context, Offset offset) {
1114+
void paint(
1115+
PaintingContext context,
1116+
Offset offset,
1117+
) {
10781118
RenderBox? child = firstChild;
10791119
while (child != null) {
10801120
_ConstraintBoxData childParentData =

0 commit comments

Comments
 (0)